Example usage for com.fasterxml.jackson.core JsonGenerator configure

List of usage examples for com.fasterxml.jackson.core JsonGenerator configure

Introduction

In this page you can find the example usage for com.fasterxml.jackson.core JsonGenerator configure.

Prototype

public final JsonGenerator configure(Feature f, boolean state) 

Source Link

Document

Method for enabling or disabling specified feature: check Feature for list of available features.

Usage

From source file:org.jberet.support.io.JsonItemWriter.java

protected static JsonGenerator configureJsonGenerator(final JsonFactory jsonFactory,
        final OutputStream outputStream, final Class<?> outputDecorator,
        final Map<String, String> jsonGeneratorFeatures) throws Exception {
    if (outputDecorator != null) {
        jsonFactory.setOutputDecorator((OutputDecorator) outputDecorator.newInstance());
    }/*from w w  w. j ava2 s.  c  o  m*/
    final JsonGenerator jsonGenerator = jsonFactory.createGenerator(outputStream);

    if (jsonGeneratorFeatures != null) {
        for (final Map.Entry<String, String> e : jsonGeneratorFeatures.entrySet()) {
            final String key = e.getKey();
            final String value = e.getValue();
            final JsonGenerator.Feature feature;
            try {
                feature = JsonGenerator.Feature.valueOf(key);
            } catch (final Exception e1) {
                throw SupportMessages.MESSAGES.unrecognizedReaderWriterProperty(key, value);
            }
            if ("true".equals(value)) {
                if (!feature.enabledByDefault()) {
                    jsonGenerator.configure(feature, true);
                }
            } else if ("false".equals(value)) {
                if (feature.enabledByDefault()) {
                    jsonGenerator.configure(feature, false);
                }
            } else {
                throw SupportMessages.MESSAGES.invalidReaderWriterProperty(null, value, key);
            }
        }
    }

    return jsonGenerator;
}

From source file:de.odysseus.staxon.json.stream.jackson.JacksonStreamFactory.java

protected JsonGenerator configure(JsonGenerator generator, boolean pretty) {
    generator.configure(JsonGenerator.Feature.AUTO_CLOSE_TARGET, false);
    if (pretty) {
        generator.useDefaultPrettyPrinter();
    }//from w w  w  .ja v  a2  s.c o  m
    return generator;
}

From source file:com.ning.metrics.action.hdfs.reader.HdfsListing.java

@SuppressWarnings({ "unchecked", "unused" })
public void toJson(final OutputStream out, final boolean pretty) throws IOException {
    final String parentPath = getParentPath() == null ? "" : getParentPath();

    final JsonGenerator generator = new JsonFactory().createJsonGenerator(out);
    generator.configure(JsonGenerator.Feature.AUTO_CLOSE_TARGET, false);
    if (pretty) {
        generator.setPrettyPrinter(new DefaultPrettyPrinter());
    }// w w  w  .  ja v a  2  s  .  c  o m

    generator.writeStartObject();
    generator.writeObjectField(JSON_LISTING_PATH, getPath());
    generator.writeObjectField(JSON_LISTING_PARENT_PATH, parentPath);
    generator.writeArrayFieldStart(JSON_LISTING_ENTRIES);
    // Important: need to flush before appending pre-serialized events
    generator.flush();

    for (HdfsEntry entry : getEntries()) {
        entry.toJson(generator);
    }
    generator.writeEndArray();

    generator.writeEndObject();
    generator.close();
}

From source file:net.solarnetwork.web.support.JSONView.java

@Override
protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request,
        HttpServletResponse response) throws Exception {

    PropertyEditorRegistrar registrar = this.propertyEditorRegistrar;
    Enumeration<String> attrEnum = request.getAttributeNames();
    while (attrEnum.hasMoreElements()) {
        String key = attrEnum.nextElement();
        Object val = request.getAttribute(key);
        if (val instanceof PropertyEditorRegistrar) {
            registrar = (PropertyEditorRegistrar) val;
            break;
        }//from  w w  w  . j a  v a 2 s . c  o m
    }

    response.setCharacterEncoding(UTF8_CHAR_ENCODING);
    response.setContentType(getContentType());
    Writer writer = response.getWriter();
    if (this.includeParentheses) {
        writer.write('(');
    }
    JsonGenerator json = new JsonFactory().createGenerator(writer);
    json.configure(JsonGenerator.Feature.AUTO_CLOSE_TARGET, false);
    if (indentAmount > 0) {
        json.useDefaultPrettyPrinter();
    }
    json.writeStartObject();
    for (String key : model.keySet()) {
        Object val = model.get(key);
        writeJsonValue(json, key, val, registrar);
    }
    json.writeEndObject();
    json.close();
    if (this.includeParentheses) {
        writer.write(')');
    }
}