Example usage for com.fasterxml.jackson.databind ObjectMapper registerModule

List of usage examples for com.fasterxml.jackson.databind ObjectMapper registerModule

Introduction

In this page you can find the example usage for com.fasterxml.jackson.databind ObjectMapper registerModule.

Prototype

public ObjectMapper registerModule(Module module) 

Source Link

Document

Method for registering a module that can extend functionality provided by this mapper; for example, by adding providers for custom serializers and deserializers.

Usage

From source file:com.example.common.MoneyJacksonAutoConfiguration.java

public CustomJsonToPojoMessageConverter(ObjectMapper mapper) {
    super(MimeTypeUtils.APPLICATION_JSON, MessageConverterUtils.X_JAVA_OBJECT);
    this.mapper = mapper;
    mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
    mapper.registerModule(new JodaMoneyModule());
}

From source file:com.mastfrog.scamper.NettyBootstrapModule.java

@Override
protected void configure() {
    bind(DataEncoding.class).toInstance(encoding);
    switch (encoding) {
    case BSON:/*from   w  ww. j a v  a 2s .  c om*/
        BsonFactory bsonFactory = new BsonFactory();
        bind(BsonFactory.class).toInstance(bsonFactory);
        ObjectMapper mapper = new ObjectMapper(bsonFactory);
        for (com.fasterxml.jackson.databind.Module m : jacksonModules) {
            mapper.registerModule(m);
        }
        bind(ObjectMapper.class).annotatedWith(Names.named(GUICE_BINDING_SCAMPER_CODEC)).toInstance(mapper);
        bind(Codec.class).annotatedWith(Names.named(GUICE_BINDING_SCAMPER_CODEC)).to(CodecImpl.class);
        break;
    case JSON:
        ObjectMapper mapper2 = new ObjectMapper();
        for (com.fasterxml.jackson.databind.Module m : jacksonModules) {
            mapper2.registerModule(m);
        }
        bind(ObjectMapper.class).annotatedWith(Names.named(GUICE_BINDING_SCAMPER_CODEC)).toInstance(mapper2);
        bind(Codec.class).annotatedWith(Names.named(GUICE_BINDING_SCAMPER_CODEC)).to(CodecImpl.class);
        break;
    case JAVA_SERIALIZATION:
        bind(Codec.class).annotatedWith(Names.named(GUICE_BINDING_SCAMPER_CODEC)).to(SerializationCodec.class);
        break;
    default:
        throw new AssertionError(encoding);
    }
    bind(ChannelHandlerAdapter.class).annotatedWith(Names.named("dispatcher")).to(adap);
    bind(ChannelHandlerAdapter.class).annotatedWith(Names.named("processor")).to(InboundMessageDecoder.class);
    bind(EventLoopGroup.class).annotatedWith(Names.named(GUICE_BINDING_SCAMPER_BOSS_THREADS))
            .toInstance(new NioEventLoopGroup(bossThreads));
    bind(EventLoopGroup.class).annotatedWith(Names.named(GUICE_BINDING_SCAMPER_WORKER_THREADS))
            .toInstance(workerThreads == -1 ? new NioEventLoopGroup() : new NioEventLoopGroup(workerThreads));
    bind(ByteBufAllocator.class).annotatedWith(Names.named(GUICE_BINDING_SCAMPER_CODEC))
            .toInstance(new PooledByteBufAllocator(true));
    bind(ShutdownHandler.class).asEagerSingleton();
}

From source file:com.miserablemind.api.consumer.tradeking.api.impl.TradeKingTemplate.java

@Override
protected MappingJackson2HttpMessageConverter getJsonMessageConverter() {
    MappingJackson2HttpMessageConverter converter = super.getJsonMessageConverter();

    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(DeserializationFeature.READ_ENUMS_USING_TO_STRING, true);
    mapper.configure(SerializationFeature.WRITE_ENUMS_USING_TO_STRING, true);
    mapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);
    mapper.registerModule(new TradeKingModule());
    mapper.registerModule(new JodaModule());
    converter.setObjectMapper(mapper);//from   w  ww . java  2s .co  m
    return converter;
}

