Class JsonSerializationHelper
java.lang.Object
de.cuioss.benchmarking.common.util.JsonSerializationHelper
Common JSON serialization utilities for benchmark results.
Provides consistent formatting and serialization across all benchmark modules.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final com.google.gson.GsonCompact Gson instance for minimal output (no pretty printing).static final com.google.gson.GsonGson instance configured for benchmark result serialization. -
Method Summary
Modifier and TypeMethodDescriptionstatic StringformatDouble(double value) Formats a double value for display.static <T> TDeserializes a JSON string to a generic type using TypeToken.static <T> TDeserializes a JSON string to an object.jsonToListOfMaps(String json) Convenience method for deserializing JSON to List<Map<String, Object>>.Convenience method for deserializing JSON to Map<String, Object>.static <T> TreadJsonFile(Path path, com.google.gson.reflect.TypeToken<T> typeToken) Reads a JSON file into a generic type using TypeToken.static <T> TreadJsonFile(Path path, Class<T> type) Reads a JSON file into an object.static StringtoCompactJson(Object object) Serializes an object to compact JSON string (no pretty printing).static StringSerializes an object to JSON string using the default GSON instance.static com.google.gson.JsonElementtoJsonTree(Object object) Creates a JsonElement from an object using the default GSON instance.static voidwriteJsonFile(Path path, Object object) Writes an object to a JSON file.
-
Field Details
-
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
Compact Gson instance for minimal output (no pretty printing). Useful for reducing JSON size in performance-critical situations.
-
-
Method Details
-
writeJsonFile
Writes an object to a JSON file.- Parameters:
path- the file path to write toobject- the object to serialize- Throws:
IOException- if an I/O error occurs
-
readJsonFile
Reads a JSON file into an object.- Type Parameters:
T- the type to deserialize to- Parameters:
path- the file path to read fromtype- 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 fromtypeToken- the TypeToken representing the generic type- Returns:
- the deserialized object
- Throws:
IOException- if an I/O error occurs
-
toJson
Serializes an object to JSON string using the default GSON instance.- Parameters:
object- the object to serialize- Returns:
- the JSON string representation
-
toCompactJson
Serializes an object to compact JSON string (no pretty printing).- Parameters:
object- the object to serialize- Returns:
- the compact JSON string representation
-
fromJson
Deserializes a JSON string to an object.- Type Parameters:
T- the type to deserialize to- Parameters:
json- the JSON stringtype- the class of the type to deserialize- Returns:
- the deserialized object
-
fromJson
Deserializes a JSON string to a generic type using TypeToken.- Type Parameters:
T- the type to deserialize to- Parameters:
json- the JSON stringtypeToken- the TypeToken representing the generic type- Returns:
- the deserialized object
-
jsonToMap
Convenience method for deserializing JSON to Map<String, Object>.- Parameters:
json- the JSON string- Returns:
- the deserialized map
-
jsonToListOfMaps
Convenience method for deserializing JSON to List<Map<String, Object>>.- Parameters:
json- the JSON string- Returns:
- the deserialized list of maps
-
toJsonTree
Creates a JsonElement from an object using the default GSON instance.- Parameters:
object- the object to convert- Returns:
- the JsonElement representation
-
formatDouble
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
-