Class JsonHelper
java.lang.Object
org.sentrysoftware.metricshub.engine.common.helpers.JsonHelper
Helper class for JSON and YAML serialization and deserialization using Jackson.
-
Method Summary
Modifier and TypeMethodDescriptionstatic com.fasterxml.jackson.databind.ObjectMapperBuild and return a newObjectMapperinstance enabling indentation.static com.fasterxml.jackson.databind.ObjectMapperBuild a newObjectMapperusingYAMLFactory.static <T> Tdeserialize(com.fasterxml.jackson.databind.ObjectMapper mapper, com.fasterxml.jackson.core.TreeNode node, Class<T> type) Deserialize the given input stream.static <T> Tdeserialize(com.fasterxml.jackson.databind.ObjectMapper mapper, InputStream input, Class<T> type) Deserialize the given input stream.static <T> Tdeserialize(InputStream is, Class<T> clazz) Deserialize and return the requested type using the InputStream.static <T> Tdeserialize(String json, Class<T> clazz) Deserialize the JSON string to a Java value.static <T> Stringserialize(T value) Serialize the Java value to a JSON String
-
Method Details
-
serialize
Serialize the Java value to a JSON String- Type Parameters:
T- The element type- Parameters:
value- the value object to serialize.- Returns:
- a string containing the object serialized in JSON format.
-
deserialize
Deserialize the JSON string to a Java value.- Type Parameters:
T- The element type- Parameters:
json- The JSON value we wish to deserialize.clazz- The target Java type.- Returns:
- new Object.
- Throws:
IOException- if an error occurs during deserialization.
-
deserialize
public static <T> T deserialize(com.fasterxml.jackson.databind.ObjectMapper mapper, InputStream input, Class<T> type) throws IOException Deserialize the given input stream.- Type Parameters:
T- The element type- Parameters:
mapper-ObjectMapperinstance used to deserialize input stream.input-InputStreaminstance.type- Java type.- Returns:
- new instance of T.
- Throws:
IOException- if an error occurs during deserialization.
-
deserialize
public static <T> T deserialize(com.fasterxml.jackson.databind.ObjectMapper mapper, com.fasterxml.jackson.core.TreeNode node, Class<T> type) throws IOException Deserialize the given input stream.- Type Parameters:
T- The element type- Parameters:
mapper-ObjectMapperinstance used to deserialize input stream.node-TreeNodeinstance.type- Java type.- Returns:
- new instance of T.
- Throws:
IOException- if an error occurs during deserialization.
-
deserialize
Deserialize and return the requested type using the InputStream.- Type Parameters:
T- The element type- Parameters:
is-InputStreamconnection to the JSON.clazz- The target Java type.- Returns:
- new Object.
- Throws:
IOException- if an error occurs during deserialization.
-
buildObjectMapper
public static com.fasterxml.jackson.databind.ObjectMapper buildObjectMapper()Build and return a newObjectMapperinstance enabling indentation.- Returns:
- new
ObjectMapperinstance.
-
buildYamlMapper
public static com.fasterxml.jackson.databind.ObjectMapper buildYamlMapper()Build a newObjectMapperusingYAMLFactory.- Returns:
- new
ObjectMapperinstance.
-