/** * Reads the given string and creates a JSON value based on the read data. The actual type of this JSON value is * unknown but can be obtained by invoking the {@code is...} methods. * * @param jsonString the JSON document to read. * @return a JSON value representing the read document. This value can be a JSON literal, a JSON object and so on. * @throws NullPointerException if {@code jsonString} is empty. * @throws JsonParseException if {@code jsonString} is empty or if it is no valid JSON. */ public static JsonValue readFrom(final String jsonString) { requireNonNull(jsonString, "The JSON string to read from must not be null!"); if (jsonString.isEmpty()) { throw new JsonParseException("The JSON string to read from must not be empty!"); } return JsonValueParser.fromString().apply(jsonString); }
private static List<JsonValue> parseToList(final String jsonArrayString) { final ValueListJsonHandler jsonHandler = new ValueListJsonHandler(); JsonValueParser.fromString(jsonHandler).accept(jsonArrayString); return jsonHandler.getValue(); }
private static Map<String, JsonField> parseToMap(final String jsonObjectString) { final FieldMapJsonHandler jsonHandler = new FieldMapJsonHandler(); JsonValueParser.fromString(jsonHandler).accept(jsonObjectString); return jsonHandler.getValue(); }
/** * 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 (isJsonNullLiteralString(jsonString)) { return nullObject(); } else { final JsonValue jsonValue = JsonValueParser.fromString().apply(jsonString); if (!jsonValue.isObject()) { final String msgPattern = "<{0}> is not a valid JSON object!"; throw JsonParseException.newBuilder().message(MessageFormat.format(msgPattern, jsonString)).build(); } return jsonValue.asObject(); } }
/** * Creates a new JSON array from the given string. * * @param jsonString the string that represents the JSON array. * @return the JSON array 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 array. */ public static JsonArray newArray(final String jsonString) { requireNonNull(jsonString, "The JSON string to create JSON array from must not be null!"); if (jsonString.isEmpty()) { throw new IllegalArgumentException("The JSON string to create a JSON array from must not be empty!"); } if (isJsonNullLiteralString(jsonString)) { return nullArray(); } else { final JsonValue jsonValue = JsonValueParser.fromString().apply(jsonString); if (!jsonValue.isArray()) { final String msgPattern = "<{0}> is not a valid JSON array!"; throw JsonParseException.newBuilder().message(MessageFormat.format(msgPattern, jsonString)).build(); } return jsonValue.asArray(); } }
/** * 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; }