Example usage for com.fasterxml.jackson.dataformat.smile SmileFactory createJsonGenerator

List of usage examples for com.fasterxml.jackson.dataformat.smile SmileFactory createJsonGenerator

Introduction

In this page you can find the example usage for com.fasterxml.jackson.dataformat.smile SmileFactory createJsonGenerator.

Prototype

@Deprecated
@Override
public SmileGenerator createJsonGenerator(OutputStream out) throws IOException 

Source Link

Usage

From source file:com.ning.metrics.serialization.event.TestSmileEnvelopeEvent.java

@BeforeTest
public void setUp() throws IOException {
    // Use same configuration as SmileEnvelopeEvent
    final SmileFactory f = new SmileFactory();
    f.configure(SmileGenerator.Feature.CHECK_SHARED_NAMES, true);
    f.configure(SmileGenerator.Feature.CHECK_SHARED_STRING_VALUES, true);
    f.configure(SmileParser.Feature.REQUIRE_HEADER, false);

    final ByteArrayOutputStream stream = new ByteArrayOutputStream();
    final JsonGenerator g = f.createJsonGenerator(stream);

    g.writeStartObject();//w ww  . j  av  a2  s . c om
    g.writeStringField(SmileEnvelopeEvent.SMILE_EVENT_GRANULARITY_TOKEN_NAME, eventGranularity.toString());
    g.writeObjectFieldStart("name");
    g.writeStringField("first", "Joe");
    g.writeStringField("last", "Sixpack");
    g.writeEndObject(); // for field 'name'
    g.writeStringField("gender", "MALE");
    g.writeNumberField(SmileEnvelopeEvent.SMILE_EVENT_DATETIME_TOKEN_NAME, eventDateTime.getMillis());
    g.writeBooleanField("verified", false);
    g.writeEndObject();
    g.close(); // important: will force flushing of output, close underlying output stream

    serializedBytes = stream.toByteArray();
    // one sanity check; should be able to round-trip via String (iff using latin-1!)
    serializedString = stream.toString(SmileEnvelopeEvent.CHARSET.toString());
}