Class Json


  • public final class Json
    extends Object

    Convenience methods for serializing and deserializing objects to and from json and creating json reader and writers.

    Author:
    David B. Bracewell
    • Field Detail

      • MAPPER

        public static final com.fasterxml.jackson.databind.ObjectMapper MAPPER
    • Method Detail

      • dump

        public static Resource dump​(Object object,
                                    @NonNull
                                    @NonNull Resource resource)
                             throws IOException
        Dumps the given object to the given output location in json format.
        Parameters:
        object - the object to dump
        resource - the resource to write the dumped object in json format to.
        Returns:
        the resource
        Throws:
        IOException - Something went wrong writing to the given resource
      • dumpPretty

        public static Resource dumpPretty​(Object object,
                                          @NonNull
                                          @NonNull Resource resource)
                                   throws IOException
        Dumps the given object to the given output location in json format.
        Parameters:
        object - the object to dump
        resource - the resource to write the dumped object in json format to.
        Returns:
        the resource
        Throws:
        IOException - Something went wrong writing to the given resource
      • dumps

        public static String dumps​(Object object)
        Dumps the given object to a string in json format.
        Parameters:
        object - the object to dump
        Returns:
        the object as a json string
      • dumpsPretty

        public static String dumpsPretty​(Object object)
        Dumps the given object to a string in json format.
        Parameters:
        object - the object to dump
        Returns:
        the object as a json string
      • parse

        public static JsonEntry parse​(String json)
                               throws IOException
        Parses the json string.
        Parameters:
        json - the json string to parse
        Returns:
        the parsed string as a json entry
        Throws:
        IOException - something went wrong parsing the json.
      • parse

        public static <T> T parse​(@NonNull
                                  @NonNull String json,
                                  @NonNull
                                  @NonNull Type type)
                           throws IOException
        Parses the json in the given resource creating an object of the given class type.
        Type Parameters:
        T - the class type parameter
        Parameters:
        json - the json to parse
        type - the class information for the object to deserialized
        Returns:
        the deserialized object
        Throws:
        IOException - something went wrong reading the json
      • parse

        public static JsonEntry parse​(@NonNull
                                      @NonNull Resource json)
                               throws IOException
        Parses the given resource as json entry
        Parameters:
        json - the resource to read from
        Returns:
        the parsed resource as a json entry
        Throws:
        IOException - Something went wrong parsing the resource
      • parse

        public static <T> T parse​(@NonNull
                                  @NonNull Resource resource,
                                  @NonNull
                                  @NonNull Type type)
                           throws IOException
        Parses the json in the given resource creating an object of the given class type.
        Type Parameters:
        T - the class type parameter
        Parameters:
        resource - the resource to read from
        type - the class information for the object to deserialized
        Returns:
        the deserialized object
        Throws:
        IOException - something went wrong reading the json
      • parseArray

        public static List<JsonEntry> parseArray​(@NonNull
                                                 @NonNull Resource resource)
                                          throws IOException
        Loads an array of objects from the given resource in json format.
        Parameters:
        resource - the resource to read from
        Returns:
        the list of objects read in from the resource
        Throws:
        IOException - Something went wrong reading from the resource
      • parseArray

        public static List<JsonEntry> parseArray​(@NonNull
                                                 @NonNull String json)
                                          throws IOException
        Parses a json string into a list of JsonEntry
        Parameters:
        json - the json string to load the array from
        Returns:
        the list of objects parsed from the string
        Throws:
        IOException - Something went wrong parsing the json string
      • parseObject

        public static Map<String,​JsonEntry> parseObject​(Resource json)
                                                       throws IOException
        Quicker method for parsing a json string into a Map of String keys and Object values.
        Parameters:
        json - the json to load from
        Returns:
        the map of representing the json object in the string
        Throws:
        IOException - Something went wrong parsing the json in the resource
      • parseObject

        public static Map<String,​JsonEntry> parseObject​(String json)
                                                       throws IOException
        Quicker method for parsing a json string into a Map of String keys and Object values.
        Parameters:
        json - the json to load from
        Returns:
        the map of representing the json object in the string
        Throws:
        IOException - Something went wrong parsing the json string
      • typeToJavaType

        public static com.fasterxml.jackson.databind.JavaType typeToJavaType​(Type type)