Package common.utils

Class JsonUtils

java.lang.Object
common.utils.JsonUtils

public class JsonUtils extends Object
Collection of utilities for working with JSON.
Author:
woodser
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static <T> T
    deserialize(com.fasterxml.jackson.databind.ObjectMapper mapper, String json, com.fasterxml.jackson.core.type.TypeReference<T> type)
    Deserializes JSON to a parameterized type.
    static <T> T
    deserialize(com.fasterxml.jackson.databind.ObjectMapper mapper, String json, Class<T> clazz)
    Deserializes JSON to a specific class.
    static <T> T
    deserialize(String json, com.fasterxml.jackson.core.type.TypeReference<T> type)
    Deserializes JSON to a parameterized type.
    static <T> T
    deserialize(String json, Class<T> clazz)
    Deserializes JSON to a specific class.
    static String
    serialize(com.fasterxml.jackson.databind.ObjectMapper mapper, Object obj)
    Serializes an object to a JSON string.
    static String
    Serializes an object to a JSON string.
    static Map<String,Object>
    toMap(com.fasterxml.jackson.databind.ObjectMapper mapper, Object obj)
    Converts an object to a map.
    static Map<String,Object>
    toMap(com.fasterxml.jackson.databind.ObjectMapper mapper, String json)
    Converts a JSON string to a map.
    static Map<String,Object>
    Converts an object to a map.
    static Map<String,Object>
    toMap(String json)
    Converts a JSON string to a map.

    Methods inherited from class java.lang.Object

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

    • JsonUtils

      public JsonUtils()
  • Method Details

    • serialize

      public static String serialize(Object obj)
      Serializes an object to a JSON string.
      Parameters:
      obj - is the object to serialize
      Returns:
      String is the object serialized to a JSON string
    • serialize

      public static String serialize(com.fasterxml.jackson.databind.ObjectMapper mapper, Object obj)
      Serializes an object to a JSON string.
      Parameters:
      mapper - is the jackson object mapper to use
      obj - is the object to serialize
      Returns:
      String is the object serialized to a JSON string
    • deserialize

      public static <T> T deserialize(String json, Class<T> clazz)
      Deserializes JSON to a specific class.
      Type Parameters:
      T - templated type
      Parameters:
      json - is the JSON to deserialize
      clazz - specifies the class to deserialize to
      Returns:
      T is the object deserialized from JSON to the given class
    • deserialize

      public static <T> T deserialize(com.fasterxml.jackson.databind.ObjectMapper mapper, String json, Class<T> clazz)
      Deserializes JSON to a specific class.
      Type Parameters:
      T - templated type
      Parameters:
      mapper - is the jackson object mapper to use
      json - is the JSON to deserialize
      clazz - specifies the class to deserialize to
      Returns:
      T is the object deserialized from JSON to the given class
    • deserialize

      public static <T> T deserialize(String json, com.fasterxml.jackson.core.type.TypeReference<T> type)
      Deserializes JSON to a parameterized type.
      Type Parameters:
      T - the map, list, object, etc to deserialize
      Parameters:
      json - is the JSON to deserialize
      type - is the parameterized type to deserialize to (e.g. new TypeReference<Map<String, Object>>(){})
      Returns:
      T is the object deserialized from JSON to the given parameterized type
    • deserialize

      public static <T> T deserialize(com.fasterxml.jackson.databind.ObjectMapper mapper, String json, com.fasterxml.jackson.core.type.TypeReference<T> type)
      Deserializes JSON to a parameterized type.
      Type Parameters:
      T - the map, list, object, etc to deserialize
      Parameters:
      mapper - is the jackson object mapper to use
      json - is the JSON to deserialize
      type - is the parameterized type to deserialize to (e.g. new TypeReference<Map<String, Object>>(){})
      Returns:
      T is the object deserialized from JSON to the given parameterized type
    • toMap

      public static Map<String,Object> toMap(String json)
      Converts a JSON string to a map.
      Parameters:
      json - is the string to convert to a map
      Returns:
      the json string converted to a map
    • toMap

      public static Map<String,Object> toMap(com.fasterxml.jackson.databind.ObjectMapper mapper, String json)
      Converts a JSON string to a map.
      Parameters:
      mapper - is the jackson object mapper to use
      json - is the string to convert to a map
      Returns:
      is the json string converted to a map
    • toMap

      public static Map<String,Object> toMap(Object obj)
      Converts an object to a map.
      Parameters:
      obj - is the object to a convert to a map
      Returns:
      the object converted to a map
    • toMap

      public static Map<String,Object> toMap(com.fasterxml.jackson.databind.ObjectMapper mapper, Object obj)
      Converts an object to a map.
      Parameters:
      mapper - is the jackson object mapper to use
      obj - is the object to a convert to a map
      Returns:
      the object converted to a map