use of com.fasterxml.jackson.core.json.JsonFactory in project jackson-core by FasterXML.
the class Base64GenerationTest method testIssue55.
// For [core#55]
public void testIssue55() throws Exception {
final JsonFactory f = new JsonFactory();
// First, byte-backed:
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
JsonGenerator gen = f.createGenerator(ObjectWriteContext.empty(), bytes);
ByteArrayInputStream data = new ByteArrayInputStream(new byte[2000]);
gen.writeBinary(data, 1999);
gen.close();
final int EXP_LEN = 2670;
assertEquals(EXP_LEN, bytes.size());
// Then char-backed
StringWriter sw = new StringWriter();
gen = f.createGenerator(ObjectWriteContext.empty(), sw);
data = new ByteArrayInputStream(new byte[2000]);
gen.writeBinary(data, 1999);
gen.close();
assertEquals(EXP_LEN, sw.toString().length());
}
use of com.fasterxml.jackson.core.json.JsonFactory in project jackson-core by FasterXML.
the class Base64GenerationTest method _testSimpleBinaryWrite.
/*
/**********************************************************
/* Helper methods
/**********************************************************
*/
private void _testSimpleBinaryWrite(boolean useCharBased) throws Exception {
/* Let's only test the standard base64 variant; but write
* values in root, array and object contexts.
*/
Base64Variant b64v = Base64Variants.getDefaultVariant();
JsonFactory jf = new JsonFactory();
for (int i = 0; i < 3; ++i) {
JsonGenerator gen;
ByteArrayOutputStream bout = new ByteArrayOutputStream(200);
if (useCharBased) {
gen = jf.createGenerator(ObjectWriteContext.empty(), new OutputStreamWriter(bout, "UTF-8"));
} else {
gen = jf.createGenerator(ObjectWriteContext.empty(), bout, JsonEncoding.UTF8);
}
switch(i) {
case // root
0:
gen.writeBinary(b64v, WIKIPEDIA_BASE64_AS_BYTES, 0, WIKIPEDIA_BASE64_AS_BYTES.length);
break;
case // array
1:
gen.writeStartArray();
gen.writeBinary(b64v, WIKIPEDIA_BASE64_AS_BYTES, 0, WIKIPEDIA_BASE64_AS_BYTES.length);
gen.writeEndArray();
break;
default:
// object
gen.writeStartObject();
gen.writeFieldName("field");
gen.writeBinary(b64v, WIKIPEDIA_BASE64_AS_BYTES, 0, WIKIPEDIA_BASE64_AS_BYTES.length);
gen.writeEndObject();
break;
}
gen.close();
JsonParser jp = jf.createParser(ObjectReadContext.empty(), new ByteArrayInputStream(bout.toByteArray()));
// Need to skip other events before binary data:
switch(i) {
case 0:
break;
case 1:
assertEquals(JsonToken.START_ARRAY, jp.nextToken());
break;
default:
assertEquals(JsonToken.START_OBJECT, jp.nextToken());
assertEquals(JsonToken.FIELD_NAME, jp.nextToken());
break;
}
assertEquals(JsonToken.VALUE_STRING, jp.nextToken());
String actualValue = jp.getText();
jp.close();
assertEquals(WIKIPEDIA_BASE64_ENCODED, actualValue);
}
}
use of com.fasterxml.jackson.core.json.JsonFactory in project jackson-core by FasterXML.
the class CoreJDKSerializabilityTest method testLocation.
public void testLocation() throws Exception {
JsonFactory jf = new JsonFactory();
JsonParser jp = jf.createParser(ObjectReadContext.empty(), " { }");
assertToken(JsonToken.START_OBJECT, jp.nextToken());
JsonLocation loc = jp.getCurrentLocation();
byte[] stuff = jdkSerialize(loc);
JsonLocation loc2 = jdkDeserialize(stuff);
assertNotNull(loc2);
assertEquals(loc.getLineNr(), loc2.getLineNr());
assertEquals(loc.getColumnNr(), loc2.getColumnNr());
jp.close();
}
use of com.fasterxml.jackson.core.json.JsonFactory in project jackson-core by FasterXML.
the class CoreJDKSerializabilityTest method testGenerationException.
public void testGenerationException() throws Exception {
JsonFactory jf = new JsonFactory();
JsonGenerator g = jf.createGenerator(ObjectWriteContext.empty(), new ByteArrayOutputStream());
JsonGenerationException exc = null;
g.writeStartObject();
try {
g.writeNumber(4);
fail("Should not get here");
} catch (JsonGenerationException e) {
exc = e;
}
g.close();
byte[] stuff = jdkSerialize(exc);
JsonGenerationException result = jdkDeserialize(stuff);
assertNotNull(result);
}
use of com.fasterxml.jackson.core.json.JsonFactory in project jackson-core by FasterXML.
the class AsyncSimpleObjectTest method testBooleans.
public void testBooleans() throws IOException {
final JsonFactory f = JSON_F;
byte[] data = _jsonDoc(aposToQuotes("{ 'a':true, 'b':false, 'acdc':true, '" + UNICODE_SHORT_NAME + "':true, 'a1234567':false," + "'" + UNICODE_LONG_NAME + "': true }"));
// first, no offsets
_testBooleans(f, data, 0, 100);
_testBooleans(f, data, 0, 3);
_testBooleans(f, data, 0, 1);
// then with some
_testBooleans(f, data, 1, 100);
_testBooleans(f, data, 1, 3);
_testBooleans(f, data, 1, 1);
}
Aggregations