Example usage for com.fasterxml.jackson.core.io SegmentedStringWriter getAndClear

List of usage examples for com.fasterxml.jackson.core.io SegmentedStringWriter getAndClear

Introduction

In this page you can find the example usage for com.fasterxml.jackson.core.io SegmentedStringWriter getAndClear.

Prototype

public String getAndClear() 

Source Link

Document

Main access method that will construct a String that contains all the contents, release all internal buffers we may have, and return result String.

Usage

From source file:org.elasticsearch.script.mustache.MustacheScriptEngineService.java

/**
 * Execute a compiled template object (as retrieved from the compile method)
 * and fill potential place holders with the variables given.
 *
 * @param template/*from  w  w w . ja  va 2  s .c  om*/
 *            compiled template object.
 * @param vars
 *            map of variables to use during substitution.
 *
 * @return the processed string with all given variables substitued.
 * */
public Object execute(Object template, Map<String, Object> vars) {
    SegmentedStringWriter result = new SegmentedStringWriter(new BufferRecycler());
    ((Mustache) template).execute(result, vars);
    return result.getAndClear();
}

From source file:net.logstash.logback.LogstashAbstractFormatter.java

public String writeValueAsString(EventType event, Context context) throws IOException {
    SegmentedStringWriter writer = new SegmentedStringWriter(getBufferRecycler());

    JsonGenerator generator = createGenerator(writer);
    writeValueToGenerator(generator, event, context);
    return writer.getAndClear();
}

From source file:net.logstash.logback.composite.CompositeJsonFormatter.java

public String writeEventAsString(Event event) throws IOException {
    SegmentedStringWriter writer = new SegmentedStringWriter(getBufferRecycler());

    JsonGenerator generator = createGenerator(writer);
    writeEventToGenerator(generator, event);
    writer.flush();/*from w w w.  j  a  v  a  2 s .  c o  m*/
    return writer.getAndClear();
}

From source file:com.github.jknack.handlebars.Jackson2Helper.java

@Override
public CharSequence apply(final Object context, final Options options) throws IOException {
    if (context == null) {
        return options.hash("default", "");
    }/* w  w  w  .  j a  v a 2  s  . co m*/
    String viewName = options.hash("view", "");
    JsonGenerator generator = null;
    try {
        final ObjectWriter writer;
        // do we need to use a view?
        if (!isEmpty(viewName)) {
            Class<?> viewClass = alias.get(viewName);
            if (viewClass == null) {
                viewClass = getClass().getClassLoader().loadClass(viewName);
            }
            writer = mapper.writerWithView(viewClass);
        } else {
            writer = mapper.writer();
        }
        JsonFactory jsonFactory = mapper.getFactory();

        SegmentedStringWriter output = new SegmentedStringWriter(jsonFactory._getBufferRecycler());

        // creates a json generator.
        generator = jsonFactory.createJsonGenerator(output);

        Boolean escapeHtml = options.hash("escapeHTML", Boolean.FALSE);
        // do we need to escape html?
        if (escapeHtml) {
            generator.setCharacterEscapes(new HtmlEscapes());
        }

        Boolean pretty = options.hash("pretty", Boolean.FALSE);

        // write the JSON output.
        if (pretty) {
            writer.withDefaultPrettyPrinter().writeValue(generator, context);
        } else {
            writer.writeValue(generator, context);
        }

        generator.close();

        return new Handlebars.SafeString(output.getAndClear());
    } catch (ClassNotFoundException ex) {
        throw new IllegalArgumentException(viewName, ex);
    } finally {
        if (generator != null && !generator.isClosed()) {
            generator.close();
        }
    }
}