/** * Returns a new {@code ImmutableJsonObject} instance which contains the given fields. * * @param fields the fields of the new JSON object. * @return a new JSON object containing the {@code fields}. * @throws NullPointerException if {@code fields} is {@code null}. */ public static ImmutableJsonObject of(final Map<String, JsonField> fields) { return of(fields, null); }
@Override public JsonObject build() { return ImmutableJsonObject.of(fields); }
@Override public JsonObject build() { return ImmutableJsonObject.of(fields); }
/** * 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); }
@Override public void endObject(final List<JsonField> jsonFields) { final StringBuilder stringBuilder = stringBuilders.poll(); if (null != stringBuilder) { if (!jsonFields.isEmpty()) { stringBuilder.deleteCharAt(stringBuilder.length() - 1); } stringBuilder.append('}'); valueString = stringBuilder.toString(); } final Map<String, JsonField> fieldMap = new LinkedHashMap<>(jsonFields.size()); for (final JsonField jsonField : jsonFields) { fieldMap.put(jsonField.getKeyName(), jsonField); } jsonValue = ImmutableJsonObject.of(fieldMap, valueString); }
/** * Creates a JSON object from the given string. * * @param jsonString the string that represents the JSON object. * @return the JSON object that has been created from the string. * @throws NullPointerException if {@code jsonString} is {@code null}. * @throws IllegalArgumentException if {@code jsonString} is empty. * @throws JsonParseException if {@code jsonString} does not contain a valid JSON object. */ public static JsonObject newObject(final String jsonString) { requireNonNull(jsonString, "The JSON string to create a JSON object from must not be null!"); if (jsonString.isEmpty()) { throw new IllegalArgumentException("The JSON string to create a JSON object from must not be empty!"); } if (Objects.equals(Json.NULL.toString(), jsonString)) { return NULL_JSON_OBJECT; } else { final com.eclipsesource.json.JsonObject jsonObject = tryToReadJsonObjectFrom(jsonString); return ImmutableJsonObject.of(toMap(jsonObject)); } }
} else if (minimalJsonValue.isObject()) { final Map<String, JsonField> jsonFields = toMap(minimalJsonValue.asObject()); result = ImmutableJsonObject.of(jsonFields); } else if (minimalJsonValue.isString()) { result = ImmutableJsonString.of(minimalJsonValue);