Package com.hazelcast.jet.json
Class JsonUtil
- java.lang.Object
-
- com.hazelcast.jet.json.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 anIterator
over the sequence of JSON objects parsed from givenreader
.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 aHazelcastJsonValue
by converting given the object to string usingObject.toString()
.static java.util.List<java.lang.Object>
listFrom(java.lang.String jsonString)
Converts a JSON string to aList
.static <T> java.util.List<T>
listFrom(java.lang.String jsonString, java.lang.Class<T> type)
Converts a JSON string to aList
of given type.static java.util.Map<java.lang.String,java.lang.Object>
mapFrom(java.lang.Object object)
Converts a JSON string to aMap
.static java.util.Iterator<java.util.Map<java.lang.String,java.lang.Object>>
mapSequenceFrom(java.io.Reader reader)
Returns anIterator
over the sequence of JSON objects parsed from givenreader
.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 ofMap
.static java.lang.String
toJson(java.lang.Object object)
Creates a JSON string for the given object.
-
-
-
Method Detail
-
hazelcastJsonValue
@Nonnull public static HazelcastJsonValue hazelcastJsonValue(@Nonnull java.lang.Object object)
Creates aHazelcastJsonValue
by converting given the object to string usingObject.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 aMap
.- 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 aList
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 aList
.- 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
. SeemapFrom(Object)
. - content is a JSON array, returns a
List
. SeelistFrom(String)
. - content is a String, null or primitive, returns String, null or primitive.
- Throws:
java.io.IOException
- content is a JSON object, returns a
-
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 anIterator
over the sequence of JSON objects parsed from givenreader
. Each object is converted to the giventype
.- 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 anIterator
over the sequence of JSON objects parsed from givenreader
. Each object is converted to aMap
. It will throwClassCastException
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.- 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 ofMap
. 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.- 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
-
-