Class JacksonSerializerFactory
java.lang.Object
io.quarkus.resteasy.reactive.jackson.deployment.processor.JacksonCodeGenerator
io.quarkus.resteasy.reactive.jackson.deployment.processor.JacksonSerializerFactory
Generates an implementation of the Jackson's
StdSerializer for each class that needs to be serialized in json.
In this way the serialization process can be performed through the ad-hoc generate serializer and then without
any use of reflection. For instance for a pojo like this
public class Person {
private String firstName;
@JsonProperty("familyName")
private String lastName;
private int age;
@SecureField(rolesAllowed = "admin")
@JsonUnwrapped
private Address address;
@JsonView(Private.class)
public int id = 0;
public Person() {
}
public Person(String firstName, String lastName, int age, Address address) {
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
this.address = address;
}
// getters and setters omitted
}
it generates the following StdSerializer implementation
public class Person$quarkusjacksonserializer extends GeneratedSerializer {
static final String[] address_ROLES_ALLOWED = new String[] { "admin" };
static final Class[] id_VIEW_CLASSES = new Class[] { Private.class };
public Person$quarkusjacksonserializer() {
super(Person.class);
}
public void serializeContent(Object object, JsonGenerator jsonGenerator, SerializerProvider serializerProvider)
throws IOException {
Person person = (Person) object;
SerializationInclude serializationInclude = SerializationInclude.decode(object, serializerProvider);
Class activeView = serializerProvider.getActiveView();
PropertyNamingStrategy propertyNamingStrategy = serializerProvider.getConfig().getPropertyNamingStrategy();
jsonGenerator.writeStartObject();
if (JacksonMapperUtil.includeSecureField(serializerProvider, address_ROLES_ALLOWED)) {
Address address = person.getAddress();
if (serializationInclude.shouldSerialize(address)) {
JacksonMapperUtil.serializeUnwrapped(address, jsonGenerator, serializerProvider);
}
}
int age = person.getAge();
if (serializationInclude.shouldSerialize(age)) {
JacksonMapperUtil.writeFieldName(jsonGenerator, propertyNamingStrategy, "age",
SerializedStrings$quarkusjacksonserializer.age);
jsonGenerator.writeNumber(age);
}
String firstName = person.getFirstName();
if (serializationInclude.shouldSerialize(firstName)) {
JacksonMapperUtil.writeFieldName(jsonGenerator, propertyNamingStrategy, "firstName",
SerializedStrings$quarkusjacksonserializer.firstName);
jsonGenerator.writeString(firstName);
}
String familyName = person.getLastName();
if (serializationInclude.shouldSerialize(familyName)) {
// familyName has an explicit json name, so we ignore the property naming strategy
jsonGenerator.writeFieldName(SerializedStrings$quarkusjacksonserializer.familyName);
jsonGenerator.writeString(familyName);
}
String lastName = person.getLastName();
if (serializationInclude.shouldSerialize(lastName)) {
JacksonMapperUtil.writeFieldName(jsonGenerator, propertyNamingStrategy, "lastName",
SerializedStrings$quarkusjacksonserializer.lastName);
jsonGenerator.writeString(lastName);
}
int id = person.getId();
if (JacksonMapperUtil.isViewIncluded(activeView, id_VIEW_CLASSES)
&& serializationInclude.shouldSerialize(lastName)) {
JacksonMapperUtil.writeFieldName(jsonGenerator, propertyNamingStrategy, "id",
SerializedStrings$quarkusjacksonserializer.id);
jsonGenerator.writeString(lastName);
}
jsonGenerator.writeEndObject();
}
}
public class SerializedStrings$quarkusjacksonserializer {
static final SerializedString age = new SerializedString("age");
static final SerializedString firstName = new SerializedString("firstName");
static final SerializedString familyName = new SerializedString("familyName");
static final SerializedString address = new SerializedString("address");
static final SerializedString id = new SerializedString("id");
}
Here, for performance reasons, the names of the fields to be serialized is stored as Jackson's SerializedStrings
in an external class, and reused for each serialization, thus avoiding executing the UTF-8 encoding of the same strings
at each serialization.
Note that in this case also the Address class has to be serialized in the same way, and then this factory triggers
the generation of a second StdSerializer also for it. More in general, if during the generation of a serializer for a
given class it discovers a non-primitive field of another type for which a serializer hasn't been generated yet, this
factory enqueues a code generation also for that type. The same is valid for both arrays of that type, like
Address[], and collections, like List<Address>.-
Nested Class Summary
Nested classes/interfaces inherited from class io.quarkus.resteasy.reactive.jackson.deployment.processor.JacksonCodeGenerator
JacksonCodeGenerator.FieldKind, JacksonCodeGenerator.FieldSpecs, JacksonCodeGenerator.TypeEntry -
Field Summary
Fields inherited from class io.quarkus.resteasy.reactive.jackson.deployment.processor.JacksonCodeGenerator
COLLECTION_NAME, generatedClassBuildItemBuildProducer, jandexIndex, MAP_NAME -
Constructor Summary
ConstructorsConstructorDescriptionJacksonSerializerFactory(io.quarkus.deployment.annotations.BuildProducer<io.quarkus.deployment.builditem.GeneratedClassBuildItem> generatedClassBuildItemBuildProducer, org.jboss.jandex.IndexView jandexIndex) -
Method Summary
Modifier and TypeMethodDescriptioncreate(Collection<org.jboss.jandex.ClassInfo> classInfos) createFromTypes(Collection<org.jboss.jandex.Type> types) protected booleancreateSerializationMethod(org.jboss.jandex.ClassInfo classInfo, io.quarkus.gizmo.ClassCreator classCreator, String beanClassName, Map<String, org.jboss.jandex.Type> typeBindings) protected booleandiscoverFieldTypes(org.jboss.jandex.ClassInfo classInfo, String beanClassName, Map<String, org.jboss.jandex.Type> typeBindings) protected Stringprotected String(package private) static StringsanitizeFieldName(String jsonName) Methods inherited from class io.quarkus.resteasy.reactive.jackson.deployment.processor.JacksonCodeGenerator
anyGetterBackingFieldName, classFields, classFields, classifyFieldType, classMethods, discoverIgnoredProperties, discoverTypes, discoverTypes, fieldSpecsFromField, fieldSpecsFromFieldParam, findAnyGetterField, findAnyGetterMethod, findConstructor, findMethod, findUnknownAnnotation, getClassIncludeValue, getIgnoredProperties, getInterfacesNames, getNamingStrategy, getPropertyOrder, getterMethodInfo, hasJsonTypeInfoInTypeChain, isAssignableTo, isClassFormatShapeArray, isEnumType, isFieldTypeIgnored, onSuperClass, registerTypeToBeGenerated, shouldGenerateCodeFor, shouldIgnoreUnknownProperties, ucFirst, vetoedClass
-
Constructor Details
-
JacksonSerializerFactory
public JacksonSerializerFactory(io.quarkus.deployment.annotations.BuildProducer<io.quarkus.deployment.builditem.GeneratedClassBuildItem> generatedClassBuildItemBuildProducer, org.jboss.jandex.IndexView jandexIndex)
-
-
Method Details
-
create
- Overrides:
createin classJacksonCodeGenerator
-
createFromTypes
- Overrides:
createFromTypesin classJacksonCodeGenerator
-
getSuperClassName
- Specified by:
getSuperClassNamein classJacksonCodeGenerator
-
getClassSuffix
- Specified by:
getClassSuffixin classJacksonCodeGenerator
-
discoverFieldTypes
protected boolean discoverFieldTypes(org.jboss.jandex.ClassInfo classInfo, String beanClassName, Map<String, org.jboss.jandex.Type> typeBindings) - Specified by:
discoverFieldTypesin classJacksonCodeGenerator
-
createSerializationMethod
protected boolean createSerializationMethod(org.jboss.jandex.ClassInfo classInfo, io.quarkus.gizmo.ClassCreator classCreator, String beanClassName, Map<String, org.jboss.jandex.Type> typeBindings) - Specified by:
createSerializationMethodin classJacksonCodeGenerator
-
sanitizeFieldName
-