Class JsonSerializationHelper

java.lang.Object
de.cuioss.benchmarking.common.util.JsonSerializationHelper

public final class JsonSerializationHelper extends Object
Common JSON serialization utilities for benchmark results. Provides consistent formatting and serialization across all benchmark modules.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final com.google.gson.Gson
    Compact Gson instance for minimal output (no pretty printing).
    static final com.google.gson.Gson
    Gson instance configured for benchmark result serialization.
  • Method Summary

    Modifier and Type
    Method
    Description
    static String
    formatDouble(double value)
    Formats a double value for display.
    static <T> T
    fromJson(String json, com.google.gson.reflect.TypeToken<T> typeToken)
    Deserializes a JSON string to a generic type using TypeToken.
    static <T> T
    fromJson(String json, Class<T> type)
    Deserializes a JSON string to an object.
    Convenience method for deserializing JSON to List<Map<String, Object>>.
    static Map<String,Object>
    Convenience method for deserializing JSON to Map<String, Object>.
    static <T> T
    readJsonFile(Path path, com.google.gson.reflect.TypeToken<T> typeToken)
    Reads a JSON file into a generic type using TypeToken.
    static <T> T
    readJsonFile(Path path, Class<T> type)
    Reads a JSON file into an object.
    static String
    Serializes an object to compact JSON string (no pretty printing).
    static String
    toJson(Object object)
    Serializes an object to JSON string using the default GSON instance.
    static com.google.gson.JsonElement
    Creates a JsonElement from an object using the default GSON instance.
    static void
    writeJsonFile(Path path, Object object)
    Writes an object to a JSON file.

    Methods inherited from class java.lang.Object

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

    • GSON

      public static final com.google.gson.Gson GSON
      Gson instance configured for benchmark result serialization. Features: - Pretty printing - Smart number formatting (integers without .0) - ISO instant formatting - Special floating point value serialization - Thread-safe singleton
    • COMPACT_GSON

      public static final com.google.gson.Gson COMPACT_GSON
      Compact Gson instance for minimal output (no pretty printing). Useful for reducing JSON size in performance-critical situations.
  • Method Details

    • writeJsonFile

      public static void writeJsonFile(Path path, Object object) throws IOException
      Writes an object to a JSON file.
      Parameters:
      path - the file path to write to
      object - the object to serialize
      Throws:
      IOException - if an I/O error occurs
    • readJsonFile

      public static <T> T readJsonFile(Path path, Class<T> type) throws IOException
      Reads a JSON file into an object.
      Type Parameters:
      T - the type to deserialize to
      Parameters:
      path - the file path to read from
      type - the class of the type to deserialize
      Returns:
      the deserialized object
      Throws:
      IOException - if an I/O error occurs
    • readJsonFile

      public static <T> T readJsonFile(Path path, com.google.gson.reflect.TypeToken<T> typeToken) throws IOException
      Reads a JSON file into a generic type using TypeToken. Useful for deserializing collections and generic types.
      Type Parameters:
      T - the type to deserialize to
      Parameters:
      path - the file path to read from
      typeToken - the TypeToken representing the generic type
      Returns:
      the deserialized object
      Throws:
      IOException - if an I/O error occurs
    • toJson

      public static String toJson(Object object)
      Serializes an object to JSON string using the default GSON instance.
      Parameters:
      object - the object to serialize
      Returns:
      the JSON string representation
    • toCompactJson

      public static String toCompactJson(Object object)
      Serializes an object to compact JSON string (no pretty printing).
      Parameters:
      object - the object to serialize
      Returns:
      the compact JSON string representation
    • fromJson

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

      public static <T> T fromJson(String json, com.google.gson.reflect.TypeToken<T> typeToken)
      Deserializes a JSON string to a generic type using TypeToken.
      Type Parameters:
      T - the type to deserialize to
      Parameters:
      json - the JSON string
      typeToken - the TypeToken representing the generic type
      Returns:
      the deserialized object
    • jsonToMap

      public static Map<String,Object> jsonToMap(String json)
      Convenience method for deserializing JSON to Map<String, Object>.
      Parameters:
      json - the JSON string
      Returns:
      the deserialized map
    • jsonToListOfMaps

      public static List<Map<String,Object>> jsonToListOfMaps(String json)
      Convenience method for deserializing JSON to List<Map<String, Object>>.
      Parameters:
      json - the JSON string
      Returns:
      the deserialized list of maps
    • toJsonTree

      public static com.google.gson.JsonElement toJsonTree(Object object)
      Creates a JsonElement from an object using the default GSON instance.
      Parameters:
      object - the object to convert
      Returns:
      the JsonElement representation
    • formatDouble

      public static String formatDouble(double value)
      Formats a double value for display. Returns integer representation if the value is a whole number.
      Parameters:
      value - the value to format
      Returns:
      formatted string