Search in sources :

Example 71 with JsonGenerator

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonGenerator in project teiid by teiid.

the class JsonSerializer method serialize.

@Override
public InputStream serialize(Document doc) throws TranslatorException {
    ByteArrayOutputStream outputStream = null;
    try {
        outputStream = new ByteArrayOutputStream(1024 * 10);
        JsonGenerator json = new JsonFactory().createGenerator(outputStream);
        writeDocument(doc, null, json, false);
        json.close();
        return new ByteArrayInputStream(outputStream.toByteArray());
    } catch (IOException | SQLException e) {
        throw new TranslatorException(e);
    } finally {
        if (outputStream != null) {
            try {
                outputStream.close();
            } catch (IOException e) {
            // ignore.
            }
        }
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) SQLException(java.sql.SQLException) JsonFactory(com.fasterxml.jackson.core.JsonFactory) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) TranslatorException(org.teiid.translator.TranslatorException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Example 72 with JsonGenerator

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonGenerator in project nuxeo-drive-server by nuxeo.

the class NuxeoDriveScrollDescendants method writeJSONBlob.

protected Blob writeJSONBlob(ScrollFileSystemItemList scrollFSIList) throws IOException {
    StringWriter writer = new StringWriter();
    JsonFactory factory = new JsonFactory();
    try (JsonGenerator jg = factory.createGenerator(writer)) {
        jg.setCodec(new ObjectMapper());
        jg.writeStartObject();
        jg.writeStringField("scrollId", scrollFSIList.getScrollId());
        jg.writeObjectField("fileSystemItems", scrollFSIList);
        jg.writeEndObject();
    }
    return Blobs.createJSONBlob(writer.toString());
}
Also used : StringWriter(java.io.StringWriter) JsonFactory(com.fasterxml.jackson.core.JsonFactory) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 73 with JsonGenerator

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonGenerator in project candlepin by candlepin.

the class CandlepinQueryInterceptor method postProcess.

@Override
public void postProcess(ServerResponse response) {
    Object entity = response.getEntity();
    if (entity instanceof CandlepinQuery) {
        final PageRequest pageRequest = ResteasyProviderFactory.getContextData(PageRequest.class);
        final Session session = this.openSession();
        final CandlepinQuery query = (CandlepinQuery) entity;
        final ObjectMapper mapper = this.jsonProvider.locateMapper(Object.class, MediaType.APPLICATION_JSON_TYPE);
        // Use a separate session so we aren't at risk of lazy loading or interceptors closing
        // our cursor mid-stream.
        query.useSession(session);
        // Apply any paging config we may have
        if (pageRequest != null) {
            // Impl note:
            // Sorting will always be required (for consistency) if a page request object is
            // present -- either isPaging() will be true, or we'll have ordering config.
            String sortField = pageRequest.getSortBy() != null ? pageRequest.getSortBy() : AbstractHibernateObject.DEFAULT_SORT_FIELD;
            PageRequest.Order order = pageRequest.getOrder() != null ? pageRequest.getOrder() : PageRequest.DEFAULT_ORDER;
            query.addOrder(order == PageRequest.Order.DESCENDING ? Order.desc(sortField) : Order.asc(sortField));
            if (pageRequest.isPaging()) {
                query.setFirstResult((pageRequest.getPage() - 1) * pageRequest.getPerPage());
                query.setMaxResults(pageRequest.getPerPage());
                // Create a page object for the link header response
                Page page = new Page();
                // This is expensive :(
                page.setMaxRecords(query.getRowCount());
                page.setPageRequest(pageRequest);
                // Note: we don't need to store the page data in the page
                ResteasyProviderFactory.pushContext(Page.class, page);
            }
        }
        // Set the output streamer that will stream our query result
        response.setEntity(new StreamingOutput() {

            @Override
            public void write(OutputStream stream) throws IOException, WebApplicationException {
                JsonGenerator generator = null;
                ResultIterator<Object> iterator = null;
                try {
                    generator = mapper.getJsonFactory().createGenerator(stream);
                    iterator = query.iterate();
                    generator.writeStartArray();
                    while (iterator.hasNext()) {
                        mapper.writeValue(generator, iterator.next());
                    }
                    generator.writeEndArray();
                } finally {
                    if (generator != null) {
                        generator.flush();
                        generator.close();
                    }
                    if (iterator != null) {
                        iterator.close();
                    }
                    if (session != null) {
                        session.close();
                    }
                }
            }
        });
    }
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) OutputStream(java.io.OutputStream) ResultIterator(org.candlepin.model.ResultIterator) CandlepinQuery(org.candlepin.model.CandlepinQuery) Page(org.candlepin.common.paging.Page) StreamingOutput(javax.ws.rs.core.StreamingOutput) IOException(java.io.IOException) PageRequest(org.candlepin.common.paging.PageRequest) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) AbstractHibernateObject(org.candlepin.model.AbstractHibernateObject) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Session(org.hibernate.Session)

Example 74 with JsonGenerator

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonGenerator in project jackson-core by FasterXML.

the class GeneratorFailFromReaderTest method _testFailOnWritingStringNotFieldName.

/*
    /**********************************************************
    /* Internal methods
    /**********************************************************
     */
private void _testFailOnWritingStringNotFieldName(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 {
        StringReader reader = new StringReader("a");
        gen.writeString(reader, -1);
        gen.flush();
        String json = bout.toString("UTF-8");
        fail("Should not have let " + gen.getClass().getName() + ".writeString() be used in place of 'writeFieldName()': output = " + json);
    } catch (JsonProcessingException e) {
        verifyException(e, "can not write a String");
    }
    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 75 with JsonGenerator

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonGenerator in project jackson-core by FasterXML.

the class GeneratorFailFromReaderTest method _testFailOnWritingStringFromNullReader.

private void _testFailOnWritingStringFromNullReader(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 {
        gen.writeFieldName("a");
        gen.writeString(null, -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, "null reader");
    }
    gen.close();
}
Also used : JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) OutputStreamWriter(java.io.OutputStreamWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) 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