Search in sources :

Example 26 with JsonGenerator

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonGenerator in project jackson-core by FasterXML.

the class TestJDKSerializability method _copyJson.

@SuppressWarnings("resource")
protected String _copyJson(JsonFactory f, String json, boolean useBytes) throws IOException {
    if (useBytes) {
        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        JsonGenerator jg = f.createGenerator(bytes);
        _copyJson(f, json, jg);
        return bytes.toString("UTF-8");
    }
    StringWriter sw = new StringWriter();
    JsonGenerator jg = f.createGenerator(sw);
    _copyJson(f, json, jg);
    return sw.toString();
}
Also used : JsonGenerator(com.fasterxml.jackson.core.JsonGenerator)

Example 27 with JsonGenerator

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonGenerator in project jackson-databind by FasterXML.

the class TestGeneratorUsingMapper method testPojoWriting.

/*
    /**********************************************************
    /* Tests for data binding integration
    /**********************************************************
     */
public void testPojoWriting() throws IOException {
    JsonFactory jf = new MappingJsonFactory();
    StringWriter sw = new StringWriter();
    JsonGenerator gen = jf.createGenerator(sw);
    gen.writeObject(new Pojo());
    gen.close();
    // trimming needed if main-level object has leading space
    String act = sw.toString().trim();
    assertEquals("{\"x\":4}", act);
}
Also used : StringWriter(java.io.StringWriter) JsonFactory(com.fasterxml.jackson.core.JsonFactory) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) SerializableString(com.fasterxml.jackson.core.SerializableString)

Example 28 with JsonGenerator

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonGenerator in project jackson-databind by FasterXML.

the class SequenceWriterTest method testSimpleArray.

public void testSimpleArray() throws Exception {
    StringWriter strw = new StringWriter();
    SequenceWriter w = WRITER.writeValuesAsArray(strw);
    w.write(new Bean(1)).write(new Bean(2)).writeAll(new Bean[] { new Bean(-7), new Bean(2) });
    w.close();
    assertEquals(aposToQuotes("[{'a':1},{'a':2},{'a':-7},{'a':2}]"), strw.toString());
    strw = new StringWriter();
    JsonGenerator gen = WRITER.getFactory().createGenerator(strw);
    w = WRITER.writeValuesAsArray(gen);
    Collection<Bean> bean = Collections.singleton(new Bean(3));
    w.write(new Bean(1)).write(null).writeAll((Iterable<Bean>) bean);
    w.close();
    gen.close();
    assertEquals(aposToQuotes("[{'a':1},null,{'a':3}]"), strw.toString());
}
Also used : StringWriter(java.io.StringWriter) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator)

Example 29 with JsonGenerator

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonGenerator in project jackson-databind by FasterXML.

the class SerializationFeaturesTest method testFlushingNotAutomatic.

public void testFlushingNotAutomatic() throws IOException {
    // but should not occur if configured otherwise
    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(SerializationFeature.FLUSH_AFTER_WRITE_VALUE, false);
    StringWriter sw = new StringWriter();
    JsonGenerator g = mapper.getFactory().createGenerator(sw);
    mapper.writeValue(g, Integer.valueOf(13));
    // no flushing now:
    assertEquals("", sw.toString());
    // except when actually flushing
    g.flush();
    assertEquals("13", sw.toString());
    g.close();
    // Also, same should happen with ObjectWriter
    sw = new StringWriter();
    g = mapper.getFactory().createGenerator(sw);
    ObjectWriter ow = mapper.writer();
    ow.writeValue(g, Integer.valueOf(99));
    assertEquals("", sw.toString());
    // except when actually flushing
    g.flush();
    assertEquals("99", sw.toString());
    g.close();
}
Also used : JsonGenerator(com.fasterxml.jackson.core.JsonGenerator)

Example 30 with JsonGenerator

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonGenerator in project geode by apache.

the class JSONUtils method formulateJsonForListQueriesCall.

public static String formulateJsonForListQueriesCall(Region<String, String> queryRegion) {
    HeapDataOutputStream outputStream = new HeapDataOutputStream(org.apache.geode.internal.Version.CURRENT);
    try {
        JsonGenerator generator = enableDisableJSONGeneratorFeature(getObjectMapper().getFactory().createGenerator((OutputStream) outputStream, JsonEncoding.UTF8));
        JsonWriter.writeQueryListAsJson(generator, "queries", queryRegion);
        generator.close();
        return new String(outputStream.toByteArray());
    } catch (IOException e) {
        throw new RuntimeException(e.getMessage());
    } finally {
        outputStream.close();
    }
}
Also used : HeapDataOutputStream(org.apache.geode.internal.HeapDataOutputStream) OutputStream(java.io.OutputStream) HeapDataOutputStream(org.apache.geode.internal.HeapDataOutputStream) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) IOException(java.io.IOException)

Aggregations

JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)711 KriptonJsonContext (com.abubusoft.kripton.KriptonJsonContext)257 KriptonByteArrayOutputStream (com.abubusoft.kripton.common.KriptonByteArrayOutputStream)257 KriptonRuntimeException (com.abubusoft.kripton.exception.KriptonRuntimeException)257 JacksonWrapperSerializer (com.abubusoft.kripton.persistence.JacksonWrapperSerializer)257 IOException (java.io.IOException)170 StringWriter (java.io.StringWriter)145 JsonFactory (com.fasterxml.jackson.core.JsonFactory)101 ByteArrayOutputStream (java.io.ByteArrayOutputStream)66 Map (java.util.Map)57 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)54 HashMap (java.util.HashMap)41 Test (org.junit.Test)30 File (java.io.File)27 ArrayList (java.util.ArrayList)26 OutputStream (java.io.OutputStream)25 Writer (java.io.Writer)22 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)21 SerializerProvider (com.fasterxml.jackson.databind.SerializerProvider)20 FileOutputStream (java.io.FileOutputStream)19