Class Json

java.lang.Object
jdk.incubator.json.Json

public final class Json extends Object
This class provides static methods for parsing and generating JSON texts.

parse(String) and parse(char[]) produce a JsonValue by parsing data adhering to the JSON syntax defined in RFC 8259.

JsonValue root = Json.parse(jsonText);
Successful parsing guarantees there are no syntax errors. Unsuccessful parsing throws a JsonParseException. Note that duplicate names in a JsonObject also result in this exception.

toDisplayString(JsonValue, String) produces a JSON text representation of the given JsonValue suitable for display.

Since:
28
External Specifications
  • Method Summary

    Modifier and Type
    Method
    Description
    static JsonValue
    parse(char[] in)
    Parses and creates a JsonValue from the given JSON text.
    static JsonValue
    Parses and creates a JsonValue from the given JSON text.
    static String
    Returns the String representation of the given JsonValue that conforms to the JSON syntax.

    Methods declared in class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    Modifier and Type
    Method
    Description
    protected Object
    Creates and returns a copy of this object.
    boolean
    Indicates whether some other object is "equal to" this one.
    protected void
    Deprecated, for removal: This API element is subject to removal in a future version.
    Finalization is deprecated and subject to removal in a future release.
    final Class<?>
    Returns the runtime class of this Object.
    int
    Returns a hash code value for this object.
    final void
    Wakes up a single thread that is waiting on this object's monitor.
    final void
    Wakes up all threads that are waiting on this object's monitor.
    Returns a string representation of the object.
    final void
    Causes the current thread to wait until it is awakened, typically by being notified or interrupted.
    final void
    wait(long timeoutMillis)
    Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed.
    final void
    wait(long timeoutMillis, int nanos)
    Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed.
  • Method Details

    • parse

      public static JsonValue parse(String in)
      Parses and creates a JsonValue from the given JSON text. If parsing succeeds, it guarantees that the input text conforms to the JSON syntax. If the text contains any JSON object that has duplicate names, a JsonParseException is thrown.

      JsonObjects preserve the order of members in the input JSON text.

      Implementation Note:
      JsonValues created by this method may produce their underlying value representation lazily.
      Parameters:
      in - the input JSON text as String. Non-null.
      Returns:
      the parsed JsonValue
      Throws:
      JsonParseException - if the input JSON text does not conform to the JSON text format or a JSON object containing duplicate names is encountered.
      NullPointerException - if in is null
    • parse

      public static JsonValue parse(char[] in)
      Parses and creates a JsonValue from the given JSON text. If parsing succeeds, it guarantees that the input text conforms to the JSON syntax. If the text contains any JSON object that has duplicate names, a JsonParseException is thrown. After parsing, changes to the input array have no effect on the returned JsonValue.

      JsonObjects preserve the order of their members declared in and parsed from the JSON text.

      Implementation Note:
      JsonValues created by this method may produce their underlying value representation lazily.
      Parameters:
      in - the input JSON text as char[]. Non-null.
      Returns:
      the parsed JsonValue
      Throws:
      JsonParseException - if the input JSON text does not conform to the JSON text format or a JSON object containing duplicate names is encountered.
      NullPointerException - if in is null
    • toDisplayString

      public static String toDisplayString(JsonValue value, String indent)
      Returns the String representation of the given JsonValue that conforms to the JSON syntax. As opposed to the compact output returned by JsonValue.toString(), this method returns JSON text that is better suited for display. The indent parameter specifies the indentation string used for each line and may contain only JSON insignificant whitespace characters: space (' '), horizontal tab ('\t'), line feed ('\n'), or carriage return ('\r').
      Parameters:
      value - the JsonValue to create the display string from. Non-null.
      indent - the String for the indentation. Non-null.
      Returns:
      the String representation of the given JsonValue that conforms to the JSON syntax
      Throws:
      IllegalArgumentException - if indent contains characters other than insignificant whitespace characters.
      NullPointerException - if value or indent is null
      See Also: