java.lang.Object
org.sentrysoftware.metricshub.engine.common.helpers.JsonHelper

public class JsonHelper extends Object
Helper class for JSON and YAML serialization and deserialization using Jackson.
  • Method Summary

    Modifier and Type
    Method
    Description
    static com.fasterxml.jackson.databind.ObjectMapper
    Build and return a new ObjectMapper instance enabling indentation.
    static com.fasterxml.jackson.databind.ObjectMapper
    Build a new ObjectMapper using YAMLFactory.
    static <T> T
    deserialize(com.fasterxml.jackson.databind.ObjectMapper mapper, com.fasterxml.jackson.core.TreeNode node, Class<T> type)
    Deserialize the given input stream.
    static <T> T
    deserialize(com.fasterxml.jackson.databind.ObjectMapper mapper, InputStream input, Class<T> type)
    Deserialize the given input stream.
    static <T> T
    deserialize(InputStream is, Class<T> clazz)
    Deserialize and return the requested type using the InputStream.
    static <T> T
    deserialize(String json, Class<T> clazz)
    Deserialize the JSON string to a Java value.
    static <T> String
    serialize(T value)
    Serialize the Java value to a JSON String

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • serialize

      public static <T> String serialize(T value)
      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

      public static <T> T deserialize(String json, Class<T> clazz) throws IOException
      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 - ObjectMapper instance used to deserialize input stream.
      input - InputStream instance.
      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 - ObjectMapper instance used to deserialize input stream.
      node - TreeNode instance.
      type - Java type.
      Returns:
      new instance of T.
      Throws:
      IOException - if an error occurs during deserialization.
    • deserialize

      public static <T> T deserialize(InputStream is, Class<T> clazz) throws IOException
      Deserialize and return the requested type using the InputStream.
      Type Parameters:
      T - The element type
      Parameters:
      is - InputStream connection 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 new ObjectMapper instance enabling indentation.
      Returns:
      new ObjectMapper instance.
    • buildYamlMapper

      public static com.fasterxml.jackson.databind.ObjectMapper buildYamlMapper()
      Build a new ObjectMapper using YAMLFactory.
      Returns:
      new ObjectMapper instance.