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
JsonMapper
for 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
@class
field containing fully qualified class or simple class name - Support for extending JSON files using a
@extends
field - 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.
DepositMoney
ormyapp.DepositMoney
)
@RegisterType
so that it can be resolved via the TypeRegistry
.
The Flux platform 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.JsonMapper
Preconfigured JsonMapper for reading/deserializing objects from JSON, including type handling.static com.fasterxml.jackson.databind.json.JsonMapper
Preconfigured 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 String
Converts an object to a JSON string.static String
asPrettyJson
(Object object) Converts an object to a formatted JSON string.static <T> T
convertValue
(Object fromValue, com.fasterxml.jackson.core.type.TypeReference<T> typeRef) Converts an object to an object of the given type.static <T> T
convertValue
(Object fromValue, Class<? extends T> toValueType) Converts an object to an object of the given class.static <T> T
convertValue
(Object fromValue, Type toValueType) Converts an object to an object of the given type which may be aParameterizedType
orClass
.static <T> T
convertValue
(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.Builder
Creates a preconfigured builder for a JsonMapper for writing/serializing objects to JSON.static void
disableJsonIgnore
(com.fasterxml.jackson.databind.ObjectMapper mapper) Disables any Jackson @JsonIgnore behavior on the specified ObjectMapper.static <T> T
Loads and deserializes a JSON file located relative to the reference point into an object.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.static <T> T
Loads and deserializes a JSON file located relative to the reference point into an object of the provided type.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.static Object
Loads 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> T
Loads and deserializes a JSON file located relative to the calling class to an object of the specified type.static <T> T
Loads and deserializes a JSON file located relative to the calling class to an object of the specified type.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.static <T> T
fromFileAs
(String fileName) Loads and deserializes a JSON file located relative to the calling class and casts it to typeJsonUtils
.static Object
fromFileWith
(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> T
fromJson
(byte[] json) Deserializes a JSON byte array and casts the result to typeJsonUtils
.static <T> T
fromJson
(byte[] json, com.fasterxml.jackson.databind.JavaType type) Deserializes a JSON byte array into an instance of the specified type.static <T> T
Deserializes a JSON byte array into an instance of the specified class type.static <T> T
Converts a JSON string to an object of the given type, which may be aParameterizedType
orClass
.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.static <T> T
static <T> T
Converts a JSON string to an object of the given type.static <T> T
Converts a JSON string to an object of the given class type.static <T> T
Converts a JSON string to an object of the given type, which may be aParameterizedType
orClass
.static <T> T
fromJson
(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 String
getContent
(Class<?> referencePoint, String fileName) protected static String
getContent
(URI fileUri) static <T> T
Merges a base object with a patch/update object, omitting null fields.static com.fasterxml.jackson.databind.node.ObjectNode
newObjectNode
(Object... keyValues) Constructs a newObjectNode
from alternating key-value arguments.static com.fasterxml.jackson.databind.JsonNode
readTree
(byte[] jsonContent) Reads a JSON structure as aJsonNode
tree from a JSON byte array.static com.fasterxml.jackson.databind.JsonNode
readTree
(InputStream jsonContent) Reads a JSON structure as aJsonNode
tree from a JSON input stream.static com.fasterxml.jackson.databind.JsonNode
Reads a JSON structure as aJsonNode
tree from a JSON string.static com.fasterxml.jackson.databind.type.TypeFactory
Provides access to the TypeFactory instance used by the writer.static <T extends com.fasterxml.jackson.databind.JsonNode>
TvalueToTree
(Object object) Converts an object to aJsonNode
of typeJsonUtils
.
-
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
Module
SPI (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
Module
SPI (ServiceLoader mechanism), which avoids overriding global configuration and ensures compatibility.Warning: This mapper is also used by the default serializer in Flux applications,
JacksonSerializer
. Misconfiguration may result in inconsistencies in search indexing or data loss. -
fromFile
Loads and deserializes a JSON file located relative to the calling class. Automatically supports@extends
inheritance for configuration reuse. -
fromFile
Loads and deserializes JSON files located relative to the calling class. Automatically supports@extends
inheritance for configuration reuse. -
fromFile
Loads and deserializes a JSON file located relative to the reference point into an object. Automatically supports@extends
inheritance for configuration reuse. -
getContent
-
getContent
-
fromFile
Loads and deserializes a JSON file located relative to the calling class to an object of the specified type. Automatically supports@extends
inheritance for configuration reuse. -
fromFile
Loads and deserializes a JSON file located relative to the calling class to an object of the specified type. Automatically supports@extends
inheritance 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@extends
inheritance for configuration reuse. -
fromFile
Loads and deserializes a JSON file located relative to the reference point into an object of the provided type. Automatically supports@extends
inheritance for configuration reuse. -
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@extends
inheritance 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@extends
inheritance for configuration reuse. -
fromFileAs
Loads and deserializes a JSON file located relative to the calling class and casts it to typeJsonUtils
. Automatically supports@extends
inheritance for configuration reuse. -
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
Converts a JSON string to an object of the given class type. -
fromJson
Converts a JSON string to an object of the given type, which may be aParameterizedType
orClass
. -
fromJson
Converts a JSON string to an object of the given type. -
fromJson
public static <T> T fromJson(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. -
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 typeJsonUtils
. -
fromJson
Converts a JSON string to an object of the given type, which may be aParameterizedType
orClass
. -
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
Converts an object to a JSON string. -
asPrettyJson
Converts an object to a formatted JSON string. -
asBytes
Converts an object to a JSON byte array. -
convertValue
Converts an object to an object of the given class. -
convertValue
Converts an object to an object of the given type which may be aParameterizedType
orClass
. -
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
public static <T> T convertValue(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. -
readTree
public static com.fasterxml.jackson.databind.JsonNode readTree(byte[] jsonContent) Reads a JSON structure as aJsonNode
tree from a JSON byte array. -
readTree
Reads a JSON structure as aJsonNode
tree from a JSON string. -
readTree
Reads a JSON structure as aJsonNode
tree from a JSON input stream. -
valueToTree
Converts an object to aJsonNode
of typeJsonUtils
. -
newObjectNode
Constructs a newObjectNode
from 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.
-