JSON-B
org.apache.johnzon:johnzon-jsonb:2.2.0
Contents
johnzon-jsonb implements the JSON-B 3.0 standard on top of the Johnzon
mapper, fully reusing JSON-B as its API:
try (Jsonb jsonb = JsonbBuilder.create()) {
String json = jsonb.toJson(myObject);
MyObject back = jsonb.fromJson(json, MyObject.class);
}Johnzon-specific configuration
Beyond the standard JsonbConfig properties, Johnzon wires its native configuration
through these keys (more in the JohnzonBuilder class):
| Property | Effect |
|---|---|
johnzon.interfaceImplementationMapping | Map<Class, Class> mapping interfaces to implementations for deserialization. |
johnzon.use-big-decimal-for-object | Use BigDecimal for untyped numbers (Object); false adjusts the type to the number size. Default true. |
johnzon.support-enum-container-deserialization | Allow EnumMap/EnumSet instantiation. Default true. |
johnzon.attributeOrder | Comparator instance sorting properties by name. |
johnzon.deduplicateObjects | Deduplicate instances. |
johnzon.supportsPrivateAccess | Use private constructors/methods carrying @JsonbCreator. |
johnzon.fail-on-unknown-properties | Fail the mapping on unmapped properties (like jsonb.fail-on-unknown-properties). |
johnzon.readAttributeBeforeWrite | Read collections before writing. This enables an “append” mode. |
johnzon.autoAdjustBuffer | Auto-adjust internal read buffers. |
johnzon.serialize-value-filter | Filter to skip serializing some values. |
johnzon.cdi.activated | Toggle CDI support. |
johnzon.accessMode | Custom access mode; can disable some JSON-B features (annotation support). |
johnzon.accessModeDelegate | Delegate access mode used by JsonbAccessModel, enriching the default. |
johnzon.failOnMissingCreatorValues | Fail when a @JsonbCreator misses values. |
johnzon.use-biginteger-stringadapter | Map BigInteger as a string. Default true; set false for strict JSON-B 3 compliance. |
johnzon.use-bigdecimal-stringadapter | Map BigDecimal as a string. Default true; set false for strict JSON-B 3 compliance. |
JAX-RS provider
The module also ships a JSON-B based JAX-RS provider,
org.apache.johnzon.jaxrs.jsonb.jaxrs.JsonbJaxrsProvider.
In JAX-RS 1.0 a provider could throw any exception for an empty incoming stream; JAX-RS
2.x requires a jakarta.ws.rs.core.NoContentException. To let you pick and to limit
breaking changes, the provider exposes throwNoContentExceptionOnEmptyStreams; the
default is derived from the available API. This behavior only works with
johnzon-core.
Integration with JsonValue
JsonValueReader and JsonValueWriter map a JsonValue to a POJO and back without any
I/O. Any Reader implementing Supplier<JsonStructure> or Writer implementing
Consumer<JsonValue> works:
final JsonValueReader<Simple> reader = new JsonValueReader<>(
Json.createObjectBuilder().add("value", "simple").build());
try (Jsonb jsonb = getJohnzonJsonb()) {
final Simple simple = jsonb.fromJson(reader, SomeModel.class);
}final JsonValueWriter writer = new JsonValueWriter();
try (Jsonb jsonb = getJohnzonJsonb()) {
jsonb.toJson(object, writer);
}
final JsonObject jsonObject = writer.getObject();As an experimental preview of a future specification version,
org.apache.johnzon.jsonb.api.experimental.JsonbExtension maps POJOs to JsonValue
and the opposite.