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

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

Introduction

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

Prototype

public SegmentedStringWriter(BufferRecycler br) 

Source Link

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.  j  av a  2  s  .  com
 *            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: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", "");
    }//from w ww .  j a v  a 2  s . c  om
    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();
        }
    }
}

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();//w  w  w.j  a  va2  s.co m
    return writer.getAndClear();
}