/** * Returns a JSON literal, which represents {@code null}. * * @return the literal. */ static JsonValue nullLiteral() { return JsonFactory.nullLiteral(); }
/** * Returns a new JSON field based on the specified key value pair and definition. * * @param key the key of the field to be created. * @param value the value of the field to be created. {@code null} will be converted to the JSON NULL Literal. * @param definition the definition of the field to be created. * @return a new JSON field containing the specified key value pair and definition. * @throws NullPointerException if {@code key} is {@code null}. */ public static JsonField newField(final JsonKey key, @Nullable final JsonValue value, @Nullable final JsonFieldDefinition definition) { return ImmutableJsonField.newInstance(key, null != value ? value : nullLiteral(), definition); }
@Override protected String createStringRepresentation() { return JsonFactory.nullLiteral().toString(); }
@Override public String toString() { return JsonFactory.nullLiteral().toString(); }
@Override protected String createStringRepresentation() { return JsonFactory.nullLiteral().toString(); }
@Override public String toString() { return JsonFactory.nullLiteral().toString(); }
/** * Returns a JsonValue that represents the given Java string as JSON string. For example the Java string * {@code "foo"} would be {@code "\"foo\""} as JSON string. * * @param jsonString the string to get a JSON representation for. * @return a JSON value that represents the given string. If {@code jsonString} is {@code null}, a "null" object is * returned. * @throws IllegalArgumentException if {@code jsonString} is empty. * @see #nullLiteral() */ public static JsonValue newValue(@Nullable final String jsonString) { final JsonValue result; if (null != jsonString) { result = ImmutableJsonString.of(jsonString); } else { result = nullLiteral(); } return result; }
/** * Tries to guess the associated JsonValue for the specified object. * * @param value the value to be converted. * @param <T> the type of {@code value}. * @return a JsonValue representation of {@code value}. * @throws JsonParseException if {@code value} is not defined for JSON. */ static <T> JsonValue getAppropriateValue(@Nullable final T value) { final JsonValue result; if (null == value) { result = nullLiteral(); } else if (value instanceof JsonValue) { result = ((JsonValue) value); } else if (value instanceof String || value instanceof CharSequence) { result = newValue(String.valueOf(value)); } else { result = convert(tryToRead(String.valueOf(value))); } return result; }
private static JsonValue mapJavaObjectToJsonValue(@Nullable final Object object, final Function<String, String> jsonKeyNameReviser) { final JsonValue result; if (null == object) { result = JsonFactory.nullLiteral(); } else if (object instanceof String) { result = JsonFactory.newValue((String) object); } else if (object instanceof Number) { result = mapJavaNumberToJsonNumber((Number) object); } else if (object instanceof BasicDBObject) { result = mapBasicDBObjectToJsonObject((BasicDBObject) object, jsonKeyNameReviser); } else if (object instanceof BasicDBList) { result = mapBasicDBListToJsonArray((BasicDBList) object, jsonKeyNameReviser); } else if (object instanceof Boolean) { result = JsonFactory.newValue((Boolean) object); } else { result = JsonFactory.nullLiteral(); } return result; }
/** * Tries to guess the associated JsonValue for the specified object. * * @param value the value to be converted. * @param <T> the type of {@code value}. * @return a JsonValue representation of {@code value}. * @throws JsonParseException if {@code value} is not defined for JSON. */ static <T> JsonValue getAppropriateValue(@Nullable final T value) { final JsonValue result; if (null == value) { result = nullLiteral(); } else if (value instanceof JsonValue) { result = (JsonValue) value; } else if (value instanceof Boolean) { result = newValue((Boolean) value); } else if (value instanceof Integer) { result = newValue((Integer) value); } else if (value instanceof Long) { result = newValue((Long) value); } else if (value instanceof Double) { result = newValue((Double) value); } else if (value instanceof CharSequence) { result = newValue(String.valueOf(value)); } else { result = JsonValueParser.fromString().apply(String.valueOf(value)); } return result; }
private static JsonValue toJson(final Object input) { return JsonFactory.nullLiteral(); } else if (input instanceof Boolean) { return JsonFactory.newValue((boolean) input);
private static JsonValue toJson(final Object input) { return JsonFactory.nullLiteral(); } else if (input instanceof Boolean) { return JsonFactory.newValue((boolean) input);
private static JsonValue mapBsonValueToJsonValue(@Nullable final BsonValue bsonValue, final Function<String, String> jsonKeyNameReviser) { final JsonValue result; if (bsonValue == null || bsonValue.isNull()) { result = JsonFactory.nullLiteral(); } else if (bsonValue.isString()) { result = JsonFactory.newValue(bsonValue.asString().getValue()); } else if (bsonValue.isNumber()) { result = mapBsonNumberToJsonNumber(bsonValue.asNumber()); } else if (bsonValue.isDocument()) { result = mapBsonDocumentToJsonObject(bsonValue.asDocument(), jsonKeyNameReviser); } else if (bsonValue.isArray()) { result = mapBsonArrayToJsonArray(bsonValue.asArray(), jsonKeyNameReviser); } else if (bsonValue.isBoolean()) { result = JsonFactory.newValue(bsonValue.asBoolean().getValue()); } else if (bsonValue.isTimestamp()) { final Instant instant = Instant.ofEpochSecond(bsonValue.asTimestamp().getTime()); result = JsonFactory.newValue(instant.toString()); } else { result = JsonFactory.nullLiteral(); } return result; }