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):

PropertyEffect
johnzon.interfaceImplementationMappingMap<Class, Class> mapping interfaces to implementations for deserialization.
johnzon.use-big-decimal-for-objectUse BigDecimal for untyped numbers (Object); false adjusts the type to the number size. Default true.
johnzon.support-enum-container-deserializationAllow EnumMap/EnumSet instantiation. Default true.
johnzon.attributeOrderComparator instance sorting properties by name.
johnzon.deduplicateObjectsDeduplicate instances.
johnzon.supportsPrivateAccessUse private constructors/methods carrying @JsonbCreator.
johnzon.fail-on-unknown-propertiesFail the mapping on unmapped properties (like jsonb.fail-on-unknown-properties).
johnzon.readAttributeBeforeWriteRead collections before writing. This enables an “append” mode.
johnzon.autoAdjustBufferAuto-adjust internal read buffers.
johnzon.serialize-value-filterFilter to skip serializing some values.
johnzon.cdi.activatedToggle CDI support.
johnzon.accessModeCustom access mode; can disable some JSON-B features (annotation support).
johnzon.accessModeDelegateDelegate access mode used by JsonbAccessModel, enriching the default.
johnzon.failOnMissingCreatorValuesFail when a @JsonbCreator misses values.
johnzon.use-biginteger-stringadapterMap BigInteger as a string. Default true; set false for strict JSON-B 3 compliance.
johnzon.use-bigdecimal-stringadapterMap 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.