Package dev.polv.taleapi.config.json
Class JsonProvider
java.lang.Object
dev.polv.taleapi.config.json.JsonProvider
- All Implemented Interfaces:
ConfigProvider
A
ConfigProvider implementation for JSON format using Jackson.
This provider supports both object mapping and dynamic node traversal. It uses Jackson with sensible defaults including pretty printing.
Example Usage
// Create with default settings (pretty printing enabled)
JsonProvider provider = new JsonProvider();
// Create with custom ObjectMapper
ObjectMapper customMapper = new ObjectMapper()
.enable(SerializationFeature.INDENT_OUTPUT)
.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
JsonProvider customProvider = new JsonProvider(customMapper);
// Use with ConfigLoader
ConfigLoader loader = new ConfigLoader(provider);
Supported Types
JsonProvider supports all types that Jackson can serialize:
- Primitives and their wrappers
- Strings
- Arrays and Collections
- Maps (with String keys)
- POJOs with public fields or getters/setters
- Records (Java 14+)
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classBuilder for creating customized JsonProvider instances. -
Constructor Summary
ConstructorsConstructorDescriptionCreates a new JsonProvider with default Jackson settings.JsonProvider(com.fasterxml.jackson.databind.ObjectMapper mapper) Creates a new JsonProvider with a custom ObjectMapper. -
Method Summary
Modifier and TypeMethodDescriptionstatic JsonProvider.Builderbuilder()Creates a builder for configuring a JsonProvider.<T> Tdeserialize(Reader reader, Class<T> type) Deserializes configuration data from a reader into an object of the specified type.deserializeToNode(Reader reader) Deserializes configuration data from a reader into a traversableConfigNode.Returns the file extension associated with this provider's format.Returns a human-readable name for this format.com.fasterxml.jackson.databind.ObjectMapperReturns the ObjectMapper used by this provider.voidserialize(Writer writer, ConfigNode node) Serializes aConfigNodeand writes it to the given writer.<T> voidSerializes a configuration object and writes it to the given writer.
-
Constructor Details
-
JsonProvider
public JsonProvider()Creates a new JsonProvider with default Jackson settings.Default settings include:
- Pretty printing enabled
- Unknown properties ignored during deserialization
-
JsonProvider
public JsonProvider(com.fasterxml.jackson.databind.ObjectMapper mapper) Creates a new JsonProvider with a custom ObjectMapper.- Parameters:
mapper- the ObjectMapper to use for serialization- Throws:
NullPointerException- if mapper is null
-
-
Method Details
-
getMapper
public com.fasterxml.jackson.databind.ObjectMapper getMapper()Returns the ObjectMapper used by this provider.- Returns:
- the ObjectMapper
-
deserialize
Description copied from interface:ConfigProviderDeserializes configuration data from a reader into an object of the specified type.- Specified by:
deserializein interfaceConfigProvider- Type Parameters:
T- the type of the configuration object- Parameters:
reader- the reader to read configuration data fromtype- the class of the object to deserialize into- Returns:
- the deserialized configuration object
- Throws:
ConfigException- if deserialization fails
-
deserializeToNode
Description copied from interface:ConfigProviderDeserializes configuration data from a reader into a traversableConfigNode.Use this when you want dynamic access to configuration values without defining a specific class structure.
- Specified by:
deserializeToNodein interfaceConfigProvider- Parameters:
reader- the reader to read configuration data from- Returns:
- the configuration as a traversable node
- Throws:
ConfigException- if deserialization fails
-
serialize
Description copied from interface:ConfigProviderSerializes a configuration object and writes it to the given writer.- Specified by:
serializein interfaceConfigProvider- Type Parameters:
T- the type of the configuration object- Parameters:
writer- the writer to write the serialized data tovalue- the configuration object to serialize- Throws:
ConfigException- if serialization fails
-
serialize
Description copied from interface:ConfigProviderSerializes aConfigNodeand writes it to the given writer.- Specified by:
serializein interfaceConfigProvider- Parameters:
writer- the writer to write the serialized data tonode- the configuration node to serialize- Throws:
ConfigException- if serialization fails
-
fileExtension
Description copied from interface:ConfigProviderReturns the file extension associated with this provider's format.The extension should not include the leading dot (e.g., "json" not ".json").
- Specified by:
fileExtensionin interfaceConfigProvider- Returns:
- the file extension for this format
-
formatName
Description copied from interface:ConfigProviderReturns a human-readable name for this format.- Specified by:
formatNamein interfaceConfigProvider- Returns:
- the format name (e.g., "JSON", "YAML")
-
builder
Creates a builder for configuring a JsonProvider.- Returns:
- a new builder instance
-