Search in sources :

Example 21 with XmlMapper

use of com.fasterxml.jackson.dataformat.xml.XmlMapper in project torodb by torodb.

the class ConfigUtils method getParam.

public static <T> JsonNode getParam(T config, String pathAndProp) throws Exception {
    XmlMapper xmlMapper = xmlMapper();
    JsonNode configNode = xmlMapper.valueToTree(config);
    if (JsonPointer.compile(pathAndProp).equals(JsonPointer.compile("/"))) {
        return configNode;
    }
    JsonPointer pathPointer = JsonPointer.compile(pathAndProp);
    JsonNode pathNode = configNode.at(pathPointer);
    if (pathNode.isMissingNode() || pathNode.isNull()) {
        return null;
    }
    return pathNode;
}
Also used : JsonNode(com.fasterxml.jackson.databind.JsonNode) JsonPointer(com.fasterxml.jackson.core.JsonPointer) XmlMapper(com.fasterxml.jackson.dataformat.xml.XmlMapper)

Example 22 with XmlMapper

use of com.fasterxml.jackson.dataformat.xml.XmlMapper in project torodb by torodb.

the class ConfigUtils method readConfigFromXml.

public static <T> T readConfigFromXml(Class<T> configClass, String xmlString) throws JsonProcessingException, IOException {
    ObjectMapper objectMapper = mapper();
    XmlMapper xmlMapper = xmlMapper();
    JsonNode configNode = xmlMapper.readTree(xmlString);
    T config = objectMapper.treeToValue(configNode, configClass);
    validateBean(config);
    return config;
}
Also used : JsonNode(com.fasterxml.jackson.databind.JsonNode) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) XmlMapper(com.fasterxml.jackson.dataformat.xml.XmlMapper)

Example 23 with XmlMapper

use of com.fasterxml.jackson.dataformat.xml.XmlMapper in project spring-framework by spring-projects.

the class Jackson2ObjectMapperFactoryBeanTests method setObjectMapper.

@Test
public void setObjectMapper() {
    this.factory.setObjectMapper(new XmlMapper());
    this.factory.afterPropertiesSet();
    assertNotNull(this.factory.getObject());
    assertTrue(this.factory.isSingleton());
    assertEquals(XmlMapper.class, this.factory.getObjectType());
}
Also used : XmlMapper(com.fasterxml.jackson.dataformat.xml.XmlMapper) Test(org.junit.Test)

Example 24 with XmlMapper

use of com.fasterxml.jackson.dataformat.xml.XmlMapper in project camel by apache.

the class JacksonXMLDataFormat method doStart.

@Override
protected void doStart() throws Exception {
    if (xmlMapper == null) {
        xmlMapper = new XmlMapper();
    }
    if (enableJaxbAnnotationModule) {
        // Enables JAXB processing
        JaxbAnnotationModule module = new JaxbAnnotationModule();
        LOG.info("Registering module: {}", module);
        xmlMapper.registerModule(module);
    }
    if (useList) {
        setCollectionType(ArrayList.class);
    }
    if (include != null) {
        JsonInclude.Include inc = getCamelContext().getTypeConverter().mandatoryConvertTo(JsonInclude.Include.class, include);
        xmlMapper.setSerializationInclusion(inc);
    }
    if (prettyPrint) {
        xmlMapper.enable(SerializationFeature.INDENT_OUTPUT);
    }
    if (enableFeatures != null) {
        Iterator<Object> it = ObjectHelper.createIterator(enableFeatures);
        while (it.hasNext()) {
            String enable = it.next().toString();
            // it can be different kind
            SerializationFeature sf = getCamelContext().getTypeConverter().tryConvertTo(SerializationFeature.class, enable);
            if (sf != null) {
                xmlMapper.enable(sf);
                continue;
            }
            DeserializationFeature df = getCamelContext().getTypeConverter().tryConvertTo(DeserializationFeature.class, enable);
            if (df != null) {
                xmlMapper.enable(df);
                continue;
            }
            MapperFeature mf = getCamelContext().getTypeConverter().tryConvertTo(MapperFeature.class, enable);
            if (mf != null) {
                xmlMapper.enable(mf);
                continue;
            }
            throw new IllegalArgumentException("Enable feature: " + enable + " cannot be converted to an accepted enum of types [SerializationFeature,DeserializationFeature,MapperFeature]");
        }
    }
    if (disableFeatures != null) {
        Iterator<Object> it = ObjectHelper.createIterator(disableFeatures);
        while (it.hasNext()) {
            String disable = it.next().toString();
            // it can be different kind
            SerializationFeature sf = getCamelContext().getTypeConverter().tryConvertTo(SerializationFeature.class, disable);
            if (sf != null) {
                xmlMapper.disable(sf);
                continue;
            }
            DeserializationFeature df = getCamelContext().getTypeConverter().tryConvertTo(DeserializationFeature.class, disable);
            if (df != null) {
                xmlMapper.disable(df);
                continue;
            }
            MapperFeature mf = getCamelContext().getTypeConverter().tryConvertTo(MapperFeature.class, disable);
            if (mf != null) {
                xmlMapper.disable(mf);
                continue;
            }
            throw new IllegalArgumentException("Disable feature: " + disable + " cannot be converted to an accepted enum of types [SerializationFeature,DeserializationFeature,MapperFeature]");
        }
    }
    if (modules != null) {
        for (Module module : modules) {
            LOG.info("Registering module: {}", module);
            xmlMapper.registerModules(module);
        }
    }
    if (moduleClassNames != null) {
        Iterable<Object> it = ObjectHelper.createIterable(moduleClassNames);
        for (Object o : it) {
            String name = o.toString();
            Class<Module> clazz = camelContext.getClassResolver().resolveMandatoryClass(name, Module.class);
            Module module = camelContext.getInjector().newInstance(clazz);
            LOG.info("Registering module: {} -> {}", name, module);
            xmlMapper.registerModule(module);
        }
    }
    if (moduleRefs != null) {
        Iterable<Object> it = ObjectHelper.createIterable(moduleRefs);
        for (Object o : it) {
            String name = o.toString();
            if (name.startsWith("#")) {
                name = name.substring(1);
            }
            Module module = CamelContextHelper.mandatoryLookup(camelContext, name, Module.class);
            LOG.info("Registering module: {} -> {}", name, module);
            xmlMapper.registerModule(module);
        }
    }
}
Also used : DeserializationFeature(com.fasterxml.jackson.databind.DeserializationFeature) JsonInclude(com.fasterxml.jackson.annotation.JsonInclude) SerializationFeature(com.fasterxml.jackson.databind.SerializationFeature) XmlMapper(com.fasterxml.jackson.dataformat.xml.XmlMapper) JaxbAnnotationModule(com.fasterxml.jackson.module.jaxb.JaxbAnnotationModule) MapperFeature(com.fasterxml.jackson.databind.MapperFeature) Module(com.fasterxml.jackson.databind.Module) JaxbAnnotationModule(com.fasterxml.jackson.module.jaxb.JaxbAnnotationModule)