From source file:edu.ucsd.crbs.cws.dao.rest.WorkspaceFileRestDAOImpl.java

@Override
public List<WorkspaceFile> getWorkspaceFilesBySourceJobId(long sourceJobId) throws Exception {
    ClientConfig cc = new DefaultClientConfig();
    cc.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
    Client client = Client.create(cc);/*from w  w  w  . j  a  v  a 2 s. c  o  m*/
    client.addFilter(new HTTPBasicAuthFilter(_user.getLogin(), _user.getToken()));
    client.setFollowRedirects(true);
    WebResource resource = client.resource(_restURL).path(Constants.REST_PATH)
            .path(Constants.WORKSPACEFILES_PATH);

    MultivaluedMap queryParams = _multivaluedMapFactory.getMultivaluedMap(_user);
    queryParams.add(Constants.SOURCE_JOB_ID_QUERY_PARAM, Long.toString(sourceJobId));

    String json = resource.queryParams(queryParams).accept(MediaType.APPLICATION_JSON).get(String.class);
    ObjectMapper mapper = new ObjectMapper();
    mapper.registerModule(new ObjectifyJacksonModule());
    return mapper.readValue(json, new TypeReference<List<WorkspaceFile>>() {
    });
}

From source file:capital.scalable.restdocs.example.jackson.JsonConfiguration.java

@Bean
public ObjectMapper objectMapper() {
    ObjectMapper mapper = new ObjectMapper();
    mapper.setVisibility(mapper.getSerializationConfig().getDefaultVisibilityChecker()
            .withFieldVisibility(JsonAutoDetect.Visibility.ANY));

    SimpleModule module = new SimpleModule();
    module.addSerializer(Money.class, new MoneySerializer());
    mapper.registerModule(module);

    return mapper;
}

From source file:edu.ucsd.crbs.cws.dao.rest.WorkspaceFileRestDAOImpl.java

@Override
public WorkspaceFile getWorkspaceFileById(String workspaceFileId, User user) throws Exception {
    ClientConfig cc = new DefaultClientConfig();
    cc.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
    Client client = Client.create(cc);// www.  j  a v  a2 s  .  co m

    if (user != null) {
        client.addFilter(new HTTPBasicAuthFilter(user.getLogin(), user.getToken()));
    } else if (_user != null) {
        client.addFilter(new HTTPBasicAuthFilter(_user.getLogin(), _user.getToken()));
    }

    client.setFollowRedirects(true);
    WebResource resource = client.resource(_restURL).path(Constants.REST_PATH)
            .path(Constants.WORKSPACEFILES_PATH).path(workspaceFileId);
    String json = resource.accept(MediaType.APPLICATION_JSON).get(String.class);

    ObjectMapper mapper = new ObjectMapper();
    mapper.registerModule(new ObjectifyJacksonModule());
    return mapper.readValue(json, new TypeReference<WorkspaceFile>() {
    });
}

From source file:edu.ucsd.crbs.cws.dao.rest.WorkspaceFileRestDAOImpl.java

@Override
public WorkspaceFile resave(long workspaceFileId) throws Exception {
    ClientConfig cc = new DefaultClientConfig();
    cc.getClasses().add(StringProvider.class);
    cc.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
    Client client = Client.create(cc);//from   w w w . ja v  a  2 s .c o m
    client.addFilter(new HTTPBasicAuthFilter(_user.getLogin(), _user.getToken()));
    client.setFollowRedirects(true);
    WebResource resource = client.resource(_restURL).path(Constants.REST_PATH)
            .path(Constants.WORKSPACEFILES_PATH).path(Long.toString(workspaceFileId));

    MultivaluedMap queryParams = _multivaluedMapFactory.getMultivaluedMap(_user);
    queryParams.add(Constants.RESAVE_QUERY_PARAM, "true");

    String json = resource.queryParams(queryParams).accept(MediaType.APPLICATION_JSON)
            .type(MediaType.APPLICATION_JSON_TYPE).entity("{}").post(String.class);

    ObjectMapper mapper = new ObjectMapper();
    mapper.registerModule(new ObjectifyJacksonModule());
    return mapper.readValue(json, new TypeReference<WorkspaceFile>() {
    });
}

