Class JsonsFactory
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:
- Jackson 2 (if
com.fasterxml.jackson.databind.ObjectMapperis present) - Gson (if
com.google.gson.Gsonis present) - Jackson 1 (if
org.codehaus.jackson.map.ObjectMapperis 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 Summary
Modifier and TypeMethodDescriptionstatic JsonParserCreate the defaultJsonParserimplementation.static JsonSerializerCreate the defaultJsonSerializerimplementation.
-
Method Details
-
createDefaultParser
Create the defaultJsonParserimplementation.The resolution strategy first checks for SPI providers and then falls back to classpath auto-detection.
- Returns:
- the resolved
JsonParserimplementation - Throws:
UnsupportedOperationException- if no suitable implementation can be found
-
createDefaultSerializer
Create the defaultJsonSerializerimplementation.The resolution strategy first checks for SPI providers and then falls back to classpath auto-detection.
- Returns:
- the resolved
JsonSerializerimplementation - Throws:
UnsupportedOperationException- if no suitable implementation can be found
-