Search in sources :

Example 76 with JsonGenerator

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();
}
Also used : StringReader(java.io.StringReader) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) OutputStreamWriter(java.io.OutputStreamWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 77 with JsonGenerator

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();
}
Also used : JsonGenerator(com.fasterxml.jackson.core.JsonGenerator)

Example 78 with JsonGenerator

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());
}
Also used : JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) ByteArrayOutputStream(java.io.ByteArrayOutputStream) JsonParser(com.fasterxml.jackson.core.JsonParser)

Example 79 with JsonGenerator

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());
}
Also used : StringWriter(java.io.StringWriter) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) StringWriter(java.io.StringWriter) Writer(java.io.Writer) JsonParser(com.fasterxml.jackson.core.JsonParser)

Example 80 with JsonGenerator

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);
    }
}
Also used : CsvSchema(com.fasterxml.jackson.dataformat.csv.CsvSchema) HttpMessageNotWritableException(org.springframework.http.converter.HttpMessageNotWritableException) JsonEncoding(com.fasterxml.jackson.core.JsonEncoding) AbstractRestV2Exception(com.infiniteautomation.mango.rest.v2.exception.AbstractRestV2Exception) MediaType(org.springframework.http.MediaType) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Aggregations

JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)704 KriptonJsonContext (com.abubusoft.kripton.KriptonJsonContext)257 KriptonByteArrayOutputStream (com.abubusoft.kripton.common.KriptonByteArrayOutputStream)257 KriptonRuntimeException (com.abubusoft.kripton.exception.KriptonRuntimeException)257 JacksonWrapperSerializer (com.abubusoft.kripton.persistence.JacksonWrapperSerializer)257 IOException (java.io.IOException)169 StringWriter (java.io.StringWriter)144 JsonFactory (com.fasterxml.jackson.core.JsonFactory)101 ByteArrayOutputStream (java.io.ByteArrayOutputStream)66 Map (java.util.Map)57 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)54 HashMap (java.util.HashMap)40 Test (org.junit.Test)30 File (java.io.File)27 ArrayList (java.util.ArrayList)25 OutputStream (java.io.OutputStream)24 Writer (java.io.Writer)22 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)21 SerializerProvider (com.fasterxml.jackson.databind.SerializerProvider)20 FileOutputStream (java.io.FileOutputStream)19