Java Code Examples for com.google.gson.JsonNull#INSTANCE
The following examples show how to use
com.google.gson.JsonNull#INSTANCE .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FormattedMessageWriter.java From arcusplatform with Apache License 2.0 | 6 votes |
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; } }
Example 2
Source File: Util.java From Migrate2Postgres with GNU General Public License v3.0 | 5 votes |
/** * 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; }
Example 3
Source File: AbstractJsonableObject.java From nomulus with Apache License 2.0 | 5 votes |
/** 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)); }
Example 4
Source File: GsonScalars.java From graphql-spqr with Apache License 2.0 | 5 votes |
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()); }
Example 5
Source File: GsonTypeSerializer.java From helper with MIT License | 5 votes |
@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); } }
Example 6
Source File: CustomGsonConverterFactory.java From JianshuApp with GNU General Public License v3.0 | 5 votes |
@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; }
Example 7
Source File: JsonRpcRequestTest.java From ja-micro with Apache License 2.0 | 5 votes |
@Test public void getIdAsString_NullJson_Null() { // given JsonRpcRequest req = new JsonRpcRequest(JsonNull.INSTANCE, "something", null); // when String result = req.getIdAsString(); // then assertThat(result).isNull(); }
Example 8
Source File: AbstractFeatureSerializer.java From quaerite with Apache License 2.0 | 5 votes |
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; } }
Example 9
Source File: GsonRuntime.java From jmespath-java with BSD 3-Clause "New" or "Revised" License | 5 votes |
private JsonElement nodeOrNullNode(JsonElement node) { if (node == null) { return JsonNull.INSTANCE; } else { return node; } }
Example 10
Source File: TypeAdapters.java From letv with Apache License 2.0 | 5 votes |
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(); } }
Example 11
Source File: Element.java From flutter-intellij with BSD 3-Clause "New" or "Revised" License | 4 votes |
/** * 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(); }
Example 12
Source File: AbstractScriptValue.java From purplejs with Apache License 2.0 | 4 votes |
@Override public JsonElement toJson() { return JsonNull.INSTANCE; }
Example 13
Source File: Json.java From morpheus-core with Apache License 2.0 | 4 votes |
@Override public JsonElement serialize(TimeZone value, Type type, JsonSerializationContext context) { return value == null ? JsonNull.INSTANCE : new JsonPrimitive(value.getID()); }
Example 14
Source File: Element.java From flutter-intellij with BSD 3-Clause "New" or "Revised" License | 4 votes |
/** * 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(); }
Example 15
Source File: GsonRuntime.java From jmespath-java with BSD 3-Clause "New" or "Revised" License | 4 votes |
@Override public JsonElement createNull() { return JsonNull.INSTANCE; }
Example 16
Source File: Json.java From morpheus-core with Apache License 2.0 | 4 votes |
@Override public JsonElement serialize(ZonedDateTime value, Type type, JsonSerializationContext context) { return value == null ? JsonNull.INSTANCE : new JsonPrimitive(formatter.format(value)); }
Example 17
Source File: Element.java From flutter-intellij with BSD 3-Clause "New" or "Revised" License | 4 votes |
/** * 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(); }
Example 18
Source File: Element.java From flutter-intellij with BSD 3-Clause "New" or "Revised" License | 4 votes |
/** * 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(); }
Example 19
Source File: JsonControlService.java From quarks with Apache License 2.0 | 3 votes |
/** * 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; }
Example 20
Source File: JsonBuilder.java From helper with MIT License | 2 votes |
/** * Returns an instance of {@link JsonNull}. * * @return a json null instance */ public static JsonNull nullValue() { return JsonNull.INSTANCE; }