Class JsonUtil


  • public final class JsonUtil
    extends java.lang.Object
    Util class to parse JSON formatted input to various object types or convert objects to JSON strings.

    We use the lightweight JSON library `jackson-jr` to parse the given input or to convert the given objects to JSON string. If `jackson-annotations` library present on the classpath, we register JacksonAnnotationExtension to so that the JSON conversion can make use of Jackson Annotations.

    Since:
    Jet 4.2
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.Object anyFrom​(java.lang.String jsonString)
      Converts a JSON string to an Object.
      static <T> T beanFrom​(java.lang.String jsonString, java.lang.Class<T> type)
      Converts a JSON string to an object of the given type.
      static <T> java.util.Iterator<T> beanSequenceFrom​(java.io.Reader reader, java.lang.Class<T> type)
      Returns an Iterator over the sequence of JSON objects parsed from given reader.
      static <T> java.util.stream.Stream<T> beanSequenceFrom​(java.nio.file.Path path, java.lang.Class<T> type)
      Parses the file and returns a stream of objects with the given type.
      static HazelcastJsonValue hazelcastJsonValue​(java.lang.Object object)
      Creates a HazelcastJsonValue by converting given the object to string using Object.toString().
      static java.util.List<java.lang.Object> listFrom​(java.lang.String jsonString)
      Converts a JSON string to a List.
      static <T> java.util.List<T> listFrom​(java.lang.String jsonString, java.lang.Class<T> type)
      Converts a JSON string to a List of given type.
      static java.util.Map<java.lang.String,​java.lang.Object> mapFrom​(java.lang.Object object)
      Converts a JSON string to a Map.
      static java.util.Iterator<java.util.Map<java.lang.String,​java.lang.Object>> mapSequenceFrom​(java.io.Reader reader)
      Returns an Iterator over the sequence of JSON objects parsed from given reader.
      static java.util.stream.Stream<java.util.Map<java.lang.String,​java.lang.Object>> mapSequenceFrom​(java.nio.file.Path path)
      Parses the file and returns a stream of Map.
      static java.lang.String toJson​(java.lang.Object object)
      Creates a JSON string for the given object.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • hazelcastJsonValue

        @Nonnull
        public static HazelcastJsonValue hazelcastJsonValue​(@Nonnull
                                                            java.lang.Object object)
        Creates a HazelcastJsonValue by converting given the object to string using Object.toString().
      • beanFrom

        @Nullable
        public static <T> T beanFrom​(@Nonnull
                                     java.lang.String jsonString,
                                     @Nonnull
                                     java.lang.Class<T> type)
                              throws java.io.IOException
        Converts a JSON string to an object of the given type.
        Throws:
        java.io.IOException
      • mapFrom

        @Nullable
        public static java.util.Map<java.lang.String,​java.lang.Object> mapFrom​(@Nonnull
                                                                                     java.lang.Object object)
                                                                              throws java.io.IOException
        Converts a JSON string to a Map.
        Throws:
        java.io.IOException
      • listFrom

        @Nullable
        public static <T> java.util.List<T> listFrom​(@Nonnull
                                                     java.lang.String jsonString,
                                                     @Nonnull
                                                     java.lang.Class<T> type)
                                              throws java.io.IOException
        Converts a JSON string to a List of given type.
        Throws:
        java.io.IOException
      • listFrom

        @Nullable
        public static java.util.List<java.lang.Object> listFrom​(@Nonnull
                                                                java.lang.String jsonString)
                                                         throws java.io.IOException
        Converts a JSON string to a List.
        Throws:
        java.io.IOException
      • anyFrom

        @Nullable
        public static java.lang.Object anyFrom​(@Nonnull
                                               java.lang.String jsonString)
                                        throws java.io.IOException
        Converts a JSON string to an Object. The returned object will differ according to the content of the string:
        • content is a JSON object, returns a Map. See mapFrom(Object).
        • content is a JSON array, returns a List. See listFrom(String).
        • content is a String, null or primitive, returns String, null or primitive.
        Throws:
        java.io.IOException
      • beanSequenceFrom

        @Nonnull
        public static <T> java.util.Iterator<T> beanSequenceFrom​(@Nonnull
                                                                 java.io.Reader reader,
                                                                 @Nonnull
                                                                 java.lang.Class<T> type)
                                                          throws java.io.IOException
        Returns an Iterator over the sequence of JSON objects parsed from given reader. Each object is converted to the given type.
        Throws:
        java.io.IOException
      • mapSequenceFrom

        @Nonnull
        public static java.util.Iterator<java.util.Map<java.lang.String,​java.lang.Object>> mapSequenceFrom​(@Nonnull
                                                                                                                 java.io.Reader reader)
                                                                                                          throws java.io.IOException
        Returns an Iterator over the sequence of JSON objects parsed from given reader. Each object is converted to a Map. It will throw ClassCastException if JSON objects are just primitives (String, Number, Boolean) or JSON arrays (List).
        Throws:
        java.io.IOException
      • beanSequenceFrom

        @Nonnull
        public static <T> java.util.stream.Stream<T> beanSequenceFrom​(java.nio.file.Path path,
                                                                      @Nonnull
                                                                      java.lang.Class<T> type)
                                                               throws java.io.IOException
        Parses the file and returns a stream of objects with the given type. The file is considered to have a streaming JSON content, where each JSON string is separated by a new-line. The JSON string itself can span on multiple lines.

        See Sources.json(String, Class).

        Throws:
        java.io.IOException
      • mapSequenceFrom

        @Nonnull
        public static java.util.stream.Stream<java.util.Map<java.lang.String,​java.lang.Object>> mapSequenceFrom​(java.nio.file.Path path)
                                                                                                               throws java.io.IOException
        Parses the file and returns a stream of Map. The file is considered to have a streaming JSON content, where each JSON string is separated by a new-line. The JSON string itself can span on multiple lines.

        See Sources.json(String, Class).

        Throws:
        java.io.IOException
      • toJson

        @Nonnull
        public static java.lang.String toJson​(@Nonnull
                                              java.lang.Object object)
                                       throws java.io.IOException
        Creates a JSON string for the given object.
        Throws:
        java.io.IOException