Class JacksonSerializerFactory

java.lang.Object
io.quarkus.resteasy.reactive.jackson.deployment.processor.JacksonCodeGenerator
io.quarkus.resteasy.reactive.jackson.deployment.processor.JacksonSerializerFactory

public class JacksonSerializerFactory extends JacksonCodeGenerator
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&gt.