use of com.fasterxml.jackson.core.json.JsonFactory in project jackson-core by FasterXML.
the class TestLocation method testDisableSourceInclusion.
// for [jackson-core#356]
public void testDisableSourceInclusion() throws Exception {
JsonFactory f = JsonFactory.builder().disable(JsonParser.Feature.INCLUDE_SOURCE_IN_LOCATION).build();
JsonParser p = f.createParser(ObjectReadContext.empty(), "[ foobar ]");
assertToken(JsonToken.START_ARRAY, p.nextToken());
try {
p.nextToken();
fail("Shouldn't have passed");
} catch (JsonParseException e) {
verifyException(e, "unrecognized token");
JsonLocation loc = e.getLocation();
assertNull(loc.getSourceRef());
assertEquals("UNKNOWN", loc.sourceDescription());
}
p.close();
// and verify same works for byte-based too
p = f.createParser(ObjectReadContext.empty(), "[ foobar ]".getBytes("UTF-8"));
assertToken(JsonToken.START_ARRAY, p.nextToken());
try {
p.nextToken();
fail("Shouldn't have passed");
} catch (JsonParseException e) {
verifyException(e, "unrecognized token");
JsonLocation loc = e.getLocation();
assertNull(loc.getSourceRef());
assertEquals("UNKNOWN", loc.sourceDescription());
}
p.close();
}
use of com.fasterxml.jackson.core.json.JsonFactory in project jackson-core by FasterXML.
the class Base64BinaryParsingTest method _testInArray.
private void _testInArray(int mode) throws IOException {
JsonFactory f = new JsonFactory();
final int entryCount = 7;
StringWriter sw = new StringWriter();
JsonGenerator jg = f.createGenerator(ObjectWriteContext.empty(), sw);
jg.writeStartArray();
byte[][] entries = new byte[entryCount][];
for (int i = 0; i < entryCount; ++i) {
byte[] b = new byte[200 + i * 100];
for (int x = 0; x < b.length; ++x) {
b[x] = (byte) (i + x);
}
entries[i] = b;
jg.writeBinary(b);
}
jg.writeEndArray();
jg.close();
JsonParser p = createParser(f, mode, sw.toString());
assertToken(JsonToken.START_ARRAY, p.nextToken());
for (int i = 0; i < entryCount; ++i) {
assertToken(JsonToken.VALUE_STRING, p.nextToken());
byte[] b = p.getBinaryValue();
assertArrayEquals(entries[i], b);
}
assertToken(JsonToken.END_ARRAY, p.nextToken());
p.close();
}
use of com.fasterxml.jackson.core.json.JsonFactory in project jackson-core by FasterXML.
the class Base64BinaryParsingTest method _testBase64Text.
/*
/**********************************************************
/* Test helper methods
/**********************************************************
*/
@SuppressWarnings("resource")
public void _testBase64Text(int mode) throws Exception {
// let's actually iterate over sets of encoding modes, lengths
final int[] LENS = { 1, 2, 3, 4, 7, 9, 32, 33, 34, 35 };
final Base64Variant[] VARIANTS = { Base64Variants.MIME, Base64Variants.MIME_NO_LINEFEEDS, Base64Variants.MODIFIED_FOR_URL, Base64Variants.PEM };
JsonFactory jsonFactory = new JsonFactory();
final ByteArrayOutputStream bytes = new ByteArrayOutputStream();
StringWriter chars = null;
for (int len : LENS) {
byte[] input = new byte[len];
for (int i = 0; i < input.length; ++i) {
input[i] = (byte) i;
}
for (Base64Variant variant : VARIANTS) {
JsonGenerator g;
if (mode == MODE_READER) {
chars = new StringWriter();
g = jsonFactory.createGenerator(ObjectWriteContext.empty(), chars);
} else {
bytes.reset();
g = jsonFactory.createGenerator(ObjectWriteContext.empty(), bytes, JsonEncoding.UTF8);
}
g.writeBinary(variant, input, 0, input.length);
g.close();
JsonParser p;
if (mode == MODE_READER) {
p = jsonFactory.createParser(ObjectReadContext.empty(), chars.toString());
} else {
p = createParser(jsonFactory, mode, bytes.toByteArray());
}
assertToken(JsonToken.VALUE_STRING, p.nextToken());
byte[] data = null;
try {
data = p.getBinaryValue(variant);
} catch (Exception e) {
IOException ioException = new IOException("Failed (variant " + variant + ", data length " + len + "): " + e.getMessage());
ioException.initCause(e);
throw ioException;
}
assertNotNull(data);
assertArrayEquals(data, input);
if (mode != MODE_DATA_INPUT) {
// no look-ahead for DataInput
assertNull(p.nextToken());
}
p.close();
}
}
}
use of com.fasterxml.jackson.core.json.JsonFactory in project jackson-core by FasterXML.
the class CoreJDKSerializabilityTest method testParseException.
public void testParseException() throws Exception {
JsonFactory jf = new JsonFactory();
JsonParser p = jf.createParser(ObjectReadContext.empty(), " { garbage! }");
JsonParseException exc = null;
try {
p.nextToken();
p.nextToken();
fail("Should not get here");
} catch (JsonParseException e) {
exc = e;
}
p.close();
byte[] stuff = jdkSerialize(exc);
JsonParseException result = jdkDeserialize(stuff);
assertNotNull(result);
}
use of com.fasterxml.jackson.core.json.JsonFactory in project jackson-core by FasterXML.
the class CoreJDKSerializabilityTest method testJsonFactorySerializable.
public void testJsonFactorySerializable() throws Exception {
JsonFactory f = new JsonFactory();
String origJson = "{\"simple\":[1,true,{}]}";
assertEquals(origJson, _copyJson(f, origJson, false));
// Ok: freeze dry factory, thaw, and try to use again:
byte[] frozen = jdkSerialize(f);
JsonFactory f2 = jdkDeserialize(frozen);
assertNotNull(f2);
assertEquals(origJson, _copyJson(f2, origJson, false));
// Let's also try byte-based variant, for fun...
assertEquals(origJson, _copyJson(f2, origJson, true));
}
Aggregations