From source file:edu.ucsd.crbs.cws.dao.rest.WorkspaceFileRestDAOImpl.java

@Override
public List<WorkspaceFile> getWorkspaceFilesById(String workspaceFileIds, User user) throws Exception {
    ClientConfig cc = new DefaultClientConfig();
    cc.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
    Client client = Client.create(cc);/*from  w  ww  .  j  av a2s . c o  m*/

    if (user != null) {
        client.addFilter(new HTTPBasicAuthFilter(user.getLogin(), user.getToken()));
    } else if (_user != null) {
        client.addFilter(new HTTPBasicAuthFilter(_user.getLogin(), _user.getToken()));
    }

    client.setFollowRedirects(true);
    WebResource resource = client.resource(_restURL).path(Constants.REST_PATH)
            .path(Constants.WORKSPACEFILES_PATH);
    MultivaluedMap queryParams = _multivaluedMapFactory.getMultivaluedMap(user);
    if (workspaceFileIds != null) {
        queryParams.add(Constants.WSFID_PARAM, workspaceFileIds);
    }

    String json = resource.queryParams(queryParams).accept(MediaType.APPLICATION_JSON).get(String.class);
    ObjectMapper mapper = new ObjectMapper();
    mapper.registerModule(new ObjectifyJacksonModule());
    return mapper.readValue(json, new TypeReference<List<WorkspaceFile>>() {
    });

}

From source file:edu.ucsd.crbs.cws.dao.rest.JobRestDAOImpl.java

@Override
public Job update(long jobId, final String status, Long estCpu, Long estRunTime, Long estDisk, Long submitDate,
        Long startDate, Long finishDate, Boolean submittedToScheduler, final String schedulerJobId,
        final Boolean deleted, final String error, final String detailedError) throws Exception {

    ClientConfig cc = new DefaultClientConfig();
    cc.getClasses().add(StringProvider.class);
    Client client = Client.create(cc);/*  w  w w.  ja  va 2  s .c o m*/
    client.addFilter(new HTTPBasicAuthFilter(_user.getLogin(), _user.getToken()));
    client.setFollowRedirects(true);
    WebResource resource = client.resource(_restURL).path(Constants.REST_PATH).path(Constants.JOBS_PATH)
            .path(Long.toString(jobId));

    MultivaluedMap queryParams = _multivaluedMapFactory.getMultivaluedMap(_user);

    if (status != null) {
        queryParams.add(Constants.STATUS_QUERY_PARAM, status);
    }
    if (estCpu != null) {
        queryParams.add(Constants.ESTCPU_QUERY_PARAM, estCpu);
    }
    if (estRunTime != null) {
        queryParams.add(Constants.ESTRUNTIME_QUERY_PARAM, estRunTime);
    }
    if (estDisk != null) {
        queryParams.add(Constants.ESTDISK_QUERY_PARAM, estDisk);
    }
    if (submitDate != null) {
        queryParams.add(Constants.SUBMITDATE_QUERY_PARAM, submitDate.toString());
    }
    if (startDate != null) {
        queryParams.add(Constants.STARTDATE_QUERY_PARAM, startDate.toString());
    }
    if (finishDate != null) {
        queryParams.add(Constants.FINISHDATE_QUERY_PARAM, finishDate.toString());
    }
    if (submittedToScheduler != null) {
        queryParams.add(Constants.SUBMITTED_TO_SCHED_QUERY_PARAM, submittedToScheduler.toString());
    }

    if (schedulerJobId != null) {
        queryParams.add(Constants.SCHEDULER_JOB_ID_QUERY_PARAM, schedulerJobId);
    }

    if (deleted != null) {
        queryParams.add(Constants.DELETED_QUERY_PARAM, deleted.toString());
    }

    if (error != null) {
        queryParams.add(Constants.ERROR_QUERY_PARAM, error);
    }

    if (detailedError != null) {
        queryParams.add(Constants.DETAILED_ERROR_QUERY_PARAM, detailedError);
    }

    String json = resource.queryParams(queryParams).accept(MediaType.APPLICATION_JSON)
            .type(MediaType.APPLICATION_JSON_TYPE).entity("{}").post(String.class);

    ObjectMapper mapper = new ObjectMapper();
    mapper.registerModule(new ObjectifyJacksonModule());
    return mapper.readValue(json, new TypeReference<Job>() {
    });
}

