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.
}
}
}
}
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());
}
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();
}
}
}
});
}
}
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();
}
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();
}
Aggregations