use of com.fasterxml.jackson.core.json.JsonFactory in project jackson-core by FasterXML.
the class TestGeneratorObject method testInvalidObjectWrite.
public void testInvalidObjectWrite() throws Exception {
StringWriter sw = new StringWriter();
JsonGenerator gen = new JsonFactory().createGenerator(ObjectWriteContext.empty(), sw);
gen.writeStartObject();
// Mismatch:
try {
gen.writeEndArray();
fail("Expected an exception for mismatched array/object write");
} catch (JsonGenerationException e) {
verifyException(e, "Current context not Array");
}
gen.close();
}
use of com.fasterxml.jackson.core.json.JsonFactory in project jackson-core by FasterXML.
the class TestGeneratorObject method testConvenienceMethodsWithNulls.
/**
* Tests to cover [JACKSON-164]
*/
public void testConvenienceMethodsWithNulls() throws Exception {
StringWriter sw = new StringWriter();
JsonGenerator gen = new JsonFactory().createGenerator(ObjectWriteContext.empty(), sw);
gen.writeStartObject();
gen.writeStringField("str", null);
gen.writeNumberField("num", null);
gen.writeObjectField("obj", null);
gen.writeEndObject();
gen.close();
String docStr = sw.toString();
JsonParser jp = createParserUsingReader(docStr);
assertEquals(JsonToken.START_OBJECT, jp.nextToken());
assertEquals(JsonToken.FIELD_NAME, jp.nextToken());
assertEquals("str", jp.currentName());
assertEquals(JsonToken.VALUE_NULL, jp.nextToken());
assertEquals(JsonToken.FIELD_NAME, jp.nextToken());
assertEquals("num", jp.currentName());
assertEquals(JsonToken.VALUE_NULL, jp.nextToken());
assertEquals(JsonToken.FIELD_NAME, jp.nextToken());
assertEquals("obj", jp.currentName());
assertEquals(JsonToken.VALUE_NULL, jp.nextToken());
assertEquals(JsonToken.END_OBJECT, jp.nextToken());
jp.close();
}
use of com.fasterxml.jackson.core.json.JsonFactory in project jackson-core by FasterXML.
the class TestGeneratorObject method testConvenienceMethods.
/**
* Methods to test functionality added for [JACKSON-26]
*/
public void testConvenienceMethods() throws Exception {
StringWriter sw = new StringWriter();
JsonGenerator gen = new JsonFactory().createGenerator(ObjectWriteContext.empty(), sw);
gen.writeStartObject();
final BigDecimal dec = new BigDecimal("0.1");
final String TEXT = "\"some\nString!\"";
gen.writeNullField("null");
gen.writeBooleanField("bt", true);
gen.writeBooleanField("bf", false);
gen.writeNumberField("int", -1289);
gen.writeNumberField("dec", dec);
gen.writeObjectFieldStart("ob");
gen.writeStringField("str", TEXT);
gen.writeEndObject();
gen.writeArrayFieldStart("arr");
gen.writeEndArray();
gen.writeEndObject();
gen.close();
String docStr = sw.toString();
JsonParser jp = createParserUsingReader(docStr);
assertEquals(JsonToken.START_OBJECT, jp.nextToken());
assertEquals(JsonToken.FIELD_NAME, jp.nextToken());
assertEquals("null", jp.getText());
assertEquals(JsonToken.VALUE_NULL, jp.nextToken());
assertEquals(JsonToken.FIELD_NAME, jp.nextToken());
assertEquals("bt", jp.getText());
assertEquals(JsonToken.VALUE_TRUE, jp.nextToken());
assertEquals(JsonToken.FIELD_NAME, jp.nextToken());
assertEquals("bf", jp.getText());
assertEquals(JsonToken.VALUE_FALSE, jp.nextToken());
assertEquals(JsonToken.FIELD_NAME, jp.nextToken());
assertEquals("int", jp.getText());
assertEquals(JsonToken.VALUE_NUMBER_INT, jp.nextToken());
assertEquals(JsonToken.FIELD_NAME, jp.nextToken());
assertEquals("dec", jp.getText());
assertEquals(JsonToken.VALUE_NUMBER_FLOAT, jp.nextToken());
assertEquals(JsonToken.FIELD_NAME, jp.nextToken());
assertEquals("ob", jp.getText());
assertEquals(JsonToken.START_OBJECT, jp.nextToken());
assertEquals(JsonToken.FIELD_NAME, jp.nextToken());
assertEquals("str", jp.getText());
assertEquals(JsonToken.VALUE_STRING, jp.nextToken());
assertEquals(TEXT, getAndVerifyText(jp));
assertEquals(JsonToken.END_OBJECT, jp.nextToken());
assertEquals(JsonToken.FIELD_NAME, jp.nextToken());
assertEquals("arr", jp.getText());
assertEquals(JsonToken.START_ARRAY, jp.nextToken());
assertEquals(JsonToken.END_ARRAY, jp.nextToken());
assertEquals(JsonToken.END_OBJECT, jp.nextToken());
assertEquals(null, jp.nextToken());
jp.close();
}
use of com.fasterxml.jackson.core.json.JsonFactory in project jackson-core by FasterXML.
the class TestParserClosing method testNoAutoCloseInputStream.
@SuppressWarnings("resource")
public void testNoAutoCloseInputStream() throws Exception {
final String DOC = "[ 1 ]";
JsonFactory f = JsonFactory.builder().disable(JsonParser.Feature.AUTO_CLOSE_SOURCE).build();
MyStream input = new MyStream(DOC.getBytes("UTF-8"));
JsonParser jp = f.createParser(ObjectReadContext.empty(), input);
// shouldn't be closed to begin with...
assertFalse(input.isClosed());
assertToken(JsonToken.START_ARRAY, jp.nextToken());
assertToken(JsonToken.VALUE_NUMBER_INT, jp.nextToken());
assertToken(JsonToken.END_ARRAY, jp.nextToken());
assertNull(jp.nextToken());
// normally would be closed now
assertFalse(input.isClosed());
// regular close won't close it either:
jp.close();
assertFalse(input.isClosed());
}
use of com.fasterxml.jackson.core.json.JsonFactory in project jackson-core by FasterXML.
the class TestParserClosing method testAutoCloseReader.
@SuppressWarnings("resource")
public void testAutoCloseReader() throws Exception {
final String DOC = "[ 1 ]";
JsonFactory f = JsonFactory.builder().enable(JsonParser.Feature.AUTO_CLOSE_SOURCE).build();
assertTrue(f.isEnabled(JsonParser.Feature.AUTO_CLOSE_SOURCE));
MyReader input = new MyReader(DOC);
JsonParser jp = f.createParser(ObjectReadContext.empty(), input);
assertFalse(input.isClosed());
assertToken(JsonToken.START_ARRAY, jp.nextToken());
// but can close half-way through
jp.close();
assertTrue(input.isClosed());
// And then let's test implicit close at the end too:
input = new MyReader(DOC);
jp = f.createParser(ObjectReadContext.empty(), input);
assertFalse(input.isClosed());
assertToken(JsonToken.START_ARRAY, jp.nextToken());
assertToken(JsonToken.VALUE_NUMBER_INT, jp.nextToken());
assertToken(JsonToken.END_ARRAY, jp.nextToken());
assertNull(jp.nextToken());
assertTrue(input.isClosed());
}
Aggregations