use of com.fasterxml.jackson.core.json.JsonFactory in project jackson-core by FasterXML.
the class TestParserClosing method testNoAutoCloseReader.
/**
* This unit test checks the default behaviour; with no auto-close, no
* automatic closing should occur, nor explicit one unless specific
* forcing method is used.
*/
public void testNoAutoCloseReader() throws Exception {
final String DOC = "[ 1 ]";
JsonFactory f = new JsonFactory();
// Check the default settings
assertTrue(f.isEnabled(JsonParser.Feature.AUTO_CLOSE_SOURCE));
// then change
f = f.rebuild().disable(JsonParser.Feature.AUTO_CLOSE_SOURCE).build();
assertFalse(f.isEnabled(JsonParser.Feature.AUTO_CLOSE_SOURCE));
@SuppressWarnings("resource") MyReader input = new MyReader(DOC);
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 testReleaseContentBytes.
// [JACKSON-287]
public void testReleaseContentBytes() throws Exception {
byte[] input = "[1]foobar".getBytes("UTF-8");
JsonParser jp = new JsonFactory().createParser(ObjectReadContext.empty(), input);
assertToken(JsonToken.START_ARRAY, jp.nextToken());
assertToken(JsonToken.VALUE_NUMBER_INT, jp.nextToken());
assertToken(JsonToken.END_ARRAY, jp.nextToken());
ByteArrayOutputStream out = new ByteArrayOutputStream();
// theoretically could have only read subset; but current impl is more greedy
assertEquals(6, jp.releaseBuffered(out));
assertArrayEquals("foobar".getBytes("UTF-8"), out.toByteArray());
jp.close();
}
use of com.fasterxml.jackson.core.json.JsonFactory in project jackson-core by FasterXML.
the class TestParserClosing method testReleaseContentChars.
public void testReleaseContentChars() throws Exception {
JsonParser jp = new JsonFactory().createParser(ObjectReadContext.empty(), "[true]xyz");
assertToken(JsonToken.START_ARRAY, jp.nextToken());
assertToken(JsonToken.VALUE_TRUE, jp.nextToken());
assertToken(JsonToken.END_ARRAY, jp.nextToken());
StringWriter sw = new StringWriter();
// theoretically could have only read subset; but current impl is more greedy
assertEquals(3, jp.releaseBuffered(sw));
assertEquals("xyz", sw.toString());
jp.close();
}
use of com.fasterxml.jackson.core.json.JsonFactory in project jackson-core by FasterXML.
the class TestPrettyPrinter method testSimpleDocWithDefault.
public void testSimpleDocWithDefault() throws Exception {
StringWriter sw = new StringWriter();
JsonGenerator gen = new JsonFactory().createGenerator(ObjectWriteContext.empty(), sw);
gen.useDefaultPrettyPrinter();
_verifyPrettyPrinter(gen, sw);
gen.close();
}
use of com.fasterxml.jackson.core.json.JsonFactory in project jackson-core by FasterXML.
the class TestPrettyPrinter method testCustomSeparatorsWithPPWithoutSpaces.
public void testCustomSeparatorsWithPPWithoutSpaces() throws Exception {
StringWriter sw = new StringWriter();
JsonGenerator gen = new JsonFactory().createGenerator(ObjectWriteContext.empty(), sw);
gen.setPrettyPrinter(new DefaultPrettyPrinter().withSeparators(Separators.createDefaultInstance().withObjectFieldValueSeparator('=').withObjectEntrySeparator(';').withArrayValueSeparator('|')).withoutSpacesInObjectEntries());
_writeTestDocument(gen);
gen.close();
assertEquals("[ 3| \"abc\"| [ true ]| {" + DefaultIndenter.SYS_LF + " \"f\"=null;" + DefaultIndenter.SYS_LF + " \"f2\"=null" + DefaultIndenter.SYS_LF + "} ]", sw.toString());
}
Aggregations