github.com/phrase/openapi@v0.0.0-20240514140800-49e8a106740e/openapi-generator/templates/java/libraries/resteasy/JSON.mustache (about)

     1  package {{invokerPackage}};
     2  
     3  import com.fasterxml.jackson.annotation.*;
     4  import com.fasterxml.jackson.databind.*;
     5  import org.openapitools.jackson.nullable.JsonNullableModule;
     6  {{#java8}}
     7  import com.fasterxml.jackson.datatype.jsr310.*;
     8  {{/java8}}
     9  {{^java8}}
    10  import com.fasterxml.jackson.datatype.joda.*;
    11  {{/java8}}
    12  
    13  import java.text.DateFormat;
    14  
    15  import javax.ws.rs.ext.ContextResolver;
    16  
    17  {{>generatedAnnotation}}
    18  public class JSON implements ContextResolver<ObjectMapper> {
    19    private ObjectMapper mapper;
    20  
    21    public JSON() {
    22      mapper = new ObjectMapper();
    23      mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    24      mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    25      mapper.configure(DeserializationFeature.FAIL_ON_INVALID_SUBTYPE, false);
    26      mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
    27      mapper.enable(SerializationFeature.WRITE_ENUMS_USING_TO_STRING);
    28      mapper.enable(DeserializationFeature.READ_ENUMS_USING_TO_STRING);
    29      mapper.setDateFormat(new RFC3339DateFormat());
    30      JsonNullableModule jnm = new JsonNullableModule();
    31      mapper.registerModule(jnm);
    32      {{#java8}}
    33      mapper.registerModule(new JavaTimeModule());
    34      {{/java8}}
    35      {{^java8}}
    36      mapper.registerModule(new JodaModule());
    37      {{/java8}}
    38    }
    39  
    40    /**
    41     * Set the date format for JSON (de)serialization with Date properties.
    42     * @param dateFormat the date format to set
    43     */
    44    public void setDateFormat(DateFormat dateFormat) {
    45      mapper.setDateFormat(dateFormat);
    46    }
    47  
    48    @Override
    49    public ObjectMapper getContext(Class<?> type) {
    50      return mapper;
    51    }
    52  }