下面列出了com.google.gson.JsonNull#INSTANCE 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private JsonElement getValue(JsonArray payload, String[] parts, int index) {
Integer offset = Integer.parseInt(parts[index]);
JsonElement e = payload.get(offset);
if(e == null) {
e = JsonNull.INSTANCE;
}
index++;
if(index == parts.length) {
return e;
}
if(e.isJsonObject()) {
return getValue(e.getAsJsonObject(), parts, index);
}
else if(e.isJsonArray()) {
return getValue(e.getAsJsonArray(), parts, index);
}
else {
return e;
}
}
JsonElement stringListJsonArr(List<String> strings) {
if (strings.size() == 0) {
return JsonNull.INSTANCE;
} else if (strings.size() == 1) {
return new JsonPrimitive(strings.get(0));
} else {
JsonArray arr = new JsonArray();
for (String s : strings) {
arr.add(s);
}
return arr;
}
}
/**
* Returns a JSON sub-element from the given JsonElement and the given path
*
* @param json - a Gson JsonElement
* @param path - a JSON path, e.g. a.b.c[2].d
* @return
*/
public static JsonElement getJsonElement(JsonElement json, String path){
String[] parts = path.split("\\.|\\[|\\]");
JsonElement result = json;
for (String key : parts) {
key = key.trim();
if (key.isEmpty())
continue;
if (result == null){
result = JsonNull.INSTANCE;
break;
}
if (result.isJsonObject()){
result = ((JsonObject)result).get(key);
}
else if (result.isJsonArray()){
int ix = Integer.valueOf(key) - 1;
result = ((JsonArray)result).get(ix);
}
else break;
}
return result;
}
/** Converts an Object to a JsonElement. */
private static JsonElement toJsonElement(String name, Member member, Object object) {
if (object instanceof Jsonable) {
Jsonable jsonable = (Jsonable) object;
verifyAllowedJsonKeyName(name, member, jsonable.getClass());
return jsonable.toJson();
}
if (object instanceof String) {
return new JsonPrimitive((String) object);
}
if (object instanceof Number) {
return new JsonPrimitive((Number) object);
}
if (object instanceof Boolean) {
return new JsonPrimitive((Boolean) object);
}
if (object instanceof DateTime) {
// According to RFC7483 section 3, the syntax of dates and times is defined in RFC3339.
//
// According to RFC3339, we should use ISO8601, which is what DateTime.toString does!
return new JsonPrimitive(((DateTime) object).toString());
}
if (object == null) {
return JsonNull.INSTANCE;
}
throw new IllegalArgumentException(
String.format(
"Unknows object type '%s' in member '%s'",
object.getClass(), member));
}
@Override
public JsonElement deserialize(TypeToken<?> type, ConfigurationNode from) throws ObjectMappingException {
if (from.getValue() == null) {
return JsonNull.INSTANCE;
}
if (from.hasListChildren()) {
List<? extends ConfigurationNode> childrenList = from.getChildrenList();
JsonArray array = new JsonArray();
for (ConfigurationNode node : childrenList) {
array.add(node.getValue(TYPE));
}
return array;
}
if (from.hasMapChildren()) {
Map<Object, ? extends ConfigurationNode> childrenMap = from.getChildrenMap();
JsonObject object = new JsonObject();
for (Map.Entry<Object, ? extends ConfigurationNode> ent : childrenMap.entrySet()) {
object.add(ent.getKey().toString(), ent.getValue().getValue(TYPE));
}
return object;
}
Object val = from.getValue();
try {
return GsonConverters.IMMUTABLE.wrap(val);
} catch (IllegalArgumentException e) {
throw new ObjectMappingException(e);
}
}
@Test
public void getIdAsString_NullJson_Null() {
// given
JsonRpcRequest req = new JsonRpcRequest(JsonNull.INSTANCE, "something", null);
// when
String result = req.getIdAsString();
// then
assertThat(result).isNull();
}
private JsonElement nodeOrNullNode(JsonElement node) {
if (node == null) {
return JsonNull.INSTANCE;
} else {
return node;
}
}
public JsonElement read(JsonReader in) throws IOException {
switch (in.peek()) {
case NUMBER:
return new JsonPrimitive(new LazilyParsedNumber(in.nextString()));
case BOOLEAN:
return new JsonPrimitive(Boolean.valueOf(in.nextBoolean()));
case STRING:
return new JsonPrimitive(in.nextString());
case NULL:
in.nextNull();
return JsonNull.INSTANCE;
case BEGIN_ARRAY:
JsonElement array = new JsonArray();
in.beginArray();
while (in.hasNext()) {
array.add(read(in));
}
in.endArray();
return array;
case BEGIN_OBJECT:
JsonElement object = new JsonObject();
in.beginObject();
while (in.hasNext()) {
object.add(in.nextName(), read(in));
}
in.endObject();
return object;
default:
throw new IllegalArgumentException();
}
}
@SuppressWarnings("unchecked")
@Override
public T convert(ResponseBody value) throws IOException {
if (type.getRawType().equals(String.class)) {
return (T) value.string();
}
JsonElement jsonElement = null;
T result;
try {
// 解析错误时可获取原始JSON
jsonElement = gson.fromJson(value.charStream(), JsonElement.class);
if (jsonElement == null) {
jsonElement = JsonNull.INSTANCE;
}
result = adapter.fromJsonTree(jsonElement);
if (result == null) {
try {
result = adapter.fromJson("{}");
} catch (Exception ignored) {
}
}
} catch (JsonSyntaxException e) {
throw new JsonParserException(jsonElement, e.getMessage());
} finally {
value.close();
}
return result;
}
private static JsonElement parseJsonValue(Value value) {
if (value instanceof BooleanValue) {
return new JsonPrimitive(((BooleanValue) value).isValue());
}
if (value instanceof EnumValue) {
return new JsonPrimitive(((EnumValue) value).getName());
}
if (value instanceof FloatValue) {
return new JsonPrimitive(((FloatValue) value).getValue());
}
if (value instanceof IntValue) {
return new JsonPrimitive(((IntValue) value).getValue());
}
if (value instanceof NullValue) {
return JsonNull.INSTANCE;
}
if (value instanceof StringValue) {
return new JsonPrimitive(((StringValue) value).getValue());
}
if (value instanceof ArrayValue) {
List<Value> values = ((ArrayValue) value).getValues();
JsonArray jsonArray = new JsonArray(values.size());
values.forEach(v -> jsonArray.add(parseJsonValue(v)));
return jsonArray;
}
if (value instanceof ObjectValue) {
final JsonObject result = new JsonObject();
((ObjectValue) value).getObjectFields().forEach(objectField ->
result.add(objectField.getName(), parseJsonValue(objectField.getValue())));
return result;
}
//Should never happen, as it would mean the variable was not replaced by the parser
throw new CoercingParseLiteralException("Unknown scalar AST type: " + value.getClass().getName());
}
/**
* A utility method to handle null values and JsonNull values.
*/
String getAsString(String name) {
final JsonElement element = json.get(name);
return (element == null || element == JsonNull.INSTANCE) ? null : element.getAsString();
}
/**
* A utility method to handle null values and JsonNull values.
*/
boolean getAsBoolean(String name) {
final JsonElement element = json.get(name);
return (element == null || element == JsonNull.INSTANCE) ? false : element.getAsBoolean();
}
/**
* A utility method to handle null values and JsonNull values.
*/
boolean getAsBoolean(String name) {
final JsonElement element = json.get(name);
return (element == null || element == JsonNull.INSTANCE) ? false : element.getAsBoolean();
}
@Override
public JsonElement serialize(ZonedDateTime value, Type type, JsonSerializationContext context) {
return value == null ? JsonNull.INSTANCE : new JsonPrimitive(formatter.format(value));
}
@Override
public JsonElement createNull() {
return JsonNull.INSTANCE;
}
/**
* A utility method to handle null values and JsonNull values.
*/
String getAsString(String name) {
final JsonElement element = json.get(name);
return (element == null || element == JsonNull.INSTANCE) ? null : element.getAsString();
}
@Override
public JsonElement serialize(TimeZone value, Type type, JsonSerializationContext context) {
return value == null ? JsonNull.INSTANCE : new JsonPrimitive(value.getID());
}
@Override
public JsonElement toJson()
{
return JsonNull.INSTANCE;
}
/**
* Handle a JSON control request.
*
* The control action is executed directly
* using the calling thread.
*
* @return JSON response, JSON null if the request was not recognized.
*/
public JsonElement controlRequest(JsonObject request) throws Exception {
if (request.has(OP_KEY))
return controlOperation(request);
return JsonNull.INSTANCE;
}
/**
* Returns an instance of {@link JsonNull}.
*
* @return a json null instance
*/
public static JsonNull nullValue() {
return JsonNull.INSTANCE;
}