Search in sources :

Example 1 with MapperFeature

use of com.fasterxml.jackson.databind.MapperFeature 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 2 with MapperFeature

use of com.fasterxml.jackson.databind.MapperFeature in project sling by apache.

the class JacksonExporter method export.

@Override
public <T> T export(@Nonnull Object model, @Nonnull Class<T> clazz, @Nonnull Map<String, String> options) throws ExportException {
    ObjectMapper mapper = new ObjectMapper();
    for (Map.Entry<String, String> optionEntry : options.entrySet()) {
        String key = optionEntry.getKey();
        if (key.startsWith(SERIALIZATION_FEATURE_PREFIX)) {
            String enumName = key.substring(SERIALIZATION_FEATURE_PREFIX_LENGTH);
            try {
                SerializationFeature feature = SerializationFeature.valueOf(enumName);
                mapper.configure(feature, Boolean.valueOf(optionEntry.getValue()));
            } catch (IllegalArgumentException e) {
                log.warn("Bad SerializationFeature option");
            }
        } else if (key.startsWith(MAPPER_FEATURE_PREFIX)) {
            String enumName = key.substring(MAPPER_FEATURE_PREFIX_LENGTH);
            try {
                MapperFeature feature = MapperFeature.valueOf(enumName);
                mapper.configure(feature, Boolean.valueOf(optionEntry.getValue()));
            } catch (IllegalArgumentException e) {
                log.warn("Bad SerializationFeature option");
            }
        }
    }
    for (ModuleProvider moduleProvider : moduleProviders) {
        mapper.registerModule(moduleProvider.getModule());
    }
    if (clazz.equals(Map.class)) {
        return (T) mapper.convertValue(model, Map.class);
    } else if (clazz.equals(String.class)) {
        final JsonFactory f = new JsonFactory();
        f.setCharacterEscapes(new EscapeCloseScriptBlocks());
        StringWriter writer = new StringWriter();
        JsonGenerator jgen;
        final boolean printTidy;
        if (options.containsKey("tidy")) {
            printTidy = Boolean.valueOf(options.get("tidy"));
        } else {
            printTidy = false;
        }
        try {
            jgen = f.createGenerator(writer);
            if (printTidy) {
                mapper.writerWithDefaultPrettyPrinter().writeValue(jgen, model);
            } else {
                mapper.writeValue(jgen, model);
            }
        } catch (final IOException e) {
            throw new ExportException(e);
        }
        return (T) writer.toString();
    } else {
        return null;
    }
}
Also used : JsonFactory(com.fasterxml.jackson.core.JsonFactory) SerializableString(com.fasterxml.jackson.core.SerializableString) IOException(java.io.IOException) SerializationFeature(com.fasterxml.jackson.databind.SerializationFeature) ExportException(org.apache.sling.models.factory.ExportException) ModuleProvider(org.apache.sling.models.jacksonexporter.ModuleProvider) StringWriter(java.io.StringWriter) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) MapperFeature(com.fasterxml.jackson.databind.MapperFeature) Map(java.util.Map) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 3 with MapperFeature

use of com.fasterxml.jackson.databind.MapperFeature in project camel by apache.

the class JacksonDataFormat method doStart.

@Override
protected void doStart() throws Exception {
    if (objectMapper == null) {
        objectMapper = new ObjectMapper();
    }
    if (enableJaxbAnnotationModule) {
        // Enables JAXB processing
        JaxbAnnotationModule module = new JaxbAnnotationModule();
        LOG.debug("Registering JaxbAnnotationModule: {}", module);
        objectMapper.registerModule(module);
    }
    if (useList) {
        setCollectionType(ArrayList.class);
    }
    if (include != null) {
        JsonInclude.Include inc = getCamelContext().getTypeConverter().mandatoryConvertTo(JsonInclude.Include.class, include);
        objectMapper.setSerializationInclusion(inc);
    }
    if (prettyPrint) {
        objectMapper.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) {
                objectMapper.enable(sf);
                continue;
            }
            DeserializationFeature df = getCamelContext().getTypeConverter().tryConvertTo(DeserializationFeature.class, enable);
            if (df != null) {
                objectMapper.enable(df);
                continue;
            }
            MapperFeature mf = getCamelContext().getTypeConverter().tryConvertTo(MapperFeature.class, enable);
            if (mf != null) {
                objectMapper.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) {
                objectMapper.disable(sf);
                continue;
            }
            DeserializationFeature df = getCamelContext().getTypeConverter().tryConvertTo(DeserializationFeature.class, disable);
            if (df != null) {
                objectMapper.disable(df);
                continue;
            }
            MapperFeature mf = getCamelContext().getTypeConverter().tryConvertTo(MapperFeature.class, disable);
            if (mf != null) {
                objectMapper.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.debug("Registering module: {}", module);
            objectMapper.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.debug("Registering module: {} -> {}", name, module);
            objectMapper.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.debug("Registering module: {} -> {}", name, module);
            objectMapper.registerModule(module);
        }
    }
}
Also used : DeserializationFeature(com.fasterxml.jackson.databind.DeserializationFeature) JsonInclude(com.fasterxml.jackson.annotation.JsonInclude) SerializationFeature(com.fasterxml.jackson.databind.SerializationFeature) 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) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

MapperFeature (com.fasterxml.jackson.databind.MapperFeature)3 SerializationFeature (com.fasterxml.jackson.databind.SerializationFeature)3 JsonInclude (com.fasterxml.jackson.annotation.JsonInclude)2 DeserializationFeature (com.fasterxml.jackson.databind.DeserializationFeature)2 Module (com.fasterxml.jackson.databind.Module)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 JaxbAnnotationModule (com.fasterxml.jackson.module.jaxb.JaxbAnnotationModule)2 JsonFactory (com.fasterxml.jackson.core.JsonFactory)1 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)1 SerializableString (com.fasterxml.jackson.core.SerializableString)1 XmlMapper (com.fasterxml.jackson.dataformat.xml.XmlMapper)1 IOException (java.io.IOException)1 StringWriter (java.io.StringWriter)1 Map (java.util.Map)1 ExportException (org.apache.sling.models.factory.ExportException)1 ModuleProvider (org.apache.sling.models.jacksonexporter.ModuleProvider)1