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.cedarsoft.serialization.jackson.IntegerSerializerTest.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, 12, Version.valueOf(1, 0, 0));

    generator.close();
    JsonUtils.assertJsonEquals("12", out.toString());
}

From source file:com.github.jonpeterson.jackson.module.interceptor.JsonInterceptingSerializer.java

@Override
public void serialize(T value, JsonGenerator generator, SerializerProvider provider) throws IOException {
    // serialize the value into a byte array buffer then parse it back out into a JsonNode tree
    // TODO: find a better way to convert the value into a tree
    JsonFactory factory = generator.getCodec().getFactory();
    ByteArrayOutputStream buffer = new ByteArrayOutputStream(4096);
    JsonGenerator bufferGenerator = factory.createGenerator(buffer);
    try {/*  w  ww.  j  a  va2  s.c  o  m*/
        delegate.serialize(value, bufferGenerator, provider);
    } finally {
        bufferGenerator.close();
    }
    JsonNode jsonNode = factory.createParser(buffer.toByteArray()).readValueAsTree();

    // execute interceptors on node
    for (JsonInterceptor interceptor : interceptors)
        jsonNode = interceptor.intercept(jsonNode, jsonNodeFactory);

    // write node
    generator.writeTree(jsonNode);
}

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

License:asdf

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

    getSerializer().serialize(generator, "asdf", Version.valueOf(1, 0, 0));

    generator.close();
    JsonUtils.assertJsonEquals("\"asdf\"", out.toString());
}

From source file:com.proteanplatform.web.core.mvc.JsonView.java

@Override
public void render(Map<String, ?> model, HttpServletRequest request, HttpServletResponse response)
        throws Exception {
    response.setContentType(getContentType());

    ObjectMapper mapper = new ObjectMapper();
    JsonFactory factory = mapper.getFactory();
    JsonGenerator json = null;

    try {/*ww w . j  av  a  2 s  . co m*/
        json = factory.createGenerator(response.getWriter());
        json.writeObject(model);
    } finally {
        if (json != null) {
            json.close();
        }
    }
}

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

@Test
public void testDouble() throws Exception {
    JsonFactory jsonFactory = JacksonSupport.getJsonFactory();
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    JsonGenerator generator = jsonFactory.createGenerator(out, JsonEncoding.UTF8);
    getSerializer().serialize(generator, 11133.4, Version.valueOf(1, 0, 0));
    generator.close();
    JsonUtils.assertJsonEquals("11133.4", out.toString());
}

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

@Test
public void testDouble2() throws Exception {
    JsonFactory jsonFactory = JacksonSupport.getJsonFactory();
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    JsonGenerator generator = jsonFactory.createGenerator(out, JsonEncoding.UTF8);
    getSerializer().serialize(generator, 11133.0, Version.valueOf(1, 0, 0));
    generator.close();
    JsonUtils.assertJsonEquals("11133.0", out.toString());
}

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

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

    getSerializer().serialize(generator, 11133, Version.valueOf(1, 0, 0));

    generator.close();
    JsonUtils.assertJsonEquals("11133", out.toString());
}

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

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

    NullSerializer serializer = getSerializer();
    serializer.serialize(generator, null, Version.valueOf(1, 0, 0));
    generator.close();
    JsonUtils.assertJsonEquals("null", out.toString());

    assertNull(serializer.deserialize(new ByteArrayInputStream(out.toByteArray())));
}

From source file:com.netflix.hystrix.contrib.sample.stream.HystrixUtilizationJsonStream.java

protected static String convertToJson(HystrixUtilization utilization) throws IOException {
    StringWriter jsonString = new StringWriter();
    JsonGenerator json = jsonFactory.createGenerator(jsonString);

    json.writeStartObject();//from  w  w  w  . jav  a  2  s .  co m
    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();

    return jsonString.getBuffer().toString();
}

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

@Test
public void testClose() throws IOException {
    File file = File.createTempFile("test", "", TEST_DIR);
    final TestCloseable closeable = new TestCloseable();
    try (FileOutputStream fos = new FileOutputStream(file)) {
        final JsonGenerator jg = objectMapper.getFactory().createGenerator(fos);
        jg.writeStartArray();/*  ww w  .j  a v a2 s . c  o m*/
        jg.writeEndArray();
        jg.close();
    }

    final JsonIterator<Map<String, Object>> jsonIterator = new JsonIterator(
            new TypeReference<Map<String, Object>>() {
            }, new FileInputStream(file), closeable, objectMapper);

    final SqlFirehose firehose = new SqlFirehose(ImmutableList.of(jsonIterator).iterator(), parser, closeable);
    firehose.hasMore(); // initialize lineIterator
    firehose.close();
    Assert.assertTrue(closeable.closed);
}