Class JSONText


  • public class JSONText
    extends Object
    Represents one JSON text.

    Instances of this class is not immutable, but thread-safe.

    • Method Detail

      • get

        @NotNull
        public @NotNull String get()
        Returns the content of this JSON text as a string.
        Returns:
        Contents of this JSON text.
      • source

        @NotNull
        public @NotNull Object source()
        Return the object which represents the source of this JSON text.
        Returns:
        An instance of File, URL or String.
      • name

        @NotNull
        public @NotNull String name()
        Return a short name of the source of this JSON text. This is useful to construct an error message of JSON parsers.
        Returns:
        name of this JSON text.
      • uri

        @NotNull
        public @NotNull URI uri()
        Return a URI of the source of this JSON text.
      • isParsed

        public boolean isParsed()
        Returns whether this JSON text has been successfully parsed in the past.

        If this method returns false, evaluate(String) and its variants result in IllegalStateException;

        Returns:
        whether this JSON text has been successfully parsed in the past.
      • evaluate

        @NotNull
        public @NotNull JSONValue evaluate​(@NotNull
                                           @NotNull String pointer)
                                    throws InvalidJSONPointerException
        Evaluate the given JSON Pointer and return the found JSON value.

        parse() (or its variant) must be successfully called before this method.

        Note that this method results in InvalidJSONPointerException if this JSON text does not have any JSON value.

        Parameters:
        pointer - a string representation of a JSON Pointer
        Returns:
        the JSON value of the pointer evaluation result
        Throws:
        InvalidJSONPointerException - when the JSON Pointer has an error
        IllegalStateException - if this JSON text has not been parsed yet
      • evaluate

        @NotNull
        public @NotNull JSONValue evaluate​(@NotNull
                                           @NotNull String pointer,
                                           boolean fragment)
                                    throws InvalidJSONPointerException
        Evaluate the given JSON Pointer and return the found JSON value.

        parse() (or its variant) must be successfully called before this method.

        Note that this method results in InvalidJSONPointerException if this JSON text does not have any JSON value.

        Parameters:
        pointer - a string representation of a JSON Pointer
        fragment - specify true to handle the given string as a URI fragment identifier starting with '#".
        Returns:
        the JSON value of the pointer evaluation result
        Throws:
        InvalidJSONPointerException - when the JSON Pointer has an error
        IllegalStateException - if this JSON text has not been parsed yet
      • fromFile

        @NotNull
        public static @NotNull JSONText fromFile​(@NotNull
                                                 @NotNull File file)
                                          throws IOException
        Reads JSON text from a file and returns an instance of JSONText for further processing. The file must be encoded using UTF-8.
        Parameters:
        file - JSON text file.
        Returns:
        An instance of JSON text.
        Throws:
        IOException - in case of an I/O error
        See Also:
        RFC 8259 - 8.1. Character Encoding
      • fromString

        @NotNull
        public static @NotNull JSONText fromString​(@NotNull
                                                   @NotNull String str,
                                                   @Nullable
                                                   @Nullable String name)
        Convert the given String to an instance of JSONText.

        Specify the name, then name() returns the specified string. The specified name will be used, for example, for the parser to show an error message . With a meaningful name, the users of your application will be able to know which JSON text has a syntax error.

        If the name is not specified (= null is given), the name is automatically determined by this library, but the users may not understand where does that the JSON text come from, so it is not recommended.

        Parameters:
        str - A string which contains JSON text.
        name - A name of this source string.
        Returns:
        An instance of JSON text.
        See Also:
        RFC 8259 - 8.1. Character Encoding