Package com.hazelcast.jet.json
Class JsonUtil
java.lang.Object
com.hazelcast.jet.json.JsonUtil
public final class JsonUtil extends 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:
- 4.2
-
Method Summary
Modifier and Type Method Description static Object
anyFrom(String jsonString)
Converts a JSON string to an Object.static <T> T
beanFrom(String jsonString, Class<T> type)
Converts a JSON string to a object of 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()
.static List<Object>
listFrom(String jsonString)
Converts a JSON string to aList
.static <T> List<T>
listFrom(String jsonString, Class<T> type)
Converts a JSON string to aList
of given type.static Map<String,Object>
mapFrom(String jsonString)
Converts a JSON string to aMap
.static Iterator<Map<String,Object>>
mapSequenceFrom(Reader reader)
Returns anIterator
over the sequence of JSON objects parsed from givenreader
.static Stream<Map<String,Object>>
mapSequenceFrom(Path path)
Parses the file and returns a stream ofMap
.static String
toJson(Object object)
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 IOExceptionConverts a JSON string to a object of 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 IOExceptionConverts 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(String)
. - 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 IOExceptionReturns 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 IOExceptionReturns 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 IOExceptionParses 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
-