Example usage for com.fasterxml.jackson.datatype.jsr310 JavaTimeModule JavaTimeModule

List of usage examples for com.fasterxml.jackson.datatype.jsr310 JavaTimeModule JavaTimeModule

Introduction

In this page you can find the example usage for com.fasterxml.jackson.datatype.jsr310 JavaTimeModule JavaTimeModule.

Prototype

public JavaTimeModule() 

Source Link

Usage

From source file:com.example.AuthzApp.java

@Bean
ObjectMapper objectMapper() {//  w w w  .j a va2  s.  c  om
    final JavaTimeModule javaTimeModule = new JavaTimeModule();
    javaTimeModule.addSerializer(LocalDateTime.class,
            new LocalDateTimeSerializer(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
    return new Jackson2ObjectMapperBuilder()
            .featuresToDisable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES).modules(javaTimeModule)
            .serializationInclusion(JsonInclude.Include.NON_NULL)
            .propertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE).build();
}

From source file:com.derpgroup.echodebugger.App.java

@Override
public void run(MainConfig config, Environment environment) throws IOException {
    if (config.isPrettyPrint()) {
        ObjectMapper mapper = environment.getObjectMapper();
        mapper.enable(SerializationFeature.INDENT_OUTPUT);
        mapper.registerModule(new JavaTimeModule());
        mapper.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, false);
        mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
    }/*from   w ww  .  ja v  a 2 s .com*/

    // Health checks
    environment.healthChecks().register("basics", new BasicHealthCheck(config, environment));

    // Load up the content
    UserDaoLocalImpl userDao = new UserDaoLocalImpl(config, environment);
    userDao.initialize();

    // Build the helper thread that saves data every X minutes
    UserDaoLocalImplThread userThread = new UserDaoLocalImplThread(config, userDao);
    userThread.start();

    EchoDebuggerResource debuggerResource = new EchoDebuggerResource(config, environment);
    debuggerResource.setUserDao(userDao);

    // Resources
    environment.jersey().register(debuggerResource);

    // Providers
    environment.jersey().register(new ResponderExceptionMapper());
}

From source file:org.lambdamatic.internal.elasticsearch.codec.DocumentSearchCodec.java

/**
 * Constructor.//from w w  w  .  j a v  a 2s .  co  m
 * 
 */
public DocumentSearchCodec() {
    this.objectMapper = new ObjectMapper();
    // custom serializers for queries
    final SimpleModule module = new SimpleModule();
    module.addSerializer(DocumentSearch.class, new DocumentSearchSerializer());
    module.addSerializer(TermQuery.class, new TermQuerySerializer());
    module.addSerializer(RangeQuery.class, new RangeQuerySerializer());
    module.addSerializer(MatchQuery.class, new MatchQuerySerializer());
    module.addSerializer(BooleanQuery.class, new BooleanQuerySerializer());
    module.addSerializer(GeoBoundingBoxQuery.class, new GeoBoundingBoxSerializer());
    this.objectMapper.registerModule(module);
    // support for Java time
    this.objectMapper.registerModule(new JavaTimeModule());
    // configure LocalDate serialization as string with pattern: YYYY-mm-dd
    this.objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);

}

From source file:org.lambdamatic.internal.elasticsearch.codec.ObjectMapperFactory.java

/**
 * Initializes an {@link ObjectMapper} configured with mixins to support serialization and
 * deserialization of all built-in and user-defined domain types.
 * <p>/*  www  .  j a v a  2  s  .c o  m*/
 * <strong>Note:</strong>The {@link ObjectMapper} is instantiated and initialized once and then
 * kept in cache, so multiple calls will retrieve the same instance.
 * </p>
 * 
 * @return the {@link ObjectMapper}
 * 
 */
