use of com.fasterxml.jackson.core.json.JsonFactory in project jackson-core by FasterXML.
the class SymbolsViaParserTest method _test17Chars.
/*
/**********************************************************
/* Secondary test methods
/**********************************************************
*/
private void _test17Chars(boolean useBytes) throws IOException {
String doc = _createDoc17();
JsonFactory f = new JsonFactory();
JsonParser p = useBytes ? f.createParser(ObjectReadContext.empty(), doc.getBytes("UTF-8")) : f.createParser(ObjectReadContext.empty(), doc);
HashSet<String> syms = new HashSet<String>();
assertToken(JsonToken.START_OBJECT, p.nextToken());
for (int i = 0; i < 50; ++i) {
assertToken(JsonToken.FIELD_NAME, p.nextToken());
syms.add(p.currentName());
assertToken(JsonToken.VALUE_TRUE, p.nextToken());
}
assertToken(JsonToken.END_OBJECT, p.nextToken());
assertEquals(50, syms.size());
p.close();
}
use of com.fasterxml.jackson.core.json.JsonFactory in project jackson-core by FasterXML.
the class SymbolsViaParserTest method _testSymbolTableExpansion.
public void _testSymbolTableExpansion(boolean useBytes) throws Exception {
JsonFactory jsonFactory = new JsonFactory();
// Important: must create separate documents to gradually build up symbol table
for (int i = 0; i < 200; i++) {
String field = Integer.toString(i);
final String doc = "{ \"" + field + "\" : \"test\" }";
JsonParser parser = useBytes ? jsonFactory.createParser(ObjectReadContext.empty(), doc.getBytes("UTF-8")) : jsonFactory.createParser(ObjectReadContext.empty(), doc);
assertToken(JsonToken.START_OBJECT, parser.nextToken());
assertToken(JsonToken.FIELD_NAME, parser.nextToken());
assertEquals(field, parser.currentName());
assertToken(JsonToken.VALUE_STRING, parser.nextToken());
assertToken(JsonToken.END_OBJECT, parser.nextToken());
assertNull(parser.nextToken());
parser.close();
}
}
use of com.fasterxml.jackson.core.json.JsonFactory in project jackson-core by FasterXML.
the class TestHashCollisionChars method testReaderCollisions.
// for 31
/*
final static String[] MULT_COLLISION_FRAGMENTS = new String[] {
// Ones generated for 31/65536...
"@~~", "A_~", "A`_", "Aa@", "Ab!", "B@~", // "BA_", "BB@", "BC!", "C!~"
};
*/
public void testReaderCollisions() throws Exception {
StringBuilder sb = new StringBuilder();
List<String> coll = collisions();
for (String field : coll) {
if (sb.length() == 0) {
sb.append("{");
} else {
sb.append(",\n");
}
sb.append('"');
sb.append(field);
sb.append("\":3");
}
sb.append("}");
// First: attempt with exceptions turned on; should catch an exception
JsonFactory f = JsonFactory.builder().enable(JsonFactory.Feature.FAIL_ON_SYMBOL_HASH_OVERFLOW).build();
JsonParser p = f.createParser(ObjectReadContext.empty(), sb.toString());
try {
while (p.nextToken() != null) {
;
}
fail("Should have failed");
} catch (IllegalStateException e) {
verifyException(e, "hash collision");
}
p.close();
// but then without feature, should pass
f = JsonFactory.builder().disable(JsonFactory.Feature.FAIL_ON_SYMBOL_HASH_OVERFLOW).build();
p = f.createParser(ObjectReadContext.empty(), sb.toString());
while (p.nextToken() != null) {
;
}
p.close();
}
use of com.fasterxml.jackson.core.json.JsonFactory in project jackson-core by FasterXML.
the class TestGeneratorClosing method testNoAutoCloseArraysAndObjects.
public void testNoAutoCloseArraysAndObjects() throws Exception {
JsonFactory f = JsonFactory.builder().disable(JsonGenerator.Feature.AUTO_CLOSE_CONTENT).build();
StringWriter sw = new StringWriter();
JsonGenerator g = f.createGenerator(ObjectWriteContext.empty(), sw);
g.writeStartArray();
g.close();
// shouldn't close
assertEquals("[", sw.toString());
// Then objects
sw = new StringWriter();
g = f.createGenerator(ObjectWriteContext.empty(), sw);
g.writeStartObject();
g.close();
assertEquals("{", sw.toString());
}
use of com.fasterxml.jackson.core.json.JsonFactory in project jackson-core by FasterXML.
the class TestGeneratorClosing method testAutoFlushOrNot.
// [JACKSON-401]
@SuppressWarnings("resource")
public void testAutoFlushOrNot() throws Exception {
JsonFactory f = new JsonFactory();
assertTrue(f.isEnabled(JsonGenerator.Feature.FLUSH_PASSED_TO_STREAM));
MyChars sw = new MyChars();
JsonGenerator g = f.createGenerator(ObjectWriteContext.empty(), sw);
g.writeStartArray();
g.writeEndArray();
assertEquals(0, sw.flushed);
g.flush();
assertEquals(1, sw.flushed);
g.close();
// ditto with stream
MyBytes bytes = new MyBytes();
g = f.createGenerator(ObjectWriteContext.empty(), bytes, JsonEncoding.UTF8);
g.writeStartArray();
g.writeEndArray();
assertEquals(0, bytes.flushed);
g.flush();
assertEquals(1, bytes.flushed);
assertEquals(2, bytes.toByteArray().length);
g.close();
// then disable and we should not see flushing again...
f = f.rebuild().disable(JsonGenerator.Feature.FLUSH_PASSED_TO_STREAM).build();
// first with a Writer
sw = new MyChars();
g = f.createGenerator(ObjectWriteContext.empty(), sw);
g.writeStartArray();
g.writeEndArray();
assertEquals(0, sw.flushed);
g.flush();
assertEquals(0, sw.flushed);
g.close();
assertEquals("[]", sw.toString());
// and then with OutputStream
bytes = new MyBytes();
g = f.createGenerator(ObjectWriteContext.empty(), bytes, JsonEncoding.UTF8);
g.writeStartArray();
g.writeEndArray();
assertEquals(0, bytes.flushed);
g.flush();
assertEquals(0, bytes.flushed);
g.close();
assertEquals(2, bytes.toByteArray().length);
}
Aggregations