Interface JsonManager

All Known Implementing Classes:
SpincastGsonManager, SpincastJsonManager

public interface JsonManager
Provides methods to play with Json strings and objects.
  • Method Details

    • create

      JsonObject create()
      Creates an empty JsonObject
    • createArray

      JsonArray createArray()
      Creates an empty JsonArray.
    • fromObject

      JsonObject fromObject(Object object)
      Creates a JsonObject from a random object..
    • fromString

      JsonObject fromString(String jsonString)
      Creates a JsonObject from a Json String.
      Returns:
      the JsonObject version of the parameter or null if the parameter is null.
    • fromMap

      JsonObject fromMap(Map<String,?> params)
      Creates an empty JsonObject based on the specified Map. An attempt will be made to create a deep copy of every elements so a modification won't affect any external references and vice-versa.

      The keys will be used as is, not parsed as JsonPaths.

      Returns:
      the JsonObject version of the parameter or null if the parameter is null.
    • fromMap

      JsonObject fromMap(Map<String,?> params, boolean parseKeysAsJsonPaths)
      Creates a JsonObject based on the specified Map. An attempt will be made to create a deep copy of every elements so a modification won't affect any external references and vice-versa.
      Parameters:
      parseKeysAsJsonPaths - if true, the keys will be parsed as JsonPaths, otherwise they will ne used as is.
      Returns:
      the JsonObject version of the parameter or null if the parameter is null.
    • fromInputStream

      JsonObject fromInputStream(InputStream inputStream)
      Creates a JsonObject from an inputStream.
      Returns:
      the JsonObject version of the parameter or null if the parameter is null.
    • fromFile

      JsonObject fromFile(File jsonFile)
      Creates a JsonObject from a Json file.
      Returns:
      the deserialized JsonObject or null if the file is null or doesn't exist.
    • fromFile

      JsonObject fromFile(String jsonFilePath)
      Creates a JsonObject from the path of a file, on the file system.
      Returns:
      the deserialized JsonObject or null if the file is null or doesn't exist.
    • fromClasspathFile

      JsonObject fromClasspathFile(String path)
      Creates a JsonObject from a classpath Json file.
      Parameters:
      the - path to the classpath file. This path always starts from the root of the classpath.
      Returns:
      the deserialized JsonObject or null if the file doesn't exist.
      Throws:
      Exception - if the path is null
    • fromStringToMap

      Map<String,Object> fromStringToMap(String jsonString)
      Creates a Map<String, Object> from a Json String.
      Returns:
      the Map version of the parameter or null if the parameter is null.
    • fromInputStreamToMap

      Map<String,Object> fromInputStreamToMap(InputStream inputStream)
      Creates a Map<String, Object> from a Json inputStream.
      Returns:
      the Map version of the parameter or null if the parameter is null.
    • fromString

      <T> T fromString(String jsonString, Class<T> clazz)
      Creates an instance of the specified T type from a Json String.
      Returns:
      the deserialized version of the parameter or null if the parameter is null.
    • fromInputStream

      <T> T fromInputStream(InputStream inputStream, Class<T> clazz)
      Creates an instance of the specified T type from a Json inputStream.
      Returns:
      the deserialized version of the parameter or null if the parameter is null.
    • fromCollectionToJsonArray

      JsonArray fromCollectionToJsonArray(Collection<?> collection)
      Creates a JsonArray from a random collection.
    • fromStringArray

      JsonArray fromStringArray(String jsonString)
      Creates a JsonArray from a Json String.
      Returns:
      the JsonArray version of the parameter or null if the parameter is null.
    • fromListArray

      JsonArray fromListArray(List<?> elements)
      Creates a JsonArray from a List of elements.
      Returns:
      the JsonArray version of the parameter or null if the parameter is null.
    • fromInputStreamArray

      JsonArray fromInputStreamArray(InputStream inputStream)
      Creates a JsonArray from an inputStream.
      Returns:
      the JsonArray version of the parameter or null if the parameter is null.
    • createForm

      Form createForm(String formName)
      Creates an empty Form, which is a JsonObject + a validations container.
    • toJsonString

      String toJsonString(Object obj)
      Gets the Json String representation of the specified object.
    • toJsonString

      String toJsonString(Object obj, boolean pretty)
      Gets the Json String representation of the specified object.
      Parameters:
      pretty - if true, the generated String will be formatted.
    • parseDateFromJson

      Date parseDateFromJson(String str)
      Converts a Json date (ISO-8601) to a Java UTC date.
    • convertToJsonDate

      String convertToJsonDate(Date date)
      Converts a Date to a Json date format.
    • convertToNativeType

      Object convertToNativeType(Object originalObject)
      Convert a random object to a valid native JsonObject type, if it's not already.
    • clone

      Object clone(Object originalObject)
      Tries to clone an object to a JsonObject or a JsonArray, if the object is not of a primitive type (or BigDecimal).

      The cloning is made by serializing the Object to a Json string, and deserializing back. So any (de)serialization rules apply.

      The non primitive object will be cloned to a JsonArray if it is a :

      • JsonArray
      • Collection
      • Array

    • clone

      Object clone(Object originalObject, boolean mutable)
      Tries to clone an object to a JsonObject or a JsonArray, if the object is not of a primitive type (or BigDecimal).

      The cloning is made by serializing the Object to a Json string, and deserializing back. So any (de)serialization rules apply.

      The non primitive object will be cloned to a JsonArray if it is a :

      • JsonArray
      • Collection
      • Array

      Parameters:
      mutable - if false, the resulting Object and all its potential children will be immutable.
    • cloneJsonObject

      JsonObject cloneJsonObject(JsonObject jsonObject, boolean mutable)
      Deep copy of the JsonObject, so any modification to the original won't affect the clone, and vice-versa.

      Note that if the current object is immutable and the mutable parameter is set to false, then the current object will be returned as is, since no cloning is required.

      Parameters:
      mutable - if true the resulting array and all its children will be mutable, otherwise they will all be immutable.
    • cloneJsonArray

      JsonArray cloneJsonArray(JsonArray jsonArray, boolean mutable)
      Deep copy of the JsonArray, so any modification to the original won't affect the clone, and vice-versa.

      Note that if the current object is immutable and the mutable parameter is set to false, then the current array will be returned as is, since no cloning is required.

      Parameters:
      mutable - if true the resulting array and all its children will be mutable, otherwise they will all be immutable.
    • getElementAtJsonPath

      Object getElementAtJsonPath(JsonObject obj, String jsonPath)
      Gets an element from the JsonObject at the specified JsonPath.
      Returns:
      the element or null if not found.
    • getElementAtJsonPath

      Object getElementAtJsonPath(JsonObject obj, String jsonPath, Object defaultElement)
      Gets an element from the JsonObject at the specified JsonPath.
      Returns:
      the element or null if not found.
    • getElementAtJsonPath

      Object getElementAtJsonPath(JsonArray array, String jsonPath)
      Gets an element from the JsonArray at the specified JsonPath.
      Returns:
      the element or null if not found.
    • getElementAtJsonPath

      Object getElementAtJsonPath(JsonArray array, String jsonPath, Object defaultElement)
      Gets an element from the JsonArray at the specified JsonPath.
      Returns:
      the element or null if not found.
    • putElementAtJsonPath

      void putElementAtJsonPath(JsonObjectOrArray obj, String jsonPath, Object element)
      Puts an element in the object at the specified JsonPath.

      All the hierarchy to the end of the JsonPath is created if required.

    • putElementAtJsonPath

      void putElementAtJsonPath(JsonObjectOrArray obj, String jsonPath, Object element, boolean clone)
      Puts a clone of the element in the object at the specified JsonPath.

      All the hierarchy to the end of the JsonPath is created if required.

    • removeElementAtJsonPath

      void removeElementAtJsonPath(JsonObject obj, String jsonPath)
      Removes an element at the specified JsonPath from the object.
    • removeElementAtJsonPath

      void removeElementAtJsonPath(JsonArray array, String jsonPath)
      Removes an element at the specified JsonPath from the array.
    • isElementExists

      boolean isElementExists(JsonObject obj, String jsonPath)
      Does the object contain an element at the specified JsonPath (even if null)?
    • isElementExists

      boolean isElementExists(JsonArray array, String jsonPath)
      Does the array contain an element at the specified JsonPath (even if null)?
    • enumToFriendlyJsonObject

      JsonObject enumToFriendlyJsonObject(Enum<?> enumValue)
      Convert the enum value to a JsonObject that has a ".name" property (the name() of the enum) and a ".label" property (the toString() of the enum)
    • enumsToFriendlyJsonArray

      JsonArray enumsToFriendlyJsonArray(Enum<?>[] enumValues)
      Convert the enums to an array of JsonObjects that have a ".name" property (the name() of the enum) and a ".label" property (the toString() of the enum).