本文整理匯總了Java中com.google.gson.JsonNull類的典型用法代碼示例。如果您正苦於以下問題:Java JsonNull類的具體用法?Java JsonNull怎麽用?Java JsonNull使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
JsonNull類屬於com.google.gson包,在下文中一共展示了JsonNull類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: deserialize
import com.google.gson.JsonNull; //導入依賴的package包/類
@Override
public EntityWrapper deserialize(JsonElement jsonElement, Type typef, JsonDeserializationContext context) throws JsonParseException {
if (jsonElement == null || jsonElement instanceof JsonNull) {
return null;
}
JsonObject root = jsonElement.getAsJsonObject();
boolean isNull = root.get(KEY_IS_NULL).getAsBoolean();
Entity entity = null;
if (!isNull) {
int dbotype = root.get(KEY_TYPE).getAsInt();
entity = context.deserialize(root.get(KEY_ENTITY), AttachmentsTypes.classForType(dbotype));
}
return new EntityWrapper(entity);
}
示例2: serialize
import com.google.gson.JsonNull; //導入依賴的package包/類
@Override
public JsonElement serialize(EntityWrapper wrapper, Type type, JsonSerializationContext context) {
if (wrapper == null) {
return JsonNull.INSTANCE;
}
JsonObject root = new JsonObject();
Entity entity = wrapper.get();
root.add(KEY_IS_NULL, new JsonPrimitive(entity == null));
if (entity != null) {
root.add(KEY_TYPE, new JsonPrimitive(AttachmentsTypes.typeForInstance(entity)));
root.add(KEY_ENTITY, context.serialize(entity));
}
return root;
}
示例3: toJson
import com.google.gson.JsonNull; //導入依賴的package包/類
private static JsonElement toJson(Object value) {
if(value instanceof ConfigurationSection) {
return toJson((ConfigurationSection) value);
} else if(value instanceof Map) {
return toJson((Map) value);
} else if(value instanceof List) {
return toJson((List) value);
} else if(value instanceof String) {
return new JsonPrimitive((String) value);
} else if(value instanceof Character) {
return new JsonPrimitive((Character) value);
} else if(value instanceof Number) {
return new JsonPrimitive((Number) value);
} else if(value instanceof Boolean) {
return new JsonPrimitive((Boolean) value);
} else if(value == null) {
return JsonNull.INSTANCE;
} else {
throw new IllegalArgumentException("Cannot coerce " + value.getClass().getSimpleName() + " to JSON");
}
}
示例4: parse
import com.google.gson.JsonNull; //導入依賴的package包/類
public static JsonElement parse(JsonReader reader) throws JsonParseException {
boolean isEmpty = true;
try {
reader.peek();
isEmpty = false;
return (JsonElement) TypeAdapters.JSON_ELEMENT.read(reader);
} catch (Throwable e) {
if (isEmpty) {
return JsonNull.INSTANCE;
}
throw new JsonIOException(e);
} catch (Throwable e2) {
throw new JsonSyntaxException(e2);
} catch (Throwable e22) {
throw new JsonIOException(e22);
} catch (Throwable e222) {
throw new JsonSyntaxException(e222);
}
}
示例5: parse
import com.google.gson.JsonNull; //導入依賴的package包/類
public static JsonElement parse(JsonReader reader) throws JsonParseException {
boolean isEmpty = true;
try {
reader.peek();
isEmpty = false;
return (JsonElement) TypeAdapters.JSON_ELEMENT.read(reader);
} catch (Throwable e) {
if (isEmpty) {
return JsonNull.INSTANCE;
}
throw new JsonSyntaxException(e);
} catch (Throwable e2) {
throw new JsonSyntaxException(e2);
} catch (Throwable e22) {
throw new JsonIOException(e22);
} catch (Throwable e222) {
throw new JsonSyntaxException(e222);
}
}
示例6: extractElement
import com.google.gson.JsonNull; //導入依賴的package包/類
public static JsonElement extractElement(JsonElement json, String field) {
if (json == null) {
return JsonNull.INSTANCE;
}
// We reached the field, return the leaf's value as a string
if (!field.contains(".")) {
if (json.isJsonObject()) {
JsonElement element = ((JsonObject) json).get(field);
return element == null ? JsonNull.INSTANCE : element;
} else {
return JsonNull.INSTANCE;
}
}
int i = field.indexOf('.');
String key = field.substring(i + 1, field.length());
JsonElement value = ((JsonObject) json).get(field.substring(0, i));
return extractElement(value, key);
}
示例7: testEqualsNonEmptyArray
import com.google.gson.JsonNull; //導入依賴的package包/類
public void testEqualsNonEmptyArray() {
JsonArray a = new JsonArray();
JsonArray b = new JsonArray();
assertEquals(a, a);
a.add(new JsonObject());
assertFalse(a.equals(b));
assertFalse(b.equals(a));
b.add(new JsonObject());
assertEquals(oson.serialize(a), oson.serialize(b));
a.add(new JsonObject());
assertFalse(a.equals(b));
assertFalse(b.equals(a));
b.add(JsonNull.INSTANCE);
assertNotEquals(oson.serialize(a), oson.serialize(b));
}
示例8: serialize
import com.google.gson.JsonNull; //導入依賴的package包/類
@Override
public JsonElement serialize(ILoggingEvent event, Type typeOfSrc, JsonSerializationContext context) {
JsonObject json = new JsonObject();
json.addProperty("name", event.getLoggerName());
json.addProperty("host", hostname);
json.addProperty("timestamp", Long.toString(event.getTimeStamp()));
json.addProperty("level", event.getLevel().toString());
json.addProperty("className", classNameConverter.convert(event));
json.addProperty("method", methodConverter.convert(event));
json.addProperty("file", fileConverter.convert(event));
json.addProperty("line", lineConverter.convert(event));
json.addProperty("thread", event.getThreadName());
json.addProperty("message", event.getFormattedMessage());
json.addProperty("runnableName", runnableName);
if (event.getThrowableProxy() == null) {
json.add("throwable", JsonNull.INSTANCE);
} else {
json.add("throwable", context.serialize(new DefaultLogThrowable(event.getThrowableProxy()), LogThrowable.class));
}
return json;
}
示例9: serialize
import com.google.gson.JsonNull; //導入依賴的package包/類
@Override
public JsonElement serialize(final Pair<Long, Long> src, final java.lang.reflect.Type typeOfSrc, final JsonSerializationContext context) {
final JsonArray array = new JsonArray();
if (src.first() != null) {
array.add(s_gson.toJsonTree(src.first()));
} else {
array.add(new JsonNull());
}
if (src.second() != null) {
array.add(s_gson.toJsonTree(src.second()));
} else {
array.add(new JsonNull());
}
return array;
}
示例10: serialize
import com.google.gson.JsonNull; //導入依賴的package包/類
@Override
public JsonElement serialize(ScienceProgress scienceProgress, Type typeOfSrc, JsonSerializationContext context) {
Science science = scienceProgress.getScience();
if (science.size() > 1) {
throw new UnsupportedOperationException("Cannot serialize science containing more than one element");
}
for (ScienceType type : ScienceType.values()) {
int quantity = science.getQuantity(type);
if (quantity == 1) {
return context.serialize(type);
}
}
if (science.getJokers() == 1) {
return new JsonPrimitive("any");
}
return JsonNull.INSTANCE;
}
示例11: serializeInventory
import com.google.gson.JsonNull; //導入依賴的package包/類
private static JsonArray serializeInventory(Inventory inv) {
JsonArray json = new JsonArray();
inv.slots().forEach(slot -> {
Optional<ItemStack> item = slot.peek();
if (item.isPresent()) {
try {
json.add(new JsonPrimitive(SerializationHelper.serialize(item.get())));
} catch (IOException ex) {
InfernoCore.logWarning("Failed to serialize ItemStack from inventory");
ex.printStackTrace();
}
} else {
json.add(JsonNull.INSTANCE);
}
});
return json;
}
示例12: deserialize
import com.google.gson.JsonNull; //導入依賴的package包/類
@Override
public BeerOnTopList deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
JsonObject object = json.getAsJsonObject();
BeerOnTopList beerOnTopList = new BeerOnTopList();
beerOnTopList.beerId = object.get("BeerID").getAsInt();
beerOnTopList.beerName = Normalizer.get().cleanHtml(object.get("BeerName").getAsString());
if (object.has("BeerStyleID") && !(object.get("BeerStyleID") instanceof JsonNull))
beerOnTopList.styleId = object.get("BeerStyleID").getAsInt();
if (!(object.get("OverallPctl") instanceof JsonNull))
beerOnTopList.overallPercentile = object.get("OverallPctl").getAsFloat();
if (!(object.get("StylePctl") instanceof JsonNull))
beerOnTopList.stylePercentile = object.get("StylePctl").getAsFloat();
if (!(object.get("AverageRating") instanceof JsonNull))
beerOnTopList.weightedRating = object.get("AverageRating").getAsFloat();
beerOnTopList.rateCount = object.get("RateCount").getAsInt();
if (object.has("HadIt") && !(object.get("HadIt") instanceof JsonNull))
beerOnTopList.ratedByUser = object.get("HadIt").getAsInt() == 1;
return beerOnTopList;
}
示例13: deserialize
import com.google.gson.JsonNull; //導入依賴的package包/類
@Override
public PlaceSearchResult deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
JsonObject object = json.getAsJsonObject();
PlaceSearchResult placeSearchResult = new PlaceSearchResult();
placeSearchResult.placeId = object.get("PlaceID").getAsInt();
placeSearchResult.placeName = Normalizer.get().cleanHtml(object.get("PlaceName").getAsString());
placeSearchResult.placeType = object.get("PlaceType").getAsInt();
placeSearchResult.city = Normalizer.get().cleanHtml(object.get("City").getAsString());
if (object.has("CountryID") && !(object.get("CountryID") instanceof JsonNull))
placeSearchResult.countryId = object.get("CountryID").getAsInt();
if (object.has("StateId") && !(object.get("StateID") instanceof JsonNull))
placeSearchResult.stateId = object.get("StateID").getAsInt();
if (object.has("Percentile") && !(object.get("Percentile") instanceof JsonNull))
placeSearchResult.overallPercentile = object.get("Percentile").getAsFloat();
if (object.has("AvgRating") && !(object.get("AvgRating") instanceof JsonNull))
placeSearchResult.averageRating = object.get("AvgRating").getAsFloat();
if (object.has("RateCount") && !(object.get("RateCount") instanceof JsonNull))
placeSearchResult.rateCount = object.get("RateCount").getAsInt();
return placeSearchResult;
}
示例14: deserialize
import com.google.gson.JsonNull; //導入依賴的package包/類
@Override
public BeerSearchResult deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
JsonObject object = json.getAsJsonObject();
BeerSearchResult beerSearchResult = new BeerSearchResult();
beerSearchResult.beerId = object.get("BeerID").getAsInt();
beerSearchResult.beerName = Normalizer.get().cleanHtml(object.get("BeerName").getAsString());
beerSearchResult.brewerId = object.get("BrewerID").getAsInt();
if (!(object.get("OverallPctl") instanceof JsonNull))
beerSearchResult.overallPercentile = object.get("OverallPctl").getAsFloat();
beerSearchResult.rateCount = object.get("RateCount").getAsInt();
if (object.has("Unrateable") && !(object.get("Unrateable") instanceof JsonNull))
beerSearchResult.unrateable = object.get("Unrateable").getAsBoolean();
if (object.has("IsAlias") && !(object.get("IsAlias") instanceof JsonNull))
beerSearchResult.alias = object.get("IsAlias").getAsBoolean();
beerSearchResult.retired = object.get("Retired").getAsBoolean();
if (object.has("IsRated") && !(object.get("IsRated") instanceof JsonNull))
beerSearchResult.ratedByUser = object.get("IsRated").getAsInt() == 1;
return beerSearchResult;
}
示例15: deserialize
import com.google.gson.JsonNull; //導入依賴的package包/類
@Override
public FeedItem deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
JsonObject object = json.getAsJsonObject();
FeedItem feedItem = new FeedItem();
feedItem.activityId = object.get("ActivityID").getAsInt();
feedItem.userId = object.get("UserID").getAsInt();
feedItem.userName = Normalizer.get().cleanHtml(object.get("Username").getAsString());
feedItem.type = object.get("Type").getAsInt();
if (!(object.get("LinkID") instanceof JsonNull))
feedItem.linkId = object.get("LinkID").getAsInt();
feedItem.linkText = object.get("LinkText").getAsString(); // Keep raw HTML
if (!(object.get("ActivityNumber") instanceof JsonNull))
feedItem.activityNumber = object.get("ActivityNumber").getAsInt();
feedItem.timeEntered = Normalizer.get().parseTime(object.get("TimeEntered").getAsString());
if (!(object.get("NumComments") instanceof JsonNull))
feedItem.numComments = object.get("NumComments").getAsInt();
return feedItem;
}