Class JsonUtils
JsonUtils wraps around Jackson's JsonMapper to provide enhanced handling of JSON processing. It
supports dynamic type handling, tree merging, custom file loading with `@extends` inheritance, and robust conversion
between types and structures.
Key Features
- Configurable
JsonMapperfor serialization (writer) and deserialization (reader) - Convenience methods for reading from resource files, merging JSON trees, and converting between types
- Support for deserializing JSON using a
@classfield containing fully qualified class or simple class name - Support for extending JSON files using a
@extendsfield - Handles file and URI-based input
Example Usage
MyClass object = JsonUtils.fromFile("config.json", MyClass.class);
String json = JsonUtils.asJson(object);
Type Resolution via @class
When deserializing JSON using generic methods likefromFile(String) or fromJson(String) that
don't specify a target type, a @class attribute is required in the JSON to indicate the target Java type.
This type can be either:
- A fully qualified class name (e.g.
com.myapp.model.DepositMoney) - A short or partially qualified name (e.g.
DepositMoneyormyapp.DepositMoney)
@RegisterType
so that it can be resolved via the TypeRegistry.
The Fluxzero Runtime uses this resolution mechanism internally to support extensibility and polymorphic message deserialization.
File Inheritance via @extends
JSON files can extend other JSON files using the@extends attribute. This allows the reuse of base
configurations or data structures with overridden values in the extending file.
Example:
// deposit-to-userA.json
{
"@class": "DepositMoney",
"amount": 23,
"recipient": "userA"
}
// deposit-to-userB.json
{
"@extends": "deposit-to-userA.json",
"recipient": "userB"
}
When deposit-to-userB.json is loaded, it will inherit all fields from deposit-to-userA.json
and override the recipient field with "userB".
The inheritance is recursive and is applied before deserialization.
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic com.fasterxml.jackson.databind.json.JsonMapperPreconfigured JsonMapper for reading/deserializing objects from JSON, including type handling.static com.fasterxml.jackson.databind.json.JsonMapperPreconfigured JsonMapper for writing/serializing objects to JSON. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic byte[]Converts an object to a JSON byte array.static StringConverts an object to a JSON string.static StringasPrettyJson(Object object) Converts an object to a formatted JSON string.static <T> TconvertValue(Object fromValue, com.fasterxml.jackson.core.type.TypeReference<T> typeRef) Converts an object to an object of the given type.static <T> TconvertValue(Object fromValue, Class<? extends T> toValueType) Converts an object to an object of the given class.static <T> TconvertValue(Object fromValue, Type toValueType) Converts an object to an object of the given type which may be aParameterizedTypeorClass.static <T> TconvertValue(Object fromValue, Function<com.fasterxml.jackson.databind.type.TypeFactory, com.fasterxml.jackson.databind.JavaType> typeFunction) Converts an object to an object of the given type.static com.fasterxml.jackson.databind.json.JsonMapper.BuilderCreates a preconfigured builder for a JsonMapper for writing/serializing objects to JSON.static voiddisableJsonIgnore(com.fasterxml.jackson.databind.ObjectMapper mapper) Disables any Jackson @JsonIgnore behavior on the specified ObjectMapper.static <T> TLoads and deserializes a JSON file located relative to the reference point into an object.static <T> TfromFile(Class<?> referencePoint, String fileName, com.fasterxml.jackson.databind.JavaType javaType) Loads and deserializes a JSON file located relative to the reference point into an object of the provided type.static <T> TLoads and deserializes a JSON file located relative to the reference point into an object of the provided type.static <T> TfromFile(Class<?> referencePoint, String fileName, Function<com.fasterxml.jackson.databind.type.TypeFactory, com.fasterxml.jackson.databind.JavaType> typeFunction) Loads and deserializes a JSON file located relative to the referencePoint into an object of the type specified by the provided type function.static ObjectLoads and deserializes a JSON file located relative to the calling class.static List<?> Loads and deserializes JSON files located relative to the calling class.static <T> TLoads and deserializes a JSON file located relative to the calling class to an object of the specified type.static <T> TLoads and deserializes a JSON file located relative to the calling class to an object of the specified type.static <T> TfromFile(String fileName, Function<com.fasterxml.jackson.databind.type.TypeFactory, com.fasterxml.jackson.databind.JavaType> typeFunction) Loads and deserializes a JSON file located relative to the calling class into an object of the type specified by the provided type function.static <T> TfromFileAs(String fileName) Loads and deserializes a JSON file located relative to the calling class and casts it to typeT.static ObjectfromFileWith(String fileName, Map<String, Object> replaceValues) Loads and deserializes a JSON file located relative to the calling class, applies property replacements, and returns the resulting object.static <T> TfromJson(byte[] json) Deserializes a JSON byte array and casts the result to typeT.static <T> TfromJson(byte[] json, com.fasterxml.jackson.databind.JavaType type) Deserializes a JSON byte array into an instance of the specified type.static <T> TDeserializes a JSON byte array into an instance of the specified class type.static <T> TConverts a JSON string to an object of the given type, which may be aParameterizedTypeorClass.static <T> TfromJson(byte[] json, Function<com.fasterxml.jackson.databind.type.TypeFactory, com.fasterxml.jackson.databind.JavaType> typeFunction) Deserializes a JSON byte array into an object of the type specified by the provided type function.static <T> Tstatic <T> TConverts a JSON string to an object of the given type.static <T> TConverts a JSON string to an object of the given class type.static <T> TConverts a JSON string to an object of the given type, which may be aParameterizedTypeorClass.static <T> TfromJson(String json, Function<com.fasterxml.jackson.databind.type.TypeFactory, com.fasterxml.jackson.databind.JavaType> typeFunction) Deserializes a JSON string into an object of the type specified by the provided type function.protected static StringgetContent(Class<?> referencePoint, String fileName) protected static StringgetContent(URI fileUri) static <T> TMerges a base object with a patch/update object, omitting null fields.static com.fasterxml.jackson.databind.node.ObjectNodenewObjectNode(Object... keyValues) Constructs a newObjectNodefrom alternating key-value arguments.static com.fasterxml.jackson.databind.JsonNodereadTree(byte[] jsonContent) Reads a JSON structure as aJsonNodetree from a JSON byte array.static com.fasterxml.jackson.databind.JsonNodereadTree(InputStream jsonContent) Reads a JSON structure as aJsonNodetree from a JSON input stream.static com.fasterxml.jackson.databind.JsonNodeReads a JSON structure as aJsonNodetree from a JSON string.static com.fasterxml.jackson.databind.type.TypeFactoryProvides access to the TypeFactory instance used by the writer.static <T extends com.fasterxml.jackson.databind.JsonNode>
TvalueToTree(Object object) Converts an object to aJsonNodeof typeT.
-
Field Details
-
writer
public static com.fasterxml.jackson.databind.json.JsonMapper writerPreconfigured JsonMapper for writing/serializing objects to JSON. By default, it is created usingdefaultWriterBuilder().In advanced scenarios, users may replace this field with a custom
JsonMapper. However, this is generally discouraged unless strictly necessary. -
reader
public static com.fasterxml.jackson.databind.json.JsonMapper readerPreconfigured JsonMapper for reading/deserializing objects from JSON, including type handling.In advanced scenarios, users may replace this field with a custom
JsonMapper. However, this is generally discouraged unless strictly necessary.A better approach for customizing Jackson behavior is to provide your own modules via the Jackson
ModuleSPI (ServiceLoader mechanism), which avoids overriding global configuration and ensures compatibility.
-
-
Constructor Details
-
JsonUtils
public JsonUtils()
-
-
Method Details
-
defaultWriterBuilder
public static com.fasterxml.jackson.databind.json.JsonMapper.Builder defaultWriterBuilder()Creates a preconfigured builder for a JsonMapper for writing/serializing objects to JSON.A better approach for customizing Jackson behavior is to provide your own modules via the Jackson
ModuleSPI (ServiceLoader mechanism), which avoids overriding global configuration and ensures compatibility.Warning: This mapper is also used by the default serializer in Fluxzero applications,
JacksonSerializer. Misconfiguration may result in inconsistencies in search indexing or data loss. -
fromFile
-
fromFile
-
fromFile
-
getContent
-
getContent
-
fromFile
-
fromFile
Loads and deserializes a JSON file located relative to the calling class to an object of the specified type. Automatically supports@extendsinheritance for configuration reuse. -
fromFile
public static <T> T fromFile(String fileName, Function<com.fasterxml.jackson.databind.type.TypeFactory, com.fasterxml.jackson.databind.JavaType> typeFunction) Loads and deserializes a JSON file located relative to the calling class into an object of the type specified by the provided type function. Automatically supports@extendsinheritance for configuration reuse. -
fromFile
-
fromFile
public static <T> T fromFile(Class<?> referencePoint, String fileName, com.fasterxml.jackson.databind.JavaType javaType) Loads and deserializes a JSON file located relative to the reference point into an object of the provided type. Automatically supports@extendsinheritance for configuration reuse. -
fromFile
public static <T> T fromFile(Class<?> referencePoint, String fileName, Function<com.fasterxml.jackson.databind.type.TypeFactory, com.fasterxml.jackson.databind.JavaType> typeFunction) Loads and deserializes a JSON file located relative to the referencePoint into an object of the type specified by the provided type function. Automatically supports@extendsinheritance for configuration reuse. -
fromFileAs
-
fromFileWith
Loads and deserializes a JSON file located relative to the calling class, applies property replacements, and returns the resulting object. The method supports updating specific properties of the deserialized object using the provided replacement values. -
fromJson
-
fromJson
-
fromJson
Converts a JSON string to an object of the given type, which may be aParameterizedTypeorClass. -
fromJson
Converts a JSON string to an object of the given type. -
fromJson
-
fromJson
Deserializes a JSON byte array into an instance of the specified class type. -
fromJson
public static <T> T fromJson(byte[] json) Deserializes a JSON byte array and casts the result to typeT. -
fromJson
Converts a JSON string to an object of the given type, which may be aParameterizedTypeorClass. -
fromJson
public static <T> T fromJson(byte[] json, com.fasterxml.jackson.databind.JavaType type) Deserializes a JSON byte array into an instance of the specified type. -
fromJson
public static <T> T fromJson(byte[] json, Function<com.fasterxml.jackson.databind.type.TypeFactory, com.fasterxml.jackson.databind.JavaType> typeFunction) Deserializes a JSON byte array into an object of the type specified by the provided type function. -
asJson
-
asPrettyJson
-
asBytes
Converts an object to a JSON byte array. -
convertValue
-
convertValue
Converts an object to an object of the given type which may be aParameterizedTypeorClass. -
convertValue
public static <T> T convertValue(Object fromValue, com.fasterxml.jackson.core.type.TypeReference<T> typeRef) Converts an object to an object of the given type. -
convertValue
-
readTree
public static com.fasterxml.jackson.databind.JsonNode readTree(byte[] jsonContent) Reads a JSON structure as aJsonNodetree from a JSON byte array. -
readTree
Reads a JSON structure as aJsonNodetree from a JSON string. -
readTree
Reads a JSON structure as aJsonNodetree from a JSON input stream. -
valueToTree
-
newObjectNode
Constructs a newObjectNodefrom alternating key-value arguments. -
merge
Merges a base object with a patch/update object, omitting null fields. Returns a new object of the same type with updated fields. -
typeFactory
public static com.fasterxml.jackson.databind.type.TypeFactory typeFactory()Provides access to the TypeFactory instance used by the writer. -
disableJsonIgnore
public static void disableJsonIgnore(com.fasterxml.jackson.databind.ObjectMapper mapper) Disables any Jackson @JsonIgnore behavior on the specified ObjectMapper.
-