Search in sources :

Example 1 with ModuleProvider

use of org.apache.sling.models.jacksonexporter.ModuleProvider 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)

Aggregations

JsonFactory (com.fasterxml.jackson.core.JsonFactory)1 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)1 SerializableString (com.fasterxml.jackson.core.SerializableString)1 MapperFeature (com.fasterxml.jackson.databind.MapperFeature)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 SerializationFeature (com.fasterxml.jackson.databind.SerializationFeature)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