use of com.fasterxml.jackson.databind.SerializationFeature 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);
}
}
}
use of com.fasterxml.jackson.databind.SerializationFeature 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);
}
}
}
use of com.fasterxml.jackson.databind.SerializationFeature 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;
}
}
Aggregations