JSON Binding support for Quarkus applications using Eclipse Yasson implementation
npx @tessl/cli install tessl/maven-io-quarkus--quarkus-jsonb@3.23.0The Quarkus JSON-B extension provides comprehensive JSON-B (JSON Binding) support for Java applications built with the Quarkus framework. It integrates Eclipse Yasson as the underlying JSON-B implementation and enables automatic serialization and deserialization of Java objects to and from JSON format with full CDI integration.
Maven:
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-jsonb</artifactId>
</dependency>Gradle:
implementation 'io.quarkus:quarkus-jsonb'import jakarta.json.bind.Jsonb;
import jakarta.json.bind.JsonbConfig;
import jakarta.json.bind.config.PropertyNamingStrategy;
import io.quarkus.jsonb.JsonbConfigCustomizer;For SPI usage (extension developers):
import io.quarkus.jsonb.spi.JsonbSerializerBuildItem;
import io.quarkus.jsonb.spi.JsonbDeserializerBuildItem;import jakarta.inject.Inject;
import jakarta.json.bind.Jsonb;
@ApplicationScoped
public class JsonService {
@Inject
Jsonb jsonb;
public String serialize(Object obj) {
return jsonb.toJson(obj);
}
public <T> T deserialize(String json, Class<T> type) {
return jsonb.fromJson(json, type);
}
}The extension automatically provides CDI beans for JSON-B operations.
@Inject
Jsonb jsonb;The Jsonb bean is available for injection and configured with any custom JsonbConfigCustomizer beans.
@Inject
JsonbConfig jsonbConfig;The JsonbConfig bean is also available for injection (Dependent scope).
Customize JSON-B configuration by implementing JsonbConfigCustomizer.
public interface JsonbConfigCustomizer {
void customize(JsonbConfig jsonbConfig);
}Example implementation:
@Singleton
public class MyJsonbCustomizer implements JsonbConfigCustomizer {
@Override
public void customize(JsonbConfig config) {
config.withFormatting(true)
.withNullValues(false)
.withPropertyNamingStrategy(PropertyNamingStrategy.LOWER_CASE_WITH_UNDERSCORES);
}
}The extension provides CDI-aware component instance creation for JSON-B components.
public class QuarkusJsonbComponentInstanceCreator implements JsonbComponentInstanceCreator {
public QuarkusJsonbComponentInstanceCreator();
public void close() throws IOException;
public <T> T getOrCreateComponent(Class<T> componentClass);
}This class automatically integrates with Quarkus CDI to create JSON-B serializers, deserializers, and adapters as CDI beans when possible.
JSON-B adapters are automatically discovered and registered when they are CDI beans.
@Singleton
public class InstantAdapter implements JsonbAdapter<Instant, Long> {
@Override
public Long adaptToJson(Instant instant) {
return instant.getEpochSecond();
}
@Override
public Instant adaptFromJson(Long epochSecond) {
return Instant.ofEpochSecond(epochSecond);
}
}The extension provides build items for registering custom serializers and deserializers.
public final class JsonbSerializerBuildItem extends MultiBuildItem {
public JsonbSerializerBuildItem(String serializerClassName);
public JsonbSerializerBuildItem(Collection<String> serializerClassNames);
public Collection<String> getSerializerClassNames();
}public final class JsonbDeserializerBuildItem extends MultiBuildItem {
public JsonbDeserializerBuildItem(String deserializerClassName);
public JsonbDeserializerBuildItem(Collection<String> deserializerClassNames);
public Collection<String> getDeserializerClassNames();
}Example usage in an extension deployment class:
@BuildStep
void registerCustomSerializers(BuildProducer<JsonbSerializerBuildItem> serializers) {
serializers.produce(new JsonbSerializerBuildItem(MyCustomSerializer.class.getName()));
}// Main producer class providing JSON-B beans
@Singleton
public class JsonbProducer {
@Produces @Dependent @DefaultBean
public JsonbConfig jsonbConfig(@All List<JsonbConfigCustomizer> customizers);
@Produces @Singleton @DefaultBean
public Jsonb jsonb(JsonbConfig jsonbConfig);
}// Interface for customizing JSON-B configuration
public interface JsonbConfigCustomizer {
void customize(JsonbConfig jsonbConfig);
}// CDI-aware component instance creator
public class QuarkusJsonbComponentInstanceCreator implements JsonbComponentInstanceCreator {
public QuarkusJsonbComponentInstanceCreator();
public void close() throws IOException;
public <T> T getOrCreateComponent(Class<T> componentClass);
}// Build items for extension developers
public final class JsonbSerializerBuildItem extends MultiBuildItem {
public JsonbSerializerBuildItem(String serializerClassName);
public JsonbSerializerBuildItem(Collection<String> serializerClassNames);
public Collection<String> getSerializerClassNames();
}
public final class JsonbDeserializerBuildItem extends MultiBuildItem {
public JsonbDeserializerBuildItem(String deserializerClassName);
public JsonbDeserializerBuildItem(Collection<String> deserializerClassNames);
public Collection<String> getDeserializerClassNames();
}The extension automatically includes and integrates with these components:
// Required dependencies (automatically included):
// - io.quarkus:quarkus-jsonp (JSON Processing support)
// - org.eclipse:yasson (Eclipse Yasson JSON-B implementation)
// - io.quarkus:quarkus-arc (CDI support)When you add quarkus-jsonb to your project, these dependencies are automatically managed.
The extension operates through several key components:
The extension automatically:
Custom serializers and deserializers must have public no-argument constructors. If CDI bean creation fails, the extension falls back to direct instantiation via reflection.
The extension throws IllegalStateException if JSON-B component instantiation fails during fallback creation.