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
Nested Classes -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic ObjectMappercreates a newObjectMapperinstancestatic ObjectMappercreateObjectMapper(ModuleOptions options) static <T> Texecuting(JsonUtils.SupplierThrowingIOException<T> supplier) static <T> TfromInputStream(InputStream content, Class<T> clazz) deserializes the given json string to the given classstatic <T> TfromInputStream(InputStream content, tools.jackson.core.type.TypeReference<T> typeReference) deserializes the given json string to the given classstatic <T> TfromJsonByteArray(byte[] content, Class<T> clazz) deserializes the given json string to the given classstatic <T> TfromJsonNode(JsonNode jsonNode, tools.jackson.core.type.TypeReference<T> typeReference) Reads a Java object from JsonNode data.static <T> TfromJsonString(String content, Class<T> clazz) deserializes the given json string to the given classstatic <T> TfromJsonString(String jsonAsString, tools.jackson.core.type.TypeReference<T> typeReference) Reads a Java object from JSON data (String).static ObjectMapperdefaultObjectMapperstatic JsonNodeParses a String containing JSON data and produces aJsonNode.static StringprettyPrint(String json) Pretty prints a given JSON string.static <T> TreadObjectFromResource(String resourcePath, Class<T> clazz) static <T> TreadObjectFromResource(String resourcePath, tools.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> TreadObjectFromResource(String resourcePath, JavaType javaType) static byte[]toJsonByteArray(Object value) serializes the given object to JSON as a byte arraystatic JsonNodetoJsonNode(Object value) Converts a commercetools Java object to JSON asJsonNode.static StringtoJsonString(Object value) serializes the given object to JSON as a byte array
-
Constructor Details
-
JsonUtils
public JsonUtils()
-
-
Method Details
-
createObjectMapper
creates a newObjectMapperinstance- Returns:
- ObjectMapper
-
createObjectMapper
- Parameters:
options- configuration for jackson modules supplied by aModuleSupplier- Returns:
- ObjectMapper
-
toJsonByteArray
serializes the given object to JSON as a byte array- Parameters:
value- object to be serialized- Returns:
- json byte array
- Throws:
tools.jackson.core.JacksonException- serialization errors
-
toJsonString
serializes the given object to JSON as a byte array- Parameters:
value- object to be serialized- Returns:
- json string
- Throws:
tools.jackson.core.JacksonException- 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, tools.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, tools.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 Java object to JSON asJsonNode.If
valueis of type String and contains JSON data, that will be ignored,valuewill 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, tools.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, tools.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:
jsonformatted
-
executing
-