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

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

Introduction

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

Prototype

public ObjectMapper registerModules(Iterable<Module> modules) 

Source Link

Document

Convenience method for registering specified modules in order; functionally equivalent to:
 for (Module module : modules) { registerModule(module); } 

Usage

From source file:keywhiz.KeywhizService.java

/**
 * Customizes ObjectMapper for common settings.
 *
 * @param objectMapper to be customized//from  w  w  w .j av a2s  . com
 * @return customized input factory
 */
public static ObjectMapper customizeObjectMapper(ObjectMapper objectMapper) {
    objectMapper.registerModules(new Jdk8Module());
    objectMapper.registerModules(new JavaTimeModule());
    objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    return objectMapper;
}

From source file:de.alpharogroup.xml.json.JsonTransformer.java

/**
 * Transforms the given json string into a java object.
 *
 * @param <T>//from w  w w.j av a  2 s .co m
 *            the generic type of the return type
 * @param jsonString
 *            the json string
 * @param clazz
 *            the clazz of the generic type
 * @param modules
 *            The modules to register for the mapper
 * @return the object
 * @throws JsonParseException
 *             If an error occurs when parsing the string into Object
 * @throws JsonMappingException
 *             the If an error occurs when mapping the string into Object
 * @throws IOException
 *             Signals that an I/O exception has occurred.
 */
public static <T> T toObject(final String jsonString, final Class<T> clazz, final Module... modules)
        throws JsonParseException, JsonMappingException, IOException {
    ObjectMapper mapper = getObjectMapper(true);
    mapper = mapper.registerModules(modules);
    final T object = mapper.readValue(jsonString, clazz);
    return object;
}

From source file:com.arpnetworking.metrics.mad.configuration.PipelineConfiguration.java

/**
 * Create an <code>ObjectMapper</code> for Pipeline configuration.
 *
 * @param injector The Guice <code>Injector</code> instance.
 * @return An <code>ObjectMapper</code> for Pipeline configuration.
 *//*  w  w  w . j  a v  a 2  s . c  om*/
public static ObjectMapper createObjectMapper(final Injector injector) {
    final ObjectMapper objectMapper = ObjectMapperFactory.createInstance();

    final SimpleModule module = new SimpleModule("Pipeline");
    module.addDeserializer(Statistic.class, new StatisticDeserializer());

    objectMapper.registerModules(module);

    final GuiceAnnotationIntrospector guiceIntrospector = new GuiceAnnotationIntrospector();
    objectMapper.setInjectableValues(new GuiceInjectableValues(injector));
    objectMapper.setAnnotationIntrospectors(
            new AnnotationIntrospectorPair(guiceIntrospector,
                    objectMapper.getSerializationConfig().getAnnotationIntrospector()),
            new AnnotationIntrospectorPair(guiceIntrospector,
                    objectMapper.getDeserializationConfig().getAnnotationIntrospector()));

    return objectMapper;
}

From source file:io.coala.json.JsonUtil.java

/**
 * @param instance/*from ww w  .jav  a2  s  . c  o  m*/
 */
public synchronized static void initialize(final ObjectMapper om) {
    om.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);

    final Module[] modules = { new JodaModule(), new UUIDModule(), new JavaTimeModule() };
    om.registerModules(modules);
    LOG.trace("Using jackson v: {} with modules: {}", om.version(),
            Arrays.asList(modules).stream().map(m -> m.getModuleName()).collect(Collectors.toList()));
}

From source file:com.arpnetworking.tsdaggregator.configuration.TsdAggregatorConfiguration.java

/**
 * Create an <code>ObjectMapper</code> for TsdAggregator configuration.
 *
 * @return An <code>ObjectMapper</code> for TsdAggregator configuration.
 *//*from   ww w . j ava2  s. co m*/
public static ObjectMapper createObjectMapper() {
    final ObjectMapper objectMapper = ObjectMapperFactory.createInstance();

    final SimpleModule module = new SimpleModule("TsdAggregator");
    BuilderDeserializer.addTo(module, TsdAggregatorConfiguration.class);

    final Set<Class<? extends MetricsLimiter>> limiterClasses = INTERFACE_DATABASE
            .findClassesWithInterface(MetricsLimiter.class);
    for (final Class<? extends MetricsLimiter> limiterClass : limiterClasses) {
        BuilderDeserializer.addTo(module, limiterClass);
    }

    objectMapper.registerModules(module);

    return objectMapper;
}

From source file:sample.config.SessionConfig.java

/**
 * Customized {@link ObjectMapper} to add mix-in for class that doesn't have default
 * constructors/*from w  w w.j  a v a 2  s  .com*/
 *
 * @return the {@link ObjectMapper} to use
 */
ObjectMapper objectMapper() {
    ObjectMapper mapper = new ObjectMapper();
    mapper.registerModules(SecurityJackson2Modules.getModules(this.loader));
    return mapper;
}

From source file:com.github.relai.vertx.springdata.JacksonAutoWire.java

@PostConstruct
private void registerModulesWithObjectMappers() {
    Collection<Module> modules = getBeans(Module.class);
    for (ObjectMapper objectMapper : getBeans(ObjectMapper.class)) {
        objectMapper.registerModules(modules);
    }//from www.  ja  v a  2 s  .co  m
}

From source file:keywhiz.cli.CliModule.java

@Provides
public ObjectMapper generalMapper() {
    /**/*from  w w w. ja v  a 2s.  c  om*/
     * 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;
}

From source file:ws.salient.knowledge.ClasspathRepository.java

@Override
public KnowledgeBase getKnowledgeBase(String knowledgeBaseId) {
    KieServices kie = KieServices.Factory.get();
    KieContainerImpl container = (KieContainerImpl) kie.getKieClasspathContainer();
    ObjectMapper json = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
            .findAndRegisterModules();//from w  ww  .j a  va 2  s  . c  o  m
    json.registerModules(ObjectMapper.findModules(container.getClassLoader()));
    KieBase base = container.getKieBase(KnowledgeRepository.getKnowledgeBaseName(knowledgeBaseId));
    InternalKieModule module = container.getKieProject()
            .getKieModuleForKBase(KnowledgeRepository.getKnowledgeBaseName(knowledgeBaseId));
    FSTConfiguration serializer = FSTConfiguration.createDefaultConfiguration();
    serializer.setShareReferences(true);
    serializer.setForceSerializable(false);
    serializer.setClassLoader(container.getClassLoader());
    return new KnowledgeBase(KnowledgeRepository.getKnowledgeBaseName(knowledgeBaseId)).withBase(base)
            .withContainer(container).withJson(json).withModule(module).withReleaseId(container.getReleaseId())
            .withSerializer(serializer);
}

From source file:de.escalon.hypermedia.spring.hydra.HydraMessageConverter.java

/**
 * Creates new HydraMessageConverter with proxyUnwrapper.
 *
 * @param proxyUnwrapper//from  w  w w.  j  av a2 s.c o  m
 *         capable of unwrapping proxified Java beans during message conversion.
 */
public HydraMessageConverter(ProxyUnwrapper proxyUnwrapper, Module... additionalModules) {
    ObjectMapper objectMapper = new ObjectMapper();
    // see https://github.com/json-ld/json-ld.org/issues/76
    objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    objectMapper.registerModules(additionalModules);
    objectMapper.registerModule(new JacksonHydraModule(proxyUnwrapper));
    this.setObjectMapper(objectMapper);
    this.setSupportedMediaTypes(Arrays.asList(HypermediaTypes.APPLICATION_JSONLD));
}