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();
}
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();
}
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();
}
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);
}
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());
}
Aggregations