Class Json


  • public final class Json
    extends Object
    A utility class that can parse strings into ordinary java objects.

    JSON is parsed using the RFC 7159 specification.

    Since:
    1.0.0
    Author:
    Emil Forslund
    • Method Detail

      • toJson

        public static String toJson​(Object object)
        Parses the specified object to a JSON string. The following objects are supported:
        • Map<String, Object>
        • List<Object>
        • String
        • Double
        • Long
        • null

        This method will output a nicely formatted JSON string (with spaces and new-lines).

        Parameters:
        object - the object to parse
        Returns:
        the parsed string
        Throws:
        IllegalArgumentException - if the input object is a or contains unsupported types
      • toJson

        public static String toJson​(Object object,
                                    boolean pretty)
        Parses the specified object to JSON and prints it to the specified stream. The following objects are supported:
        • Map<String, Object>
        • List<Object>
        • String
        • Double
        • Long
        • null

        This method will output a nicely formatted JSON string (with spaces and new-lines).

        Parameters:
        object - the object to parse
        pretty - if the result should be formatted in a pretty way
        Returns:
        JSON String
        Throws:
        IllegalArgumentException - if the input object is of or contains unsupported types
      • toJson

        public static void toJson​(Object object,
                                  OutputStream out)
                           throws IOException
        Parses the specified object to JSON and prints it to the specified stream. The following objects are supported:
        • Map<String, Object>
        • List<Object>
        • String
        • Double
        • Long
        • null

        This method will output a nicely formatted JSON string (with spaces and new-lines).

        Parameters:
        object - the object to parse
        out - the stream to write the result to
        Throws:
        IllegalArgumentException - if the inputed object is of or contains unsupported types
        IOException - if the stream could not be written to
      • toJson

        public static void toJson​(Object object,
                                  OutputStream out,
                                  boolean pretty)
                           throws IOException
        Parses the specified object to JSON and prints it to the specified stream. The following objects are supported:
        • Map<String, Object>
        • List<Object>
        • String
        • Double
        • Long
        • null

        This method will output a nicely formatted JSON string (with spaces and new-lines).

        Parameters:
        object - the object to parse
        out - the stream to write the result to
        pretty - if the result should be formatted in a pretty way
        Throws:
        IllegalArgumentException - if the input object is of or contains unsupported types
        IOException - if the stream could not be written to
      • fromJson

        public static Object fromJson​(String json)
        Parses the specified JSON string into a java object. The following objects are supported:
        • Map<String, Object>
        • List<Object>
        • String
        • Double
        • Long
        • null
        Parameters:
        json - the json to parse
        Returns:
        the created object
        Throws:
        JsonSyntaxException - if there is a syntax error
      • fromJson

        public static Object fromJson​(InputStream in)
                               throws IOException
        Parses the specified JSON stream of unicode characters into a java object. The following objects are supported:
        • Map<String, Object>
        • List<Object>
        • String
        • Double
        • Long
        • null
        Parameters:
        in - the json to parse
        Returns:
        the created object
        Throws:
        IOException - if the stream can not be read
        JsonSyntaxException - if there is a syntax error