/** * Tries to guess the appropriate JsonValue for the given Java value. * * @param value the Java value to be converted to its JsonValue counterpart. * @param <T> the Java type to be converted. * @return the appropriate JsonValue. * @throws JsonParseException if {@code value} cannot be converted to a valid JSON value. */ static <T> JsonValue of(@Nullable final T value) { return JsonFactory.getAppropriateValue(value); }
@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); }
@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; }
/** * Returns an instance of {@code JsonArray} which contains the given values. * This method tries to determine the appropriate {@link JsonValue}-counterpart for each * given value. * * @param value the mandatory value of the returned JsonArray. * @param furtherValues further optional values of the returned JsonArray. * @return the JsonArray. * @throws NullPointerException if {@code furtherValues} is {@code null}. * @throws JsonParseException if either {@code value} or any item of {@code furtherValues} * cannot be converted to {@code JsonValue}. */ @SuppressWarnings("unchecked") static <T> JsonArray of(@Nullable final T value, final T... furtherValues) { final JsonArrayBuilder arrayBuilder = newBuilder(); arrayBuilder.add(JsonFactory.getAppropriateValue(value)); for (final T furtherValue : furtherValues) { arrayBuilder.add(JsonFactory.getAppropriateValue(furtherValue)); } return arrayBuilder.build(); }