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

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

Introduction

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

Prototype

@Override
public abstract void close() throws IOException;

Source Link

Document

Method called to close this generator, so that no more content can be written.

Usage

From source file:com.jayway.jsonpath.internal.spi.json.JacksonJsonProvider.java

@Override
public String toJson(Object obj) {
    StringWriter writer = new StringWriter();
    try {//w  ww  . j a va 2 s.com
        JsonGenerator generator = objectMapper.getFactory().createGenerator(writer);
        objectMapper.writeValue(generator, obj);
        writer.flush();
        writer.close();
        generator.close();
        return writer.getBuffer().toString();
    } catch (IOException e) {
        throw new InvalidJsonException();
    }
}

From source file:com.netflix.hystrix.serial.SerialHystrixUtilization.java

private static void serializeUtilization(HystrixUtilization utilization, JsonGenerator json) {
    try {/*from  w ww  .  jav  a2s.c  o  m*/
        json.writeStartObject();
        json.writeStringField("type", "HystrixUtilization");
        json.writeObjectFieldStart("commands");
        for (Map.Entry<HystrixCommandKey, HystrixCommandUtilization> entry : utilization
                .getCommandUtilizationMap().entrySet()) {
            final HystrixCommandKey key = entry.getKey();
            final HystrixCommandUtilization commandUtilization = entry.getValue();
            writeCommandUtilizationJson(json, key, commandUtilization);

        }
        json.writeEndObject();

        json.writeObjectFieldStart("threadpools");
        for (Map.Entry<HystrixThreadPoolKey, HystrixThreadPoolUtilization> entry : utilization
                .getThreadPoolUtilizationMap().entrySet()) {
            final HystrixThreadPoolKey threadPoolKey = entry.getKey();
            final HystrixThreadPoolUtilization threadPoolUtilization = entry.getValue();
            writeThreadPoolUtilizationJson(json, threadPoolKey, threadPoolUtilization);
        }
        json.writeEndObject();
        json.writeEndObject();
        json.close();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:com.cedarsoft.couchdb.io.ActionFailedExceptionSerializer.java

public void serialize(@Nonnull ActionFailedException object, @Nonnull OutputStream out) throws IOException {
    JsonFactory jsonFactory = JacksonSupport.getJsonFactory();
    JsonGenerator generator = jsonFactory.createJsonGenerator(out, JsonEncoding.UTF8);

    generator.writeStartObject();//from w w w.  j a va 2 s.c  o m

    serialize(generator, object);
    generator.writeEndObject();

    generator.close();
}

From source file:com.cedarsoft.couchdb.io.CouchDocSerializer.java

public <T> void serialize(@Nonnull CouchDoc<T> doc, @Nonnull JacksonSerializer<? super T> wrappedSerializer,
        @Nonnull OutputStream out) throws IOException {
    JsonGenerator generator = RawCouchDocSerializer.createJsonGenerator(out);

    serialize(doc, wrappedSerializer, generator);
    generator.close();
}

From source file:com.ning.metrics.action.hdfs.data.RowSmile.java

/**
 * Serialize the row into the DataOutput
 *
 * @param out DataOutput to write/*from   w  ww. j a  v a2 s .  c o m*/
 * @throws java.io.IOException generic serialization error
 */
@Override
public void write(DataOutput out) throws IOException {
    schema.write(out);
    WritableUtils.writeVInt(out, data.size());

    ByteArrayOutputStream outStream = new ByteArrayOutputStream();
    JsonGenerator gen = objectMapper.getJsonFactory().createJsonGenerator(outStream, JsonEncoding.UTF8);
    for (JsonNodeComparable dataItem : data) {
        objectMapper.writeValue(gen, dataItem);
    }
    gen.close();

    // Size of Smile payload. Needed for deserialization, see below
    WritableUtils.writeVInt(out, outStream.size());

    out.write(outStream.toByteArray());
}

From source file:org.hawkular.apm.tests.dockerized.TestScenarioRunner.java

private String serialize(Object object) throws IOException {
    StringWriter out = new StringWriter();

    JsonGenerator gen = objectMapper.getFactory().createGenerator(out);
    gen.writeObject(object);/*from  ww w. ja  v a  2  s .  c  o  m*/

    gen.close();
    out.close();

    return out.toString();
}

From source file:com.cedarsoft.serialization.jackson.ListSerializerTest.java

@Test
public void testIt() throws Exception {
    JsonFactory jsonFactory = JacksonSupport.getJsonFactory();
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    JsonGenerator generator = jsonFactory.createGenerator(out, JsonEncoding.UTF8);

    getSerializer().serialize(generator, Arrays.asList("a", "b", "c", 42, 3.141, false, null),
            Version.valueOf(1, 0, 0));/*from  w  ww  .  j  a  v a  2  s  .  com*/

    generator.close();
    JsonUtils.assertJsonEquals("[ \"a\", \"b\", \"c\", 42, 3.141, false, null ]", out.toString());

    List<? extends Object> deserialized = getSerializer()
            .deserialize(new ByteArrayInputStream(out.toByteArray()));
    assertEquals(7, deserialized.size());
    assertEquals("a", deserialized.get(0));
    assertEquals("b", deserialized.get(1));
    assertEquals("c", deserialized.get(2));
    assertEquals(42, deserialized.get(3));
    assertEquals(3.141, deserialized.get(4));
    assertEquals(false, deserialized.get(5));
    assertEquals(null, deserialized.get(6));
}

From source file:io.druid.data.input.impl.SqlFirehoseTest.java

@Before
public void setup() throws IOException {
    TEST_DIR = File.createTempFile(SqlFirehose.class.getSimpleName(), "testDir");
    FileUtils.forceDelete(TEST_DIR);//  www . ja  v a2 s  .c  o m
    FileUtils.forceMkdir(TEST_DIR);

    final List<Map<String, Object>> inputTexts = ImmutableList.of(
            ImmutableMap.of("x", "foostring1", "timestamp", 2000),
            ImmutableMap.of("x", "foostring2", "timestamp", 2000));
    List<FileInputStream> testFile = new ArrayList<>();
    this.objectMapper = new ObjectMapper(new SmileFactory());
    int i = 0;
    for (Map m : inputTexts) {
        File file = new File(TEST_DIR, "test_" + i++);
        try (FileOutputStream fos = new FileOutputStream(file)) {
            final JsonGenerator jg = objectMapper.getFactory().createGenerator(fos);
            jg.writeStartArray();
            jg.writeObject(m);
            jg.writeEndArray();
            jg.close();
            testFile.add(new FileInputStream(file));
        }
    }

    this.fileList = testFile;
    parser = new MapInputRowParser(new TimeAndDimsParseSpec(new TimestampSpec("timestamp", "auto", null),
            new DimensionsSpec(DimensionsSpec.getDefaultSchemas(ImmutableList.of("x")), null, null)));

    this.inputs = inputTexts;
}

From source file:de.softwareforge.streamery.JsonStreamer.java

protected void writeEnd(JsonGenerator jg, int count, boolean success) throws IOException {
    jg.writeEndArray();/*from   w w  w.  j  a  va2  s. com*/
    if (success) {
        jg.writeNumberField("count", count);
    }
    jg.writeBooleanField("success", success);
    jg.writeEndObject();
    jg.flush();
    jg.close();
}