Example 25 with XmlMapper

use of com.fasterxml.jackson.dataformat.xml.XmlMapper in project oap by oaplatform.

the class Binder method initialize.

private static ObjectMapper initialize(ObjectMapper mapper, boolean defaultTyping, boolean nonNullInclusion) {
    if (mapper instanceof XmlMapper) {
        ((XmlMapper) mapper).setDefaultUseWrapper(false);
    }
    AnnotationIntrospector introspector = new JacksonAnnotationIntrospector();
    mapper.getDeserializationConfig().with(introspector);
    mapper.getSerializationConfig().with(introspector);
    mapper.registerModule(new AfterburnerModule());
    mapper.registerModule(new Jdk8Module().configureAbsentsAsNulls(true));
    mapper.registerModule(new JodaModule());
    mapper.registerModule(new ParameterNamesModule(JsonCreator.Mode.DEFAULT));
    mapper.enable(DeserializationFeature.USE_LONG_FOR_INTS);
    mapper.enable(JsonParser.Feature.ALLOW_SINGLE_QUOTES);
    mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
    mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
    mapper.disable(SerializationFeature.WRITE_EMPTY_JSON_ARRAYS);
    mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
    mapper.configure(JsonGenerator.Feature.AUTO_CLOSE_TARGET, false);
    mapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
    if (!nonNullInclusion)
        mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    modules.forEach(mapper::registerModule);
    if (defaultTyping)
        mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);
    return mapper;
}
Also used : JacksonAnnotationIntrospector(com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector) AfterburnerModule(com.fasterxml.jackson.module.afterburner.AfterburnerModule) Jdk8Module(com.fasterxml.jackson.datatype.jdk8.Jdk8Module) ParameterNamesModule(com.fasterxml.jackson.module.paramnames.ParameterNamesModule) JodaModule(com.fasterxml.jackson.datatype.joda.JodaModule) JacksonAnnotationIntrospector(com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector) AnnotationIntrospector(com.fasterxml.jackson.databind.AnnotationIntrospector) XmlMapper(com.fasterxml.jackson.dataformat.xml.XmlMapper)

Aggregations

XmlMapper (com.fasterxml.jackson.dataformat.xml.XmlMapper)33 Test (org.junit.Test)15 IOException (java.io.IOException)8 JacksonXmlModule (com.fasterxml.jackson.dataformat.xml.JacksonXmlModule)5 ByteArrayInputStream (java.io.ByteArrayInputStream)5 InputStream (java.io.InputStream)5 BadRequestException (ninja.exceptions.BadRequestException)5 File (java.io.File)4 JsonNode (com.fasterxml.jackson.databind.JsonNode)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 JsonParser (com.fasterxml.jackson.core.JsonParser)2 DeserializationContext (com.fasterxml.jackson.databind.DeserializationContext)2 DeserializationProblemHandler (com.fasterxml.jackson.databind.deser.DeserializationProblemHandler)2 AfterburnerModule (com.fasterxml.jackson.module.afterburner.AfterburnerModule)2 HashMap (java.util.HashMap)2 NinjaTest (ninja.NinjaTest)2 JsonInclude (com.fasterxml.jackson.annotation.JsonInclude)1 JsonPointer (com.fasterxml.jackson.core.JsonPointer)1 AnnotationIntrospector (com.fasterxml.jackson.databind.AnnotationIntrospector)1 DeserializationFeature (com.fasterxml.jackson.databind.DeserializationFeature)1