Class JsonUtils
java.lang.Object
io.vrap.rmf.base.client.utils.json.JsonUtils
Class with methods to customize the JSON serialization/deserialization
-
Nested Class Summary
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic ObjectMapper
creates a newObjectMapper
instancestatic ObjectMapper
createObjectMapper
(ModuleOptions options) static <T> T
executing
(JsonUtils.SupplierThrowingIOException<T> supplier) static <T> T
fromInputStream
(InputStream content, com.fasterxml.jackson.core.type.TypeReference<T> typeReference) deserializes the given json string to the given classstatic <T> T
fromInputStream
(InputStream content, Class<T> clazz) deserializes the given json string to the given classstatic <T> T
fromJsonByteArray
(byte[] content, Class<T> clazz) deserializes the given json string to the given classstatic <T> T
fromJsonNode
(JsonNode jsonNode, com.fasterxml.jackson.core.type.TypeReference<T> typeReference) Reads a Java object from JsonNode data.static <T> T
fromJsonString
(String jsonAsString, com.fasterxml.jackson.core.type.TypeReference<T> typeReference) Reads a Java object from JSON data (String).static <T> T
fromJsonString
(String content, Class<T> clazz) deserializes the given json string to the given classstatic ObjectMapper
defaultObjectMapper
static JsonNode
Parses a String containing JSON data and produces aJsonNode
.static String
prettyPrint
(String json) Pretty prints a given JSON string.static <T> T
readObjectFromResource
(String resourcePath, com.fasterxml.jackson.core.type.TypeReference<T> typeReference) Reads a UTF-8 JSON text file from the classpath of the current thread and transforms it into a Java object.static <T> T
readObjectFromResource
(String resourcePath, JavaType javaType) static <T> T
readObjectFromResource
(String resourcePath, Class<T> clazz) static byte[]
toJsonByteArray
(Object value) serializes the given object to JSON as a byte arraystatic JsonNode
toJsonNode
(Object value) Converts a commercetools Composable Commerce Java object to JSON asJsonNode
.static String
toJsonString
(Object value) serializes the given object to JSON as a byte array
-
Constructor Details
-
JsonUtils
public JsonUtils()
-
-
Method Details
-
createObjectMapper
creates a newObjectMapper
instance- Returns:
- ObjectMapper
-
createObjectMapper
- Parameters:
options
- configuration for jackson modules supplied by aModuleSupplier
- Returns:
- ObjectMapper
-
toJsonByteArray
public static byte[] toJsonByteArray(Object value) throws com.fasterxml.jackson.core.JsonProcessingException serializes the given object to JSON as a byte array- Parameters:
value
- object to be serialized- Returns:
- json byte array
- Throws:
com.fasterxml.jackson.core.JsonProcessingException
- serialization errors
-
toJsonString
public static String toJsonString(Object value) throws com.fasterxml.jackson.core.JsonProcessingException serializes the given object to JSON as a byte array- Parameters:
value
- object to be serialized- Returns:
- json string
- Throws:
com.fasterxml.jackson.core.JsonProcessingException
- serialization errors
-
fromJsonString
deserializes the given json string to the given class- Type Parameters:
T
- type of the result- Parameters:
clazz
- class to serialize tocontent
- json as string- Returns:
- deserialized object
-
fromJsonString
public static <T> T fromJsonString(String jsonAsString, com.fasterxml.jackson.core.type.TypeReference<T> typeReference) Reads a Java object from JSON data (String).- Type Parameters:
T
- the type of the result- Parameters:
jsonAsString
- the JSON data which represents sth. of type<T>
typeReference
- the full generic type information about the object to create- Returns:
- the created objected
-
fromJsonNode
public static <T> T fromJsonNode(JsonNode jsonNode, com.fasterxml.jackson.core.type.TypeReference<T> typeReference) Reads a Java object from JsonNode data.- Type Parameters:
T
- the type of the result- Parameters:
jsonNode
- the JSON data which represents sth. of type<T>
typeReference
- the full generic type information about the object to create- Returns:
- the created objected
-
toJsonNode
Converts a commercetools Composable Commerce Java object to JSON asJsonNode
.If
value
is of type String and contains JSON data, that will be ignored,value
will be treated as just any String. If you want to parse a JSON String to a JsonNode useparse(java.lang.String)
instead.- Parameters:
value
- the object to convert- Returns:
- new json
-
parse
Parses a String containing JSON data and produces aJsonNode
.- Parameters:
jsonAsString
- json data- Returns:
- new JsonNode
-
fromJsonByteArray
deserializes the given json string to the given class- Type Parameters:
T
- type of the result- Parameters:
clazz
- class to serialize tocontent
- json as byte array- Returns:
- deserialized object
- Throws:
JsonException
- deserialization errors
-
fromInputStream
deserializes the given json string to the given class- Type Parameters:
T
- type of the result- Parameters:
clazz
- class to serialize tocontent
- json as inputstream- Returns:
- deserialized object
- Throws:
JsonException
- deserialization errors
-
fromInputStream
public static <T> T fromInputStream(InputStream content, com.fasterxml.jackson.core.type.TypeReference<T> typeReference) deserializes the given json string to the given class- Type Parameters:
T
- type of the result- Parameters:
typeReference
- the full generic type information about the object to createcontent
- json as inputstream- Returns:
- deserialized object
- Throws:
JsonException
- deserialization errors
-
readObjectFromResource
public static <T> T readObjectFromResource(String resourcePath, com.fasterxml.jackson.core.type.TypeReference<T> typeReference) Reads a UTF-8 JSON text file from the classpath of the current thread and transforms it into a Java object.- Type Parameters:
T
- the type of the result- Parameters:
resourcePath
- the path to the resource. Example: If the file is located in "src/test/resources/foo/bar/product.json" then the path should be "foo/bar/product.json"typeReference
- the full generic type information about the object to create- Returns:
- the created objected
-
readObjectFromResource
-
readObjectFromResource
-
getConfiguredObjectMapper
defaultObjectMapper
- Returns:
- ObjectMapper
-
prettyPrint
Pretty prints a given JSON string.- Parameters:
json
- JSON code as String which should be formatted- Returns:
json
formatted
-
executing
-