public final class SphereJsonUtils extends Object
If an error occurs, the JsonException (a RuntimeException) will be thrown:
final String brokenJsonString = "{\"de\":\",]]]]"; assertThatThrownBy(() -> SphereJsonUtils.readObject(brokenJsonString, LocalizedString.typeReference())) .isInstanceOf(JsonException.class);See the test code.
| Modifier and Type | Method and Description |
|---|---|
static com.fasterxml.jackson.databind.ObjectMapper |
configureObjectMapper(com.fasterxml.jackson.databind.ObjectMapper objectMapper)
Configures an existing external Jackson
ObjectMapper with all modules and settings required
to serialize and deserialize according to JVM SDK conventions and capabilities:
Jackson Parameter Names Module (requires Java compiler flag -parameters)
Jackson JavaTime Module
Jackson config: do not fail on unknown properties
Jackson config: do not fail on empty beans
Jackson config: do not use getters as setters
Jackson config: do not serialize null value properties
Serialize LinkedHashSet as Set
Composable Commerce LocalizedString
Composable Commerce Time, Date, DateTime
Composable Commerce Money for Java MonetaryAmount
Composable Commerce Enum
|
static com.fasterxml.jackson.databind.JavaType |
convertToJavaType(Class<?> clazz) |
static <T> com.fasterxml.jackson.databind.JavaType |
convertToJavaType(com.fasterxml.jackson.core.type.TypeReference<T> typeReference) |
static com.fasterxml.jackson.databind.JavaType |
createCustomObjectJavaType(Class<?> customObject,
Class<?> param) |
static com.fasterxml.jackson.databind.ObjectMapper |
newObjectMapper()
Creates a new
ObjectMapper which is configured for sphere projects. |
static com.fasterxml.jackson.databind.node.ObjectNode |
newObjectNode()
Creates a new
ObjectNode created by the commercetools Composable Commerce object mapper. |
static com.fasterxml.jackson.databind.JsonNode |
parse(byte[] jsonAsBytes)
Parses a byte array containing JSON data and produces a
JsonNode. |
static com.fasterxml.jackson.databind.JsonNode |
parse(String jsonAsString)
Parses a String containing JSON data and produces a
JsonNode. |
static String |
prettyPrint(com.fasterxml.jackson.databind.JsonNode jsonNode) |
static String |
prettyPrint(String json)
Pretty prints a given JSON string.
|
static <T> T |
readObject(byte[] jsonAsBytes,
com.fasterxml.jackson.databind.JavaType javaType) |
static <T> T |
readObject(byte[] jsonAsBytes,
com.fasterxml.jackson.core.type.TypeReference<T> typeReference)
Reads a Java object from JSON string data encoded as UTF-8.
|
static <T> T |
readObject(com.fasterxml.jackson.databind.JsonNode jsonNode,
Class<T> clazz)
Reads a Java object from JsonNode data.
|
static <T> T |
readObject(com.fasterxml.jackson.databind.JsonNode jsonNode,
com.fasterxml.jackson.databind.JavaType javaType) |
static <T> T |
readObject(com.fasterxml.jackson.databind.JsonNode jsonNode,
com.fasterxml.jackson.core.type.TypeReference<T> typeReference)
Reads a Java object from JsonNode data.
|
static <T> T |
readObject(String jsonAsString,
Class<T> clazz) |
static <T> T |
readObject(String jsonAsString,
com.fasterxml.jackson.core.type.TypeReference<T> typeReference)
Reads a Java object from JSON data (String).
|
static <T> T |
readObjectFromResource(String resourcePath,
Class<T> clazz) |
static <T> T |
readObjectFromResource(String resourcePath,
com.fasterxml.jackson.databind.JavaType javaType) |
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 com.fasterxml.jackson.databind.JsonNode |
toJsonNode(Object value)
Converts a Composable Commerce Java object to JSON as
JsonNode. |
static String |
toJsonString(Object value)
Converts a commercetools Composable Commerce Java object to JSON as String (one liner).
|
static String |
toPrettyJsonString(Object value)
Converts a Composable Commerce Java object to JSON as String (pretty).
|
public static com.fasterxml.jackson.databind.ObjectMapper newObjectMapper()
ObjectMapper which is configured for sphere projects.public static com.fasterxml.jackson.databind.ObjectMapper configureObjectMapper(com.fasterxml.jackson.databind.ObjectMapper objectMapper)
ObjectMapper with all modules and settings required
to serialize and deserialize according to JVM SDK conventions and capabilities:
objectMapper - the object mapper to configurepublic static String toJsonString(Object value)
final LocalizedString value = LocalizedString.of(ENGLISH, "dog food", GERMAN, "Hundefutter"); assertThat(SphereJsonUtils.toJsonString(value)) .isEqualTo("{\"en\":\"dog food\",\"de\":\"Hundefutter\"}");See the test code.
value - the object to convertpublic static String toPrettyJsonString(Object value)
final LocalizedString value = LocalizedString.of(ENGLISH, "dog food", GERMAN, "Hundefutter"); assertThat(SphereJsonUtils.toPrettyJsonString(value)) .isEqualTo("{\n" + " \"en\" : \"dog food\",\n" + " \"de\" : \"Hundefutter\"\n" + "}");See the test code.
value - the object to convertpublic static com.fasterxml.jackson.databind.JsonNode toJsonNode(Object value)
JsonNode.
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 use parse(java.lang.String) instead.
final LocalizedString value = LocalizedString.of(ENGLISH, "dog food", GERMAN, "Hundefutter"); final JsonNode actual = SphereJsonUtils.toJsonNode(value); assertThat(actual.get("de").asText()).isEqualTo("Hundefutter"); assertThat(actual.get("en").asText()).isEqualTo("dog food");See the test code.
value - the object to convertpublic static com.fasterxml.jackson.databind.JsonNode parse(String jsonAsString)
JsonNode.
final JsonNode actual = SphereJsonUtils.parse("{\"de\":\"Hundefutter\",\"en\":\"dog food\"}"); final ObjectNode expected = SphereJsonUtils.newObjectNode(); expected.put("de", "Hundefutter"); expected.put("en", "dog food"); assertThat(actual).isEqualTo(expected);See the test code.
jsonAsString - json datapublic static com.fasterxml.jackson.databind.JsonNode parse(byte[] jsonAsBytes)
JsonNode.jsonAsBytes - json datapublic static String prettyPrint(String json)
final String jsonString = "{\"de\":\"Hundefutter\",\"en\":\"dog food\"}"; final String actual = SphereJsonUtils.prettyPrint(jsonString); assertThat(actual).isEqualTo("{\n" + " \"de\" : \"Hundefutter\",\n" + " \"en\" : \"dog food\"\n" + "}" );See the test code.
json - JSON code as String which should be formattedjson formattedpublic static String prettyPrint(com.fasterxml.jackson.databind.JsonNode jsonNode)
public static <T> T readObjectFromResource(String resourcePath, com.fasterxml.jackson.core.type.TypeReference<T> typeReference)
T - the type of the resultresourcePath - 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 createpublic static <T> T readObjectFromResource(String resourcePath, com.fasterxml.jackson.databind.JavaType javaType)
public static <T> T readObjectFromResource(String resourcePath, Class<T> clazz)
public static <T> T readObject(String jsonAsString, com.fasterxml.jackson.core.type.TypeReference<T> typeReference)
final String jsonString = "{\"de\":\"Hundefutter\",\"en\":\"dog food\"}"; final LocalizedString actual = SphereJsonUtils.readObject(jsonString, LocalizedString.typeReference()); assertThat(actual).isEqualTo(LocalizedString.of(ENGLISH, "dog food", GERMAN, "Hundefutter"));See the test code.
T - the type of the resultjsonAsString - the JSON data which represents sth. of type <T>typeReference - the full generic type information about the object to createpublic static <T> T readObject(com.fasterxml.jackson.databind.JsonNode jsonNode,
com.fasterxml.jackson.core.type.TypeReference<T> typeReference)
final ObjectNode jsonNode = SphereJsonUtils.newObjectNode(); jsonNode.put("de", "Hundefutter"); jsonNode.put("en", "dog food"); final LocalizedString actual = SphereJsonUtils.readObject(jsonNode, LocalizedString.typeReference()); assertThat(actual).isEqualTo(LocalizedString.of(ENGLISH, "dog food", GERMAN, "Hundefutter"));See the test code.
T - the type of the resultjsonNode - the JSON data which represents sth. of type <T>typeReference - the full generic type information about the object to createpublic static <T> T readObject(com.fasterxml.jackson.databind.JsonNode jsonNode,
Class<T> clazz)
final ObjectNode jsonNode = SphereJsonUtils.newObjectNode(); jsonNode.put("de", "Hundefutter"); jsonNode.put("en", "dog food"); final LocalizedString actual = SphereJsonUtils.readObject(jsonNode, LocalizedString.class); assertThat(actual).isEqualTo(LocalizedString.of(ENGLISH, "dog food", GERMAN, "Hundefutter"));See the test code.
T - the type of the resultjsonNode - the JSON data which represents sth. of type <T>clazz - the class of the type to createpublic static <T> T readObject(com.fasterxml.jackson.databind.JsonNode jsonNode,
com.fasterxml.jackson.databind.JavaType javaType)
public static <T> T readObject(byte[] jsonAsBytes,
com.fasterxml.jackson.core.type.TypeReference<T> typeReference)
final ObjectNode jsonNode = SphereJsonUtils.newObjectNode(); jsonNode.put("de", "Hundefutter"); jsonNode.put("en", "dog food"); final LocalizedString actual = SphereJsonUtils.readObject(jsonNode, LocalizedString.class); assertThat(actual).isEqualTo(LocalizedString.of(ENGLISH, "dog food", GERMAN, "Hundefutter"));See the test code.
T - the type of the resultjsonAsBytes - the JSON data which represents sth. of type <T>typeReference - the full generic type information about the object to createpublic static <T> T readObject(byte[] jsonAsBytes,
com.fasterxml.jackson.databind.JavaType javaType)
public static com.fasterxml.jackson.databind.node.ObjectNode newObjectNode()
ObjectNode created by the commercetools Composable Commerce object mapper.
final ObjectNode jsonNode = SphereJsonUtils.newObjectNode(); jsonNode.put("de", "Hundefutter"); jsonNode.put("en", "dog food"); final LocalizedString actual = SphereJsonUtils.readObject(jsonNode, LocalizedString.class); assertThat(actual).isEqualTo(LocalizedString.of(ENGLISH, "dog food", GERMAN, "Hundefutter"));See the test code.
public static <T> com.fasterxml.jackson.databind.JavaType convertToJavaType(com.fasterxml.jackson.core.type.TypeReference<T> typeReference)
public static com.fasterxml.jackson.databind.JavaType createCustomObjectJavaType(Class<?> customObject, Class<?> param)
public static com.fasterxml.jackson.databind.JavaType convertToJavaType(Class<?> clazz)