/** * Reads the entire input stream from the specified reader and parses it as JSON value. The input stream is expected * to contain a valid JSON value with optional whitespace padding. * <p> * As characters are read in chunks and buffered internally it does <em>not</em> improve reading performance to wrap * an existing reader in a {@code BufferedReader}. * </p> * * @param reader the reader to read the JSON value from. * @return a JSON value which represents the read stream. * @throws NullPointerException if {@code reader} is {@code null}. * @throws JsonParseException if an I/O error occurred or if the input is no valid JSON. */ public static JsonValue readFrom(final Reader reader) { return JsonValueParser.fromReader().apply(reader); }
/** * 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); }
/** * Returns a parser which accepts a String and which uses the given handler for object creation. * The parsed JsonValue can be obtained from the given handler finally. * * @param jsonHandler receives parser events in order to create a {@link JsonValue}. * @param <A> the type to be used for parsing JSON arrays. * @param <O> the type to be used for parsing JSON objects. * @param <V> the type of the value this handler returns. * @return the parse Function. * @throws NullPointerException if {@code jsonHandler} is {@code null}. * @throws JsonParseException if the string cannot be parsed. * @see DittoJsonHandler#getValue() */ public static <A, O, V> Consumer<String> fromString(final DittoJsonHandler<A, O, V> jsonHandler) { return jsonString -> tryToParseJsonValue(jsonString, jsonHandler); }
private static <T> T tryToParseJsonValue(final String jsonString, final DittoJsonHandler<?, ?, T> dittoJsonHandler) { try { return parseJsonValue(jsonString, dittoJsonHandler); } catch (final ParseException | UnsupportedOperationException | StackOverflowError e) { throw JsonParseException.newBuilder() .message(MessageFormat.format("Failed to parse JSON string ''{0}''!", jsonString)) .cause(e) .build(); } }
private static JsonValue tryToReadJsonValueFrom(final Reader reader) { try { return readJsonValueFrom(reader); } catch (final ParseException | IOException | StackOverflowError e) { throw JsonParseException.newBuilder() .message("Failed to parse JSON value from reader!") .cause(e) .build(); } }
private static List<JsonValue> parseToList(final String jsonArrayString) { final ValueListJsonHandler jsonHandler = new ValueListJsonHandler(); JsonValueParser.fromString(jsonHandler).accept(jsonArrayString); return jsonHandler.getValue(); }
/** * Returns a Function for parsing a String to an instance of {@link JsonValue}. * * @return the function. */ public static Function<String, JsonValue> fromString() { Function<String, JsonValue> result = fromStringInstance; if (null == result) { result = jsonString -> tryToParseJsonValue(jsonString, DefaultDittoJsonHandler.newInstance()); fromStringInstance = result; } return result; }
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; }