From source file:io.seldon.api.state.PredictionAlgorithmStore.java

@Override
public void configUpdated(String client, String configKey, String configValue) {
    SimpleModule module = new SimpleModule("PredictionStrategyDeserializerModule");
    module.addDeserializer(Strategy.class, new PredictionStrategyDeserializer());
    ObjectMapper mapper = new ObjectMapper();
    mapper.registerModule(module);
    if (configKey.equals(ALG_KEY)) {
        logger.info("Received new algorithm config for " + client + ": " + configValue);
        try {/* w ww.j  av a2 s .  c om*/
            Strategy configStrategy = mapper.readValue(configValue, Strategy.class);
            if (configStrategy instanceof AlgorithmConfig) {
                List<PredictionAlgorithmStrategy> strategies = new ArrayList<>();
                AlgorithmConfig config = (AlgorithmConfig) configStrategy;
                for (Algorithm algorithm : config.algorithms) {
                    PredictionAlgorithmStrategy strategy = toAlgorithmStrategy(algorithm);
                    strategies.add(strategy);
                }
                List<FeatureTransformerStrategy> featureTransformerStrategies = new ArrayList<>();
                if (config.transformers != null)
                    for (Transformer transformer : config.transformers) {
                        FeatureTransformerStrategy strategy = toFeatureTransformerStrategy(transformer);
                        featureTransformerStrategies.add(strategy);
                    }
                predictionStore.put(client,
                        new SimplePredictionStrategy(PredictionStrategy.DEFAULT_NAME,
                                Collections.unmodifiableList(featureTransformerStrategies),
                                Collections.unmodifiableList(strategies)));
                logger.info("Successfully added new algorithm config for " + client);
            } else if (configStrategy instanceof TestConfig) {
                TestConfig config = (TestConfig) configStrategy;
                //TestConfig config = mapper.readValue(configValue, TestConfig.class);

                List<VariationPredictionStrategy.Variation> variations = new ArrayList<>();
                for (TestVariation var : config.variations) {
                    List<PredictionAlgorithmStrategy> strategies = new ArrayList<>();
                    for (Algorithm alg : var.config.algorithms) {
                        PredictionAlgorithmStrategy strategy = toAlgorithmStrategy(alg);
                        strategies.add(strategy);
                    }
                    List<FeatureTransformerStrategy> featureTransformerStrategies = new ArrayList<>();
                    if (var.config.transformers != null)
                        for (Transformer transformer : var.config.transformers) {
                            FeatureTransformerStrategy strategy = toFeatureTransformerStrategy(transformer);
                            featureTransformerStrategies.add(strategy);
                        }
                    //Need to add combiner here 
                    variations
                            .add(new VariationPredictionStrategy.Variation(
                                    new SimplePredictionStrategy(var.label,
                                            Collections.unmodifiableList(featureTransformerStrategies),
                                            Collections.unmodifiableList(strategies)),
                                    new BigDecimal(var.ratio)));

                }
                predictionStore.put(client, VariationPredictionStrategy.build(variations));
                logger.info("Succesfully added " + variations.size() + " variation test for " + client);
            } else {
                logger.error("Unknown type for algorithm config");
            }
        } catch (IOException | BeansException e) {
            logger.error("Couldn't update algorithms for client " + client, e);
        }
    }

}