Class JsonSupport

java.lang.Object
net.ygbstudio.jbrave.core.utils.JsonSupport

public final class JsonSupport extends Object
JsonSupport provides JSON serialization and deserialization support using Jackson.

This class exists to reuse common JSON operations across different classes without code duplication. It also provides a pre-configured ObjectMapper instance for reuse, consistency and maximum performance gain.

Author:
Yoham Gabriel @ YGB Studio
  • Method Summary

    Modifier and Type
    Method
    Description
    static tools.jackson.databind.ObjectMapper
    Configures and returns a pre-configured ObjectMapper instance.
    static tools.jackson.databind.ObjectMapper
    Returns the configured ObjectMapper instance that is reused throughout the application.
    static <T> T
    jsonReader(Reader source, Class<T> clazz)
    Reads a JSON string from a Reader and deserializes it into an object of the specified class.
    static <T> T
    objectFromJson(String json, Class<T> clazz)
    Deserializes a JSON string into an object of the specified class.
    static <T> T
    readJsonFs(File jsonFile, Class<T> clazz)
    Reads a JSON file from the filesystem and deserializes it into an object of the specified class.
    static String
    Serializes an object into a JSON string.
    static <T> void
    writeJsonFs(File jsonFile, T obj)
    Serializes an object and writes it to a JSON file on the filesystem.

    Methods inherited from class java.lang.Object

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

    • configureMapper

      public static tools.jackson.databind.ObjectMapper configureMapper()
      Configures and returns a pre-configured ObjectMapper instance.

      This instance is configured to:

      • Indent output for better readability.
      • Fail on empty beans during serialization.
      • Ignore unknown properties during deserialization.

      The configured ObjectMapper is intended for reuse across different classes to ensure consistency and performance.

      Returns:
      a configured ObjectMapper instance
    • getMapper

      public static tools.jackson.databind.ObjectMapper getMapper()
      Returns the configured ObjectMapper instance that is reused throughout the application.
      Returns:
      the configured ObjectMapper instance
    • objectFromJson

      public static <T> T objectFromJson(String json, Class<T> clazz)
      Deserializes a JSON string into an object of the specified class.
      Type Parameters:
      T - the type of the object to deserialize into
      Parameters:
      json - the JSON string to deserialize
      clazz - the class of the object to deserialize into
      Returns:
      the deserialized object
    • toJsonString

      public static String toJsonString(Object obj)
      Serializes an object into a JSON string.
      Parameters:
      obj - the object to serialize
      Returns:
      the JSON string representation of the object
    • readJsonFs

      public static <T> T readJsonFs(File jsonFile, Class<T> clazz)
      Reads a JSON file from the filesystem and deserializes it into an object of the specified class.
      Type Parameters:
      T - the type of the object to deserialize into
      Parameters:
      jsonFile - the JSON file to read
      clazz - the class of the object to deserialize into
      Returns:
      the deserialized object
    • jsonReader

      public static <T> T jsonReader(Reader source, Class<T> clazz)
      Reads a JSON string from a Reader and deserializes it into an object of the specified class.
      Type Parameters:
      T - the type of the object to deserialize into
      Parameters:
      source - reader instance for the JSON string to read from
      clazz - the class of the object to deserialize into
      Returns:
      the deserialized object
    • writeJsonFs

      public static <T> void writeJsonFs(File jsonFile, T obj)
      Serializes an object and writes it to a JSON file on the filesystem.
      Type Parameters:
      T - the type of the object to serialize
      Parameters:
      jsonFile - the JSON file to write to
      obj - the object to serialize