/** * Returns a new JSON field based on the specified key value pair. * * @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. * @return a new JSON field containing the specified key value pair. * @throws NullPointerException if {@code key} is null; */ public static JsonField newField(final JsonKey key, @Nullable final JsonValue value) { return newField(key, value, null); }
/** * Returns a new JSON field based on the specified key value pair. * * @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. * @return a new JSON field containing the specified key value pair. * @throws NullPointerException if {@code key} is null. */ static JsonField newInstance(final CharSequence key, @Nullable final JsonValue value) { return JsonFactory.newField(JsonFactory.newKey(key), value); }
/** * 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}. */ static JsonField newInstance(final CharSequence key, @Nullable final JsonValue value, @Nullable final JsonFieldDefinition definition) { return JsonFactory.newField(JsonFactory.newKey(key), value, definition); }
/** * Creates a JSON object from the given key-value pairs aka fields. * * @param fields the fields of the JSON object to be created. * @return a new JSON object based on the provided fields. * @throws NullPointerException if {@code fields} is {@code null}. */ public static JsonObject newObject(final Map<JsonKey, JsonValue> fields) { final Map<String, JsonField> jsonFields = new LinkedHashMap<>(fields.size()); fields.forEach((jsonKey, jsonValue) -> jsonFields.put(jsonKey.toString(), newField(jsonKey, jsonValue))); return ImmutableJsonObject.of(jsonFields); }
/** * Creates a JSON object from the given key-value pairs aka fields. * * @param fields the fields of the JSON object to be created. * @return a new JSON object based on the provided fields. * @throws NullPointerException if {@code fields} is {@code null}. */ public static JsonObject newObject(final Map<JsonKey, JsonValue> fields) { final Map<String, JsonField> jsonFields = new LinkedHashMap<>(fields.size()); fields.forEach(((jsonKey, jsonValue) -> jsonFields.put(jsonKey.toString(), newField(jsonKey, jsonValue)))); return ImmutableJsonObject.of(jsonFields); }
/** * Returns a new JSON field based on the specified key value pair. * * @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. * @return a new JSON field containing the specified key value pair. * @throws NullPointerException if {@code key} is null. */ static JsonField newInstance(final CharSequence key, @Nullable final JsonValue value) { return JsonFactory.newField(JsonFactory.newKey(key), value); }
/** * 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}. */ static JsonField newInstance(final CharSequence key, @Nullable final JsonValue value, @Nullable final JsonFieldDefinition definition) { return JsonFactory.newField(JsonFactory.newKey(key), value, definition); }
@Override public JsonObject toJson(final JsonSchemaVersion schemaVersion, final Predicate<JsonField> predicate) { final List<JsonField> fields = mapping.entrySet().stream().map(e -> JsonFactory.newField(JsonFactory.newKey(e.getKey()), JsonFactory.newValue(e.getValue()))).collect(Collectors.toList()); return JsonFactory.newObjectBuilder(fields).build(); }
@Override public JsonObject toJson(final JsonSchemaVersion schemaVersion, final Predicate<JsonField> predicate) { final List<JsonField> fields = mapping.entrySet().stream().map(e -> JsonFactory.newField(JsonFactory.newKey(e.getKey()), JsonFactory.newValue(e.getValue()))).collect(Collectors.toList()); return JsonFactory.newObjectBuilder(fields).build(); }
/** * Convert an indexed address metric into a JSON field. * * @param key Key of the address metric; may be empty. * @param metric the address metric to convert. * @return a valid JSON field for the address metric. */ static JsonField toJsonField(final String key, final AddressMetric metric) { final String nonemptyKey = key.isEmpty() ? "<empty>" : key; return JsonFactory.newField(JsonKey.of(nonemptyKey), metric.toJson()); } }
/** * Convert an indexed address metric into a JSON field. * * @param key Key of the address metric; may be empty. * @param metric the address metric to convert. * @return a valid JSON field for the address metric. */ static JsonField toJsonField(final String key, final AddressMetric metric) { final String nonemptyKey = key.isEmpty() ? "<empty>" : key; return JsonFactory.newField(JsonKey.of(nonemptyKey), metric.toJson()); } }
@Override public <T> JsonObject set(final JsonFieldDefinition<T> fieldDefinition, @Nullable final T value) { requireNonNull(fieldDefinition, "The JSON field definition to set the value for must not be null!"); final JsonPointer pointer = fieldDefinition.getPointer(); final JsonKey leafKey = pointer.getLeaf().orElseThrow(() -> { final String msgTemplate = "The pointer of the field definition <{0}> must not be empty!"; return new IllegalArgumentException(MessageFormat.format(msgTemplate, fieldDefinition)); }); final JsonField field = JsonFactory.newField(leafKey, JsonFactory.getAppropriateValue(value), fieldDefinition); return setFieldInHierarchy(this, pointer, field); }
static JsonObject mapBasicDBObjectToJsonObject(final BasicDBObject basicDBObject, final Function<String, String> jsonKeyNameReviser) { return basicDBObject.entrySet() .stream() .map(e -> JsonFactory.newField(reviseKeyName(e.getKey(), jsonKeyNameReviser), mapJavaObjectToJsonValue(e.getValue(), jsonKeyNameReviser))) .collect(JsonCollectors.fieldsToObject()); }
static JsonObject mapBsonDocumentToJsonObject(final BsonDocument bsonDocument, final Function<String, String> jsonKeyNameReviser) { return bsonDocument.entrySet() .stream() .map(e -> JsonFactory.newField(reviseKeyName(e.getKey(), jsonKeyNameReviser), mapBsonValueToJsonValue(e.getValue(), jsonKeyNameReviser))) .collect(JsonCollectors.fieldsToObject()); }
@Override public <T> JsonObjectBuilder set(final JsonFieldDefinition<T> fieldDefinition, @Nullable final T value, final Predicate<JsonField> predicate) { requireNonNull(fieldDefinition, "The definition of the JSON field to set the value for must not be null!"); checkPredicate(predicate); final JsonPointer pointer = fieldDefinition.getPointer(); pointer.getLeaf() .map(leafKey -> JsonFactory.newField(leafKey, JsonFactory.getAppropriateValue(value), fieldDefinition)) .filter(predicate) .ifPresent(jsonField -> setFieldInHierarchy(this, pointer, jsonField)); return this; }
@Override public <T> JsonObjectBuilder set(final JsonFieldDefinition<T> fieldDefinition, @Nullable final T value, final Predicate<JsonField> predicate) { requireNonNull(fieldDefinition, "The definition of the JSON field to set the value for must not be null!"); checkPredicate(predicate); final JsonPointer pointer = fieldDefinition.getPointer(); pointer.getLeaf() .map(leafKey -> JsonFactory.newField(leafKey, JsonFactory.getAppropriateValue(value), fieldDefinition)) .filter(predicate) .ifPresent(jsonField -> setFieldInHierarchy(this, pointer, jsonField)); return this; }
@Override public ImmutableJsonObjectBuilder set(final CharSequence key, final JsonValue value, final Predicate<JsonField> predicate) { final JsonPointer pointer = JsonFactory.getNonEmptyPointer(key); checkValue(value); checkPredicate(predicate); pointer.getLeaf() .map(leafKey -> JsonFactory.newField(leafKey, value)) .filter(predicate) .ifPresent(jsonField -> setFieldInHierarchy(this, pointer, jsonField)); return this; }
@Override public JsonObject setValue(final CharSequence key, final JsonValue value) { final JsonPointer pointer = JsonFactory.getNonEmptyPointer(key); final JsonKey leafKey = pointer.getLeaf().orElse(ROOT_KEY); final Optional<JsonFieldDefinition> keyDefinition = getDefinitionForKey(leafKey); return setFieldInHierarchy(this, pointer, JsonFactory.newField(leafKey, value, keyDefinition.orElse(null))); }
private static Map<String, JsonField> toMap(final com.eclipsesource.json.JsonObject minimalJsonObject) { final Map<String, JsonField> result = new LinkedHashMap<>(minimalJsonObject.size()); for (final com.eclipsesource.json.JsonObject.Member member : minimalJsonObject) { final JsonKey key = newKey(member.getName()); final JsonValue value = convert(member.getValue()); result.put(key.toString(), newField(key, value)); } return result; }
@Override public ImmutableJsonObjectBuilder set(final CharSequence key, final JsonValue value, final Predicate<JsonField> predicate) { final JsonPointer pointer = JsonFactory.getNonEmptyPointer(key); checkValue(value); checkPredicate(predicate); pointer.getLeaf() .map(leafKey -> JsonFactory.newField(leafKey, value)) .filter(predicate) .ifPresent(jsonField -> setFieldInHierarchy(this, pointer, jsonField)); return this; }