Class JsonProvider

java.lang.Object
dev.polv.taleapi.config.json.JsonProvider
All Implemented Interfaces:
ConfigProvider

public class JsonProvider extends Object implements 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:
  • 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

      public <T> T deserialize(Reader reader, Class<T> type) throws ConfigException
      Description copied from interface: ConfigProvider
      Deserializes configuration data from a reader into an object of the specified type.
      Specified by:
      deserialize in interface ConfigProvider
      Type Parameters:
      T - the type of the configuration object
      Parameters:
      reader - the reader to read configuration data from
      type - the class of the object to deserialize into
      Returns:
      the deserialized configuration object
      Throws:
      ConfigException - if deserialization fails
    • deserializeToNode

      public ConfigNode deserializeToNode(Reader reader) throws ConfigException
      Description copied from interface: ConfigProvider
      Deserializes configuration data from a reader into a traversable ConfigNode.

      Use this when you want dynamic access to configuration values without defining a specific class structure.

      Specified by:
      deserializeToNode in interface ConfigProvider
      Parameters:
      reader - the reader to read configuration data from
      Returns:
      the configuration as a traversable node
      Throws:
      ConfigException - if deserialization fails
    • serialize

      public <T> void serialize(Writer writer, T value) throws ConfigException
      Description copied from interface: ConfigProvider
      Serializes a configuration object and writes it to the given writer.
      Specified by:
      serialize in interface ConfigProvider
      Type Parameters:
      T - the type of the configuration object
      Parameters:
      writer - the writer to write the serialized data to
      value - the configuration object to serialize
      Throws:
      ConfigException - if serialization fails
    • serialize

      public void serialize(Writer writer, ConfigNode node) throws ConfigException
      Description copied from interface: ConfigProvider
      Serializes a ConfigNode and writes it to the given writer.
      Specified by:
      serialize in interface ConfigProvider
      Parameters:
      writer - the writer to write the serialized data to
      node - the configuration node to serialize
      Throws:
      ConfigException - if serialization fails
    • fileExtension

      public String fileExtension()
      Description copied from interface: ConfigProvider
      Returns the file extension associated with this provider's format.

      The extension should not include the leading dot (e.g., "json" not ".json").

      Specified by:
      fileExtension in interface ConfigProvider
      Returns:
      the file extension for this format
    • formatName

      public String formatName()
      Description copied from interface: ConfigProvider
      Returns a human-readable name for this format.
      Specified by:
      formatName in interface ConfigProvider
      Returns:
      the format name (e.g., "JSON", "YAML")
    • builder

      public static JsonProvider.Builder builder()
      Creates a builder for configuring a JsonProvider.
      Returns:
      a new builder instance