Package com.hazelcast.jet.json
Class JsonUtil
java.lang.Object
com.hazelcast.jet.json.JsonUtil
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
Modifier and TypeMethodDescriptionstatic Object
Converts a JSON string to an Object.static <T> T
Converts a JSON string to an object of the given type.static <T> Iterator<T>
beanSequenceFrom
(Reader reader, Class<T> type) Returns anIterator
over the sequence of JSON objects parsed from givenreader
.static <T> Stream<T>
beanSequenceFrom
(Path path, Class<T> type) Parses the file and returns a stream of objects with the given type.static HazelcastJsonValue
hazelcastJsonValue
(Object object) Creates aHazelcastJsonValue
by converting given the object to string usingObject.toString()
.Converts a JSON string to aList
.static <T> List<T>
Converts a JSON string to aList
of given type.Converts a JSON string to aMap
.mapSequenceFrom
(Reader reader) Returns anIterator
over the sequence of JSON objects parsed from givenreader
.mapSequenceFrom
(Path path) Parses the file and returns a stream ofMap
.static String
Creates a JSON string for the given object.
-
Method Details
-
hazelcastJsonValue
Creates aHazelcastJsonValue
by converting given the object to string usingObject.toString()
. -
beanFrom
@Nullable public static <T> T beanFrom(@Nonnull String jsonString, @Nonnull Class<T> type) throws IOException Converts a JSON string to an object of the given type.- Throws:
IOException
-
mapFrom
Converts a JSON string to aMap
.- Throws:
IOException
-
listFrom
@Nullable public static <T> List<T> listFrom(@Nonnull String jsonString, @Nonnull Class<T> type) throws IOException Converts a JSON string to aList
of given type.- Throws:
IOException
-
listFrom
Converts a JSON string to aList
.- Throws:
IOException
-
anyFrom
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:
IOException
- content is a JSON object, returns a
-
beanSequenceFrom
@Nonnull public static <T> Iterator<T> beanSequenceFrom(@Nonnull Reader reader, @Nonnull Class<T> type) throws IOException Returns anIterator
over the sequence of JSON objects parsed from givenreader
. Each object is converted to the giventype
.- Throws:
IOException
-
mapSequenceFrom
@Nonnull public static Iterator<Map<String,Object>> mapSequenceFrom(@Nonnull Reader reader) throws 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:
IOException
-
beanSequenceFrom
@Nonnull public static <T> Stream<T> beanSequenceFrom(Path path, @Nonnull Class<T> type) throws 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:
IOException
-
mapSequenceFrom
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:
IOException
-
toJson
Creates a JSON string for the given object.- Throws:
IOException
-