Class JsonUtils

java.lang.Object
io.vrap.rmf.base.client.utils.json.JsonUtils

public class JsonUtils extends Object
Class with methods to customize the JSON serialization/deserialization
  • Constructor Details

    • JsonUtils

      public JsonUtils()
  • Method Details

    • createObjectMapper

      public static ObjectMapper createObjectMapper()
      creates a new ObjectMapper instance
      Returns:
      ObjectMapper
    • createObjectMapper

      public static ObjectMapper createObjectMapper(ModuleOptions options)
      Parameters:
      options - configuration for jackson modules supplied by a ModuleSupplier
      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

      public static <T> T fromJsonString(String content, Class<T> clazz)
      deserializes the given json string to the given class
      Type Parameters:
      T - type of the result
      Parameters:
      clazz - class to serialize to
      content - 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

      public static JsonNode toJsonNode(Object value)
      Converts a commercetools Composable Commerce Java object to JSON as 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.

      Parameters:
      value - the object to convert
      Returns:
      new json
    • parse

      public static JsonNode parse(String jsonAsString)
      Parses a String containing JSON data and produces a JsonNode.
      Parameters:
      jsonAsString - json data
      Returns:
      new JsonNode
    • fromJsonByteArray

      public static <T> T fromJsonByteArray(byte[] content, Class<T> clazz)
      deserializes the given json string to the given class
      Type Parameters:
      T - type of the result
      Parameters:
      clazz - class to serialize to
      content - json as byte array
      Returns:
      deserialized object
      Throws:
      JsonException - deserialization errors
    • fromInputStream

      public static <T> T fromInputStream(InputStream content, Class<T> clazz)
      deserializes the given json string to the given class
      Type Parameters:
      T - type of the result
      Parameters:
      clazz - class to serialize to
      content - 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 create
      content - 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

      public static <T> T readObjectFromResource(String resourcePath, JavaType javaType)
    • readObjectFromResource

      public static <T> T readObjectFromResource(String resourcePath, Class<T> clazz)
    • getConfiguredObjectMapper

      public static ObjectMapper getConfiguredObjectMapper()
      default ObjectMapper
      Returns:
      ObjectMapper
    • prettyPrint

      public static String prettyPrint(String json)
      Pretty prints a given JSON string.
      Parameters:
      json - JSON code as String which should be formatted
      Returns:
      json formatted
    • executing

      public static <T> T executing(JsonUtils.SupplierThrowingIOException<T> supplier)