Example usage for com.fasterxml.jackson.databind.module SimpleModule addSerializer

List of usage examples for com.fasterxml.jackson.databind.module SimpleModule addSerializer

Introduction

In this page you can find the example usage for com.fasterxml.jackson.databind.module SimpleModule addSerializer.

Prototype

public SimpleModule addSerializer(JsonSerializer<?> ser) 

Source Link

Usage

From source file:be.dnsbelgium.rdap.jackson.CustomObjectMapper.java

public CustomObjectMapper() {
    super.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
    setSerializationInclusion(JsonInclude.Include.NON_NULL);
    configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
    configure(SerializationFeature.WRITE_EMPTY_JSON_ARRAYS, false);
    SimpleModule simpleModule = new SimpleModule("SimpleModule", new Version(1, 0, 0, null));

    simpleModule.addSerializer(new RDAPContactSerializer());
    simpleModule.addSerializer(new StructuredValueSerializer());
    simpleModule.addSerializer(new TextListSerializer());
    simpleModule.addSerializer(new TextSerializer());
    simpleModule.addSerializer(new URIValueSerializer());
    simpleModule.addSerializer(new DomainNameSerializer());
    simpleModule.addSerializer(new DateTimeSerializer());
    simpleModule.addSerializer(new StatusSerializer());
    for (JsonSerializer serializer : getSerializers()) {
        simpleModule.addSerializer(serializer);
    }/*  w  ww  .java2  s  .c o  m*/

    registerModule(simpleModule);
}

From source file:com.tdclighthouse.prototype.utils.JacksonSerializer.java

public void initialize() {
    SimpleModule module = new SimpleModule();
    for (JsonSerializer<?> jsonSerializer : serializers) {
        module.addSerializer(jsonSerializer);
    }//from w w  w.j  a va 2  s .c  o m
    objectMapper.registerModule(module);
}

From source file:org.openmhealth.dsu.configuration.JacksonConfiguration.java

@Bean
public SimpleModule jdk18Module() {

    SimpleModule module = new SimpleModule();

    module.addSerializer(new OptionalSerializer());
    module.addDeserializer(Optional.class, new OptionalDeserializer());

    return module;
}

From source file:com.heisenberg.impl.json.JacksonJsonService.java

public JacksonJsonService(ServiceRegistry serviceRegistry) {
    this.objectMapper = serviceRegistry.getService(ObjectMapper.class);
    this.jsonFactory = serviceRegistry.getService(JsonFactory.class);

    objectMapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS)
            .setVisibility(PropertyAccessor.ALL, Visibility.NONE)
            .setVisibility(PropertyAccessor.FIELD, Visibility.ANY).setSerializationInclusion(Include.NON_EMPTY);

    SimpleModule module = new SimpleModule();
    module.addSerializer(new LocalDateTimeSerializer());
    module.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer());
    objectMapper.registerModule(module);
}

From source file:nl.knaw.huygens.timbuctoo.rest.providers.HTMLProviderHelper.java

/**
 * Returns an object writer for a class with the specified annotations.
 * <ul>/*from www. j a  va 2s  .  c  o m*/
 * <li>The current implementation is not thread safe.</li>
 * <li>Apparently uses undocumented features of Jackson. In version 2.1 it used
 * <code>com.fasterxml.jackson.jaxrs.json.util.AnnotationBundleKey</code> and
 * <code>com.fasterxml.jackson.jaxrs.json.annotation.EndpointConfig</code>.
 * In Jackson 2.2 the first of these classes was moved to a different package,
 * and the second was replaced by <code>JsonEndpointConfig</code>.</li>
 * </ul>
 */
public ObjectWriter getObjectWriter(Annotation[] annotations) {
    AnnotationBundleKey key = new AnnotationBundleKey(annotations, AnnotationBundleKey.class);
    ObjectWriter writer = writers.get(key);
    if (writer == null) {
        // A quick hack to add custom serialization of the Reference type.
        SimpleModule module = new SimpleModule();
        module.addSerializer(new ReferenceSerializer(registry));
        ObjectMapper mapper = new ObjectMapper();
        mapper.registerModule(module);
        JsonEndpointConfig endpointConfig = JsonEndpointConfig.forWriting(mapper, annotations, null);
        writer = endpointConfig.getWriter();
        writers.put(key, writer);
    }
    return writer;
}

From source file:com.mastfrog.acteur.mongo.impl.JacksonMongoDB.java

@Override
public ObjectMapper configure(ObjectMapper om) {
    om.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
    SimpleModule sm = new SimpleModule("mongo", new Version(1, 0, 0, null, "com.timboudreau", "trackerapi"));
    sm.addSerializer(new ObjectIdSerializer());
    sm.addDeserializer(ObjectId.class, new ObjectIdDeserializer());
    om.registerModule(sm);/*from ww  w. j av  a2  s. c o  m*/
    return om;
}

From source file:fr.norad.jmxzabbix.core.ZabbixClient.java

public ZabbixClient(ZabbixConfig config) {
    this.config = config;

    // zabbix does not understand boolean with value true without quotes which is default of jackson
    SimpleModule module = new SimpleModule("BooleanAsString", new Version(1, 0, 0, null, null, null));
    module.addSerializer(new NonTypedScalarSerializerBase<Boolean>(Boolean.class) {
        @Override/*  ww w  .ja va 2s . c  o  m*/
        public void serialize(Boolean value, JsonGenerator jgen, SerializerProvider provider)
                throws IOException {
            jgen.writeString(value.toString());
        }
    });

    mapper.registerModule(module);
}

From source file:com.cloudera.nav.sdk.client.writer.JsonMetadataWriter.java

