Search in sources :

Example 61 with JsonGenerator

use of com.fasterxml.jackson.core.JsonGenerator in project jackson-core by FasterXML.

the class GeneratorFailFromReaderTest method _testFailOnWritingStringFromReaderWithTooFewCharacters.

private void _testFailOnWritingStringFromReaderWithTooFewCharacters(JsonFactory f, boolean useReader) throws Exception {
    JsonGenerator gen;
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    if (useReader) {
        gen = f.createGenerator(new OutputStreamWriter(bout, "UTF-8"));
    } else {
        gen = f.createGenerator(bout, JsonEncoding.UTF8);
    }
    gen.writeStartObject();
    try {
        String testStr = "aaaaaaaaa";
        StringReader reader = new StringReader(testStr);
        gen.writeFieldName("a");
        gen.writeString(reader, testStr.length() + 1);
        gen.flush();
        String json = bout.toString("UTF-8");
        fail("Should not have let " + gen.getClass().getName() + ".writeString() ': output = " + json);
    } catch (JsonProcessingException e) {
        verifyException(e, "Didn't read enough from reader");
    }
    gen.close();
}
Also used : StringReader(java.io.StringReader) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) OutputStreamWriter(java.io.OutputStreamWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 62 with JsonGenerator

use of com.fasterxml.jackson.core.JsonGenerator in project jackson-core by FasterXML.

the class GeneratorFailFromReaderTest method _testFailOnWritingStringFromNullReader.

private void _testFailOnWritingStringFromNullReader(JsonFactory f, boolean useReader) throws Exception {
    JsonGenerator gen;
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    if (useReader) {
        gen = f.createGenerator(new OutputStreamWriter(bout, "UTF-8"));
    } else {
        gen = f.createGenerator(bout, JsonEncoding.UTF8);
    }
    gen.writeStartObject();
    try {
        gen.writeFieldName("a");
        gen.writeString(null, -1);
        gen.flush();
        String json = bout.toString("UTF-8");
        fail("Should not have let " + gen.getClass().getName() + ".writeString() ': output = " + json);
    } catch (JsonProcessingException e) {
        verifyException(e, "null reader");
    }
    gen.close();
}
Also used : JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) OutputStreamWriter(java.io.OutputStreamWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 63 with JsonGenerator

use of com.fasterxml.jackson.core.JsonGenerator in project jackson-core by FasterXML.

the class GeneratorFailFromReaderTest method _testFailOnWritingStringNotFieldName.

/*
    /**********************************************************
    /* Internal methods
    /**********************************************************
     */
private void _testFailOnWritingStringNotFieldName(JsonFactory f, boolean useReader) throws Exception {
    JsonGenerator gen;
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    if (useReader) {
        gen = f.createGenerator(new OutputStreamWriter(bout, "UTF-8"));
    } else {
        gen = f.createGenerator(bout, JsonEncoding.UTF8);
    }
    gen.writeStartObject();
    try {
        StringReader reader = new StringReader("a");
        gen.writeString(reader, -1);
        gen.flush();
        String json = bout.toString("UTF-8");
        fail("Should not have let " + gen.getClass().getName() + ".writeString() be used in place of 'writeFieldName()': output = " + json);
    } catch (JsonProcessingException e) {
        verifyException(e, "can not write a String");
    }
    gen.close();
}
Also used : StringReader(java.io.StringReader) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) OutputStreamWriter(java.io.OutputStreamWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 64 with JsonGenerator

use of 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 65 with JsonGenerator

use of 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)

Aggregations

JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)187 StringWriter (java.io.StringWriter)81 IOException (java.io.IOException)52 JsonFactory (com.fasterxml.jackson.core.JsonFactory)44 ByteArrayOutputStream (java.io.ByteArrayOutputStream)25 Map (java.util.Map)17 OutputStream (java.io.OutputStream)14 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)13 JsonParser (com.fasterxml.jackson.core.JsonParser)11 ArrayList (java.util.ArrayList)11 HashMap (java.util.HashMap)10 Test (org.junit.Test)10 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)8 File (java.io.File)7 OutputStreamWriter (java.io.OutputStreamWriter)6 ServletOutputStream (javax.servlet.ServletOutputStream)6 HeapDataOutputStream (org.apache.geode.internal.HeapDataOutputStream)6 List (java.util.List)5 AccessExecutionVertex (org.apache.flink.runtime.executiongraph.AccessExecutionVertex)5 JsonEncoding (com.fasterxml.jackson.core.JsonEncoding)4