Class JsonsFactory

java.lang.Object
com.github.mjeanroy.dbunit.json.JsonsFactory

public final class JsonsFactory extends Object
Factory used to create default JsonParser and JsonSerializer implementations.

This factory resolves implementations using the following strategy:

1. Service Provider Interface (SPI)

The factory first attempts to discover user-provided implementations via ServiceLoader. If a provider is found for the requested type (JsonParser or JsonSerializer), it is returned immediately. This allows applications to override the default JSON implementation without modifying the library configuration.

2. Classpath Auto-Detection

If no SPI provider is found, the factory falls back to classpath detection. The first available implementation in the following order is selected:

  1. Jackson 2 (if com.fasterxml.jackson.databind.ObjectMapper is present)
  2. Gson (if com.google.gson.Gson is present)
  3. Jackson 1 (if org.codehaus.jackson.map.ObjectMapper is present)

Availability is determined using classpath inspection at runtime.

Failure Behavior

If no SPI implementation is found and none of the supported JSON libraries are available on the classpath, an UnsupportedOperationException is thrown.

Thread-Safety

This factory is stateless and thread-safe.

Design Notes

Implementations are selected dynamically to avoid introducing a mandatory dependency on a specific JSON library. This enables flexible integration depending on the application's classpath configuration.

  • Method Details

    • createDefaultParser

      public static JsonParser createDefaultParser()
      Create the default JsonParser implementation.

      The resolution strategy first checks for SPI providers and then falls back to classpath auto-detection.

      Returns:
      the resolved JsonParser implementation
      Throws:
      UnsupportedOperationException - if no suitable implementation can be found
    • createDefaultSerializer

      public static JsonSerializer createDefaultSerializer()
      Create the default JsonSerializer implementation.

      The resolution strategy first checks for SPI providers and then falls back to classpath auto-detection.

      Returns:
      the resolved JsonSerializer implementation
      Throws:
      UnsupportedOperationException - if no suitable implementation can be found