use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonGenerator in project jackson-core by FasterXML.
the class GeneratorFailFromReaderTest method _testFailOnWritingStringFromReaderWithTooFewCharacters.
private void _testFailOnWritingStringFromReaderWithTooFewCharacters(JsonFactory f, boolean useReader) throws Exception {
JsonGenerator gen;
ByteArrayOutputStream bout = new ByteArrayOutputStream();
if (useReader) {
gen = f.createGenerator(ObjectWriteContext.empty(), new OutputStreamWriter(bout, "UTF-8"));
} else {
gen = f.createGenerator(ObjectWriteContext.empty(), bout, JsonEncoding.UTF8);
}
gen.writeStartObject();
try {
String testStr = "aaaaaaaaa";
StringReader reader = new StringReader(testStr);
gen.writeFieldName("a");
gen.writeString(reader, testStr.length() + 1);
gen.flush();
String json = bout.toString("UTF-8");
fail("Should not have let " + gen.getClass().getName() + ".writeString() ': output = " + json);
} catch (JsonProcessingException e) {
verifyException(e, "Didn't read enough from reader");
}
gen.close();
}
use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonGenerator in project jackson-core by FasterXML.
the class CoreJDKSerializabilityTest method _copyJson.
@SuppressWarnings("resource")
protected String _copyJson(JsonFactory f, String json, boolean useBytes) throws IOException {
if (useBytes) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
JsonGenerator jg = f.createGenerator(ObjectWriteContext.empty(), bytes);
_copyJson(f, json, jg);
return bytes.toString("UTF-8");
}
StringWriter sw = new StringWriter();
JsonGenerator jg = f.createGenerator(ObjectWriteContext.empty(), sw);
_copyJson(f, json, jg);
return sw.toString();
}
use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonGenerator in project jackson-core by FasterXML.
the class Surrogate223Test method testSurrogatesByteBacked.
// for [core#223]
public void testSurrogatesByteBacked() throws Exception {
ByteArrayOutputStream out;
JsonGenerator g;
final String toQuote = new String(Character.toChars(0x1F602));
// just sanity check
assertEquals(2, toQuote.length());
// default should be disabled:
// assertFalse(JSON_F.isEnabled(JsonGenerator.Feature.ESCAPE_UTF8_SURROGATES));
out = new ByteArrayOutputStream();
g = JSON_F.createGenerator(ObjectWriteContext.empty(), out);
g.writeStartArray();
g.writeString(toQuote);
g.writeEndArray();
g.close();
// brackets, quotes, 4-byte encoding
assertEquals(2 + 2 + 4, out.size());
// Also parse back to ensure correctness
JsonParser p = JSON_F.createParser(ObjectReadContext.empty(), out.toByteArray());
assertToken(JsonToken.START_ARRAY, p.nextToken());
assertToken(JsonToken.VALUE_STRING, p.nextToken());
assertToken(JsonToken.END_ARRAY, p.nextToken());
p.close();
// but may revert back to original behavior
out = new ByteArrayOutputStream();
g = JSON_F.createGenerator(ObjectWriteContext.empty(), out);
// g.enable(JsonGenerator.Feature.ESCAPE_UTF8_SURROGATES);
g.writeStartArray();
g.writeString(toQuote);
g.writeEndArray();
g.close();
// brackets, quotes, 2 x 6 byte JSON escape
assertEquals(2 + 2 + 12, out.size());
}
use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonGenerator in project jackson-core by FasterXML.
the class Surrogate223Test method testSurrogatesCharBacked.
// for [core#223]
public void testSurrogatesCharBacked() throws Exception {
Writer out;
JsonGenerator g;
final String toQuote = new String(Character.toChars(0x1F602));
// just sanity check
assertEquals(2, toQuote.length());
// default should be disabled:
// assertFalse(JSON_F.isEnabled(JsonGenerator.Feature.ESCAPE_UTF8_SURROGATES));
out = new StringWriter();
g = JSON_F.createGenerator(ObjectWriteContext.empty(), out);
g.writeStartArray();
g.writeString(toQuote);
g.writeEndArray();
g.close();
// brackets, quotes, 2 chars as is
assertEquals(2 + 2 + 2, out.toString().length());
// Also parse back to ensure correctness
JsonParser p = JSON_F.createParser(ObjectReadContext.empty(), out.toString());
assertToken(JsonToken.START_ARRAY, p.nextToken());
assertToken(JsonToken.VALUE_STRING, p.nextToken());
assertToken(JsonToken.END_ARRAY, p.nextToken());
p.close();
// but may revert back to original behavior
out = new StringWriter();
g = JSON_F.createGenerator(ObjectWriteContext.empty(), out);
// g.enable(JsonGenerator.Feature.ESCAPE_UTF8_SURROGATES);
g.writeStartArray();
g.writeString(toQuote);
g.writeEndArray();
g.close();
// brackets, quotes, 2 x 6 byte JSON escape
assertEquals(2 + 2 + 12, out.toString().length());
}
use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonGenerator in project ma-core-public by infiniteautomation.
the class ExceptionCsvMessageConverter method writeInternal.
@Override
protected void writeInternal(Object object, Type type, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
MediaType contentType = outputMessage.getHeaders().getContentType();
JsonEncoding encoding = getJsonEncoding(contentType);
JsonGenerator generator = this.objectMapper.getFactory().createGenerator(outputMessage.getBody(), encoding);
try {
CsvSchema schema;
if (object instanceof AbstractRestV2Exception) {
schema = this.restExceptionSchema;
object = new CsvRestException((AbstractRestV2Exception) object);
} else {
schema = this.exceptionSchema;
}
writePrefix(generator, object);
ObjectWriter objectWriter = this.objectMapper.writer().with(schema);
objectWriter.writeValue(generator, object);
writeSuffix(generator, object);
generator.flush();
} catch (JsonProcessingException ex) {
throw new HttpMessageNotWritableException("Could not write content: " + ex.getMessage(), ex);
}
}
Aggregations