private ObjectMapper newMapper() {
    ObjectMapper mapper = new ObjectMapper();
    SimpleModule module = new SimpleModule("MetadataSerializer");
    if (config.getApiVersion() < 9) {
        module.addSerializer(new EntitySerializer(registry));
    } else {//from   ww w.  j  av  a 2 s . c o m
        module.addSerializer(new EntityV9Serializer(registry));
    }
    module.addSerializer(new RelationSerializer(registry));
    mapper.registerModule(module);
    mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
    mapper.configure(DeserializationFeature.WRAP_EXCEPTIONS, false);
    mapper.registerModule(new JodaModule());
    return mapper;
}

From source file:com.proofpoint.event.collector.TestFilteringMapSerializer.java

private ObjectMapper getMapper(FilteringMapSerializer filteringMapSerializer) {
    SimpleModule testModule = new SimpleModule("FilteringEventModule", Version.unknownVersion());
    testModule.addSerializer(filteringMapSerializer);
    ObjectMapper mapper = new ObjectMapperProvider().get();
    mapper.registerModule(testModule);/*  ww  w. j a v a  2s  .c  o m*/
    return mapper;
}

From source file:com.hpcloud.mon.MonApiApplication.java

@Override
public void run(MonApiConfiguration config, Environment environment) throws Exception {
    /** Wire services */
    Injector.registerModules(new MonApiModule(environment, config));

    /** Configure resources */
    environment.jersey().register(Injector.getInstance(VersionResource.class));
    environment.jersey().register(Injector.getInstance(AlarmResource.class));
    environment.jersey().register(Injector.getInstance(MetricResource.class));
    environment.jersey().register(Injector.getInstance(MeasurementResource.class));
    environment.jersey().register(Injector.getInstance(StatisticResource.class));
    environment.jersey().register(Injector.getInstance(NotificationMethodResource.class));

    /** Configure providers */
    removeExceptionMappers(environment.jersey().getResourceConfig().getSingletons());
    environment.jersey().register(new EntityExistsExceptionMapper());
    environment.jersey().register(new EntityNotFoundExceptionMapper());
    environment.jersey().register(new IllegalArgumentExceptionMapper());
    environment.jersey().register(new InvalidEntityExceptionMapper());
    environment.jersey().register(new JsonProcessingExceptionMapper());
    environment.jersey().register(new JsonMappingExceptionManager());
    environment.jersey().register(new ConstraintViolationExceptionMapper());
    environment.jersey().register(new ThrowableExceptionMapper<Throwable>() {
    });/*  w ww. ja  v  a  2s.com*/

    /** Configure Jackson */
    environment.getObjectMapper()
            .setPropertyNamingStrategy(PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES);
    environment.getObjectMapper().enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
    environment.getObjectMapper().disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
    SimpleModule module = new SimpleModule("SerializationModule");
    module.addSerializer(new SubAlarmExpressionSerializer());
    environment.getObjectMapper().registerModule(module);

    /** Configure health checks */
    environment.healthChecks().register("kafka", new KafkaHealthCheck(config.kafka));

    /** Configure auth filters */
    Dynamic preAuthenticationFilter = environment.servlets().addFilter("pre-auth",
            new PreAuthenticationFilter());
    preAuthenticationFilter.addMappingForUrlPatterns(null, true, "/");
    preAuthenticationFilter.addMappingForUrlPatterns(null, true, "/v2.0/*");

    if (config.middleware.enabled) {
        Map<String, String> authInitParams = new HashMap<String, String>();
        authInitParams.put("ServiceIds", config.middleware.serviceIds);
        authInitParams.put("EndpointIds", config.middleware.endpointIds);
        authInitParams.put("ServerVIP", config.middleware.serverVIP);
        authInitParams.put("ServerPort", config.middleware.serverPort);
        authInitParams.put("ConnTimeout", config.middleware.connTimeout);
        authInitParams.put("ConnSSLClientAuth", config.middleware.connSSLClientAuth);
        authInitParams.put("Keystore", config.middleware.keystore);
        authInitParams.put("KeystorePass", config.middleware.keystorePass);
        authInitParams.put("Truststore", config.middleware.truststore);
        authInitParams.put("TruststorePass", config.middleware.truststorePass);
        authInitParams.put("ConnPoolMaxActive", config.middleware.connPoolMaxActive);
        authInitParams.put("ConnPoolMaxIdle", config.middleware.connPoolMaxActive);
        authInitParams.put("ConnPoolEvictPeriod", config.middleware.connPoolEvictPeriod);
        authInitParams.put("ConnPoolMinIdleTime", config.middleware.connPoolMinIdleTime);
        authInitParams.put("ConnRetryTimes", config.middleware.connRetryTimes);
        authInitParams.put("ConnRetryInterval", config.middleware.connRetryInterval);

        Dynamic tokenAuthFilter = environment.servlets().addFilter("token-auth", new TokenAuth());
        tokenAuthFilter.addMappingForUrlPatterns(null, true, "/");
        tokenAuthFilter.addMappingForUrlPatterns(null, true, "/v2.0/*");
        tokenAuthFilter.setInitParameters(authInitParams);
    } else {
        Dynamic mockAuthenticationFilter = environment.servlets().addFilter("mock-auth",
                new MockAuthenticationFilter());
        mockAuthenticationFilter.addMappingForUrlPatterns(null, true, "/");
        mockAuthenticationFilter.addMappingForUrlPatterns(null, true, "/v2.0/*");
    }
    Dynamic postAuthenticationFilter = environment.servlets().addFilter("post-auth",
            new PostAuthenticationFilter(Collections.<String>singletonList("")));
    postAuthenticationFilter.addMappingForUrlPatterns(null, true, "/");
    postAuthenticationFilter.addMappingForUrlPatterns(null, true, "/v2.0/*");

    /** Configure swagger */
    SwaggerBundle.configure(config);
}