public static ObjectMapper getObjectMapper() {
    if (instance.objectMapper == null) {
        LOGGER.info("Initializing the ObjectMapper");
        final ObjectMapper mapper = new ObjectMapper();
        final ExecutorService availableProcessorsThreadPool = Executors
                .newFixedThreadPool(Runtime.getRuntime().availableProcessors());
        final Reflections reflections = new Reflections(new ConfigurationBuilder()

                // TODO: allow for configuration settings to reduce the scope of searching, using package
                // names instead of a classloader
                .setUrls(ClasspathHelper.forJavaClassPath())
                // .setUrls(ClasspathHelper.forClassLoader())
                .setScanners(new SubTypesScanner(), new TypeAnnotationsScanner())
                .setExecutorService(availableProcessorsThreadPool));
        // thread pool must be closed after it has been used, to avoid leaking threads in the JVM.
        availableProcessorsThreadPool.shutdown();
        // final Reflections reflections = new Reflections();
        reflections.getTypesAnnotatedWith(Mixin.class).stream().forEach(mixin -> {
            final Mixin mixinAnnotation = mixin.getAnnotation(Mixin.class);
            LOGGER.info("Adding mixin {} to {}", mixin, mixinAnnotation.target());
            mapper.addMixIn(mixinAnnotation.target(), mixin);
        });
        mapper.registerModule(new JavaTimeModule());
        // configure LocalDate serialization as string with pattern: YYYY-mm-dd
        mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
        instance.objectMapper = mapper;
    }
    return instance.objectMapper;
}

From source file:com.linecorp.bot.model.event.CallbackRequestTest.java

private void parse(String resourceName, RequestTester callback) throws IOException {
    try (InputStream resource = getClass().getClassLoader().getResourceAsStream(resourceName)) {
        String json = StreamUtils.copyToString(resource, StandardCharsets.UTF_8);
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        objectMapper.registerModule(new JavaTimeModule())
                .configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, false);
        CallbackRequest callbackRequest = objectMapper.readValue(json, CallbackRequest.class);

        callback.call(callbackRequest);//w  w  w  .java2s. c om
    }
}

From source file:org.moserp.common.rest.ObjectMapperCustomizer.java

@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {

    if (!(bean instanceof ObjectMapper)) {
        return bean;
    }//  w w w  .  j a  v  a2s . c om

    ObjectMapper mapper = (ObjectMapper) bean;
    mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
    mapper.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, false);
    mapper.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, false);
    mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
    mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
    registerQuantitySerializer(mapper);
    mapper.registerModules(new MoneyModule(), new JavaTimeModule(), new Jackson2HalModule());

    return mapper;
}

From source file:com.samovich.service.blueprint.App.java

/**
 * Object mapper/*  www .ja  v a 2  s  .  c  o m*/
 * @return mapper
 */
@Bean
public ObjectMapper objectMapper() {
    ObjectMapper mapper = new ObjectMapper();
    mapper.registerModule(new AfterburnerModule());
    mapper.setSerializationInclusion(Include.NON_NULL);
    mapper.registerModule(new JavaTimeModule());
    return mapper;
}

From source file:io.pivotal.strepsirrhini.chaosloris.ChaosLoris.java

@Bean
JavaTimeModule javaTimeModule() {
    return new JavaTimeModule();
}

From source file:net.fischboeck.discogs.DiscogsClient.java

private void init() {
    HttpClientBuilder builder = HttpClients.custom();
    RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(REQUEST_TIMEOUT).build();

    this.httpClient = builder.setDefaultHeaders(this.getDefaultHeaders()).setDefaultRequestConfig(requestConfig)
            .build();//w  ww. j a v  a2  s. c o m

    this.mapper = new ObjectMapper();
    mapper.registerModule(new JavaTimeModule());
    mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
}

From source file:keywhiz.cli.CliModule.java

@Provides
public ObjectMapper generalMapper() {
    /**/*from w w w.  j  av a  2s.c o m*/
     * Customizes ObjectMapper for common settings.
     *
     * @param objectMapper to be customized
     * @return customized input factory
     */
    ObjectMapper objectMapper = Jackson.newObjectMapper();
    objectMapper.registerModule(new Jdk8Module());
    objectMapper.registerModules(new JavaTimeModule());
    objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    return objectMapper;
}