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

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

Introduction

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

Prototype

public DeserializationConfig getDeserializationConfig() 

Source Link

Document

Method that returns the shared default DeserializationConfig object that defines configuration settings for deserialization.

Usage

From source file:com.ethlo.geodata.restdocs.AbstractJacksonFieldSnippet.java

private void resolveFieldDescriptors(Map<String, FieldDescriptor> fieldDescriptors, Type type,
        ObjectMapper objectMapper, JavadocReader javadocReader, ConstraintReader constraintReader)
        throws JsonMappingException {
    FieldDocumentationGenerator generator = new FieldDocumentationGenerator(objectMapper.writer(),
            objectMapper.getDeserializationConfig(), javadocReader, constraintReader);
    List<FieldDescriptor> descriptors = generator.generateDocumentation(type, objectMapper.getTypeFactory());
    for (FieldDescriptor descriptor : descriptors) {
        if (fieldDescriptors.get(descriptor.getPath()) == null) {
            fieldDescriptors.put(descriptor.getPath(), descriptor);
        }/*from   w  ww  . j  av  a2s  .co  m*/
    }
}

From source file:com.arpnetworking.jackson.ObjectMapperFactoryTest.java

@Test
public void testNewInstance() {
    final ObjectMapper objectMapper1 = ObjectMapperFactory.createInstance();
    final ObjectMapper objectMapper2 = ObjectMapperFactory.createInstance();
    Assert.assertNotSame(objectMapper1, objectMapper2);

    // Deserialization feature
    objectMapper1.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, true);
    objectMapper2.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    Assert.assertTrue(objectMapper1.getDeserializationConfig()
            .isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES));
    Assert.assertFalse(objectMapper2.getDeserializationConfig()
            .isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES));

    // Serialization feature
    objectMapper1.configure(SerializationFeature.INDENT_OUTPUT, true);
    objectMapper2.configure(SerializationFeature.INDENT_OUTPUT, false);
    Assert.assertTrue(objectMapper1.getSerializationConfig().isEnabled(SerializationFeature.INDENT_OUTPUT));
    Assert.assertFalse(objectMapper2.getSerializationConfig().isEnabled(SerializationFeature.INDENT_OUTPUT));
}

From source file:org.springframework.data.rest.webmvc.json.JacksonMetadata.java

/**
 * Creates a new {@link JacksonMetadata} instance for the given {@link ObjectMapper} and type.
 * //from ww  w . ja  v  a 2 s . c  o m
 * @param mapper must not be {@literal null}.
 * @param type must not be {@literal null}.
 */
public JacksonMetadata(ObjectMapper mapper, Class<?> type) {

    Assert.notNull(mapper, "ObjectMapper must not be null!");
    Assert.notNull(type, "Type must not be null!");

    this.mapper = mapper;

    SerializationConfig serializationConfig = mapper.getSerializationConfig();
    JavaType javaType = serializationConfig.constructType(type);
    BeanDescription description = serializationConfig.introspect(javaType);

    this.definitions = description.findProperties();
    this.isValue = description.findJsonValueMethod() != null;

    DeserializationConfig deserializationConfig = mapper.getDeserializationConfig();
    JavaType deserializationType = deserializationConfig.constructType(type);

    this.deserializationDefinitions = deserializationConfig.introspect(deserializationType).findProperties();
}

From source file:info.archinnov.achilles.configuration.ArgumentExtractorTest.java

@Test
public void should_init_default_object_factory_mapper() throws Exception {
    JacksonMapperFactory actual = extractor.initObjectMapperFactory(configMap);

    assertThat(actual).isNotNull();/*from  w  w  w .j  av  a2  s .c o  m*/

    ObjectMapper mapper = actual.getMapper(Integer.class);

    assertThat(mapper).isNotNull();
    assertThat(mapper.getSerializationConfig().getSerializationInclusion())
            .isEqualTo(JsonInclude.Include.NON_NULL);
    assertThat(mapper.getDeserializationConfig().isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES))
            .isFalse();
    Collection<AnnotationIntrospector> ais = mapper.getSerializationConfig().getAnnotationIntrospector()
            .allIntrospectors();

    assertThat(ais).hasSize(2);
    Iterator<AnnotationIntrospector> iterator = ais.iterator();

    assertThat(iterator.next()).isInstanceOfAny(JacksonAnnotationIntrospector.class,
            JaxbAnnotationIntrospector.class);
    assertThat(iterator.next()).isInstanceOfAny(JacksonAnnotationIntrospector.class,
            JaxbAnnotationIntrospector.class);
}

From source file:com.addthis.codec.plugins.PluginRegistry.java

public PluginRegistry(@Nonnull Config config) {
    this.config = config;
    Config defaultPluginMapSettings = config.getConfig(PLUGIN_DEFAULTS_PATH);
    String pluginPath = config.getString(PLUGINS_PATH_PATH);
    Config pluginConfigs = config.getConfig(pluginPath);

    Set<String> categories = pluginConfigs.root().keySet();
    Map<String, PluginMap> mapsFromConfig = new HashMap<>(categories.size());
    BiMap<Class<?>, PluginMap> mapsFromConfigByClass = HashBiMap.create(categories.size());
    // Throwaway ObjectMapper to facilitate AnnotatedClass construction below
    ObjectMapper temp = new ObjectMapper();
    for (String category : categories) {
        Config pluginConfig = pluginConfigs.getConfig(category).withFallback(defaultPluginMapSettings);
        PluginMap pluginMap = new PluginMap(category, pluginConfig);
        mapsFromConfig.put(category, pluginMap);
        Class<?> baseClass = pluginMap.baseClass();
        if (baseClass != null) {
            // if two categories define _class, then ensure the annotated one (if any) is the canonical one
            if (mapsFromConfigByClass.containsKey(baseClass)) {
                AnnotatedClass annotatedClass = AnnotatedClass.construct(temp.constructType(baseClass),
                        temp.getDeserializationConfig());
                String existingCategory = mapsFromConfigByClass.get(baseClass).category();
                if (!annotatedClass.hasAnnotation(Pluggable.class)
                        || !annotatedClass.getAnnotation(Pluggable.class).value().equals(existingCategory)) {
                    mapsFromConfigByClass.put(pluginMap.baseClass(), pluginMap);
                }//from  w ww  . j  a v a  2 s. c o  m
            } else {
                mapsFromConfigByClass.put(pluginMap.baseClass(), pluginMap);
            }
        }
    }
    pluginMapsByCategory = Collections.unmodifiableMap(mapsFromConfig);
    pluginMapsByClass = Maps.unmodifiableBiMap(mapsFromConfigByClass);
}

From source file:com.arpnetworking.tsdaggregator.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.
 *//*from ww  w .j  a  v a 2 s .c  o m*/
public static ObjectMapper createObjectMapper(final Injector injector) {
    final ObjectMapper objectMapper = ObjectMapperFactory.createInstance();

    final SimpleModule module = new SimpleModule("Pipeline");
    BuilderDeserializer.addTo(module, PipelineConfiguration.class);

    final Set<Class<? extends Sink>> sinkClasses = INTERFACE_DATABASE.findClassesWithInterface(Sink.class);
    for (final Class<? extends Sink> sinkClass : sinkClasses) {
        BuilderDeserializer.addTo(module, sinkClass);
    }

    final Set<Class<? extends Source>> sourceClasses = INTERFACE_DATABASE
            .findClassesWithInterface(Source.class);
    for (final Class<? extends Source> sourceClass : sourceClasses) {
        BuilderDeserializer.addTo(module, sourceClass);
    }

    @SuppressWarnings({ "rawtypes", "unchecked" })
    final Set<Class<? extends Parser<?>>> parserClasses = INTERFACE_DATABASE
            .findClassesWithInterface((Class) Parser.class);
    for (final Class<? extends Parser<?>> parserClass : parserClasses) {
        BuilderDeserializer.addTo(module, parserClass);
    }

    final Set<Class<? extends DynamicConfigurationFactory>> dcFactoryClasses = INTERFACE_DATABASE
            .findClassesWithInterface(DynamicConfigurationFactory.class);
    for (final Class<? extends DynamicConfigurationFactory> dcFactoryClass : dcFactoryClasses) {
        BuilderDeserializer.addTo(module, dcFactoryClass);
    }

    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:org.apache.asterix.test.common.TestExecutor.java

public void cleanup(String testCase, List<String> badtestcases) throws Exception {
    try {/*from  ww  w  .ja v a  2  s  . c o  m*/
        ArrayList<String> toBeDropped = new ArrayList<>();
        InputStream resultStream = executeQueryService(
                "select dv.DataverseName from Metadata.`Dataverse` as dv;", getEndpoint(Servlets.QUERY_SERVICE),
                OutputFormat.CLEAN_JSON);
        String out = IOUtils.toString(resultStream);
        ObjectMapper om = new ObjectMapper();
        om.setConfig(
                om.getDeserializationConfig().with(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT));
        JsonNode result;
        try {
            result = om.readValue(out, ObjectNode.class).get("results");
        } catch (JsonMappingException e) {
            result = om.createArrayNode();
        }
        for (int i = 0; i < result.size(); i++) {
            JsonNode json = result.get(i);
            if (json != null) {
                String dvName = json.get("DataverseName").asText();
                if (!dvName.equals("Metadata") && !dvName.equals("Default")) {
                    toBeDropped.add(dvName);
                }
            }
        }
        if (!toBeDropped.isEmpty()) {
            badtestcases.add(testCase);
            LOGGER.warning(
                    "Last test left some garbage. Dropping dataverses: " + StringUtils.join(toBeDropped, ','));
            StringBuilder dropStatement = new StringBuilder();
            for (String dv : toBeDropped) {
                dropStatement.append("drop dataverse ");
                dropStatement.append(dv);
                dropStatement.append(";\n");
            }
            resultStream = executeQueryService(dropStatement.toString(), getEndpoint(Servlets.QUERY_SERVICE),
                    OutputFormat.CLEAN_JSON);
            ResultExtractor.extract(resultStream);
        }
    } catch (Throwable th) {
        th.printStackTrace();
        throw th;
    }
}

From source file:org.openengsb.core.util.JsonUtils.java

public static ObjectMapper createObjectMapperWithIntroSpectors() {
    ObjectMapper mapper = new ObjectMapper();
    AnnotationIntrospector primaryIntrospector = new JacksonAnnotationIntrospector();
    AnnotationIntrospector secondaryIntrospector = new JaxbAnnotationIntrospector(mapper.getTypeFactory());
    AnnotationIntrospector introspector = new AnnotationIntrospectorPair(primaryIntrospector,
            secondaryIntrospector);//from   w ww  . ja v a2 s.  c o  m
    mapper.getDeserializationConfig().withAppendedAnnotationIntrospector(introspector);
    mapper.getSerializationConfig().withAppendedAnnotationIntrospector(introspector);
    return mapper;
}

From source file:org.springframework.security.jackson2.SecurityJackson2Modules.java

public static void enableDefaultTyping(ObjectMapper mapper) {
    if (mapper != null) {
        TypeResolverBuilder<?> typeBuilder = mapper.getDeserializationConfig().getDefaultTyper(null);
        if (typeBuilder == null) {
            mapper.setDefaultTyping(createWhitelistedDefaultTyping());
        }//  w  w  w.j  a v a 2 s .  c  om
    }
}