List of usage examples for javax.json.stream JsonGeneratorFactory createGenerator
JsonGenerator createGenerator(OutputStream out);
From source file:edu.harvard.hms.dbmi.bd2k.irct.ws.rs.resultconverter.JSONTabularDataConverter.java
@Override public StreamingOutput createStream(final Result result) { StreamingOutput stream = new StreamingOutput() { @Override// www . j av a 2s . co m public void write(OutputStream outputStream) throws IOException, WebApplicationException { JsonGenerator jg = null; ResultSet rs = null; try { rs = (ResultSet) result.getData(); rs.load(result.getResultSetLocation()); Map<String, Object> properties = new HashMap<String, Object>(1); JsonGeneratorFactory jgf = Json.createGeneratorFactory(properties); jg = jgf.createGenerator(outputStream); jg.writeStartObject(); //Start Object jg.writeStartArray("columns"); // Get columns for (Column column : rs.getColumns()) { jg.write(column.toJson()); } jg.writeEnd(); //End columns jg.writeStartArray("data"); rs.beforeFirst(); while (rs.next()) { jg.writeStartArray(); //Begin Row Array for (int columnIndex = 0; columnIndex < rs.getColumnSize(); columnIndex++) { String value = rs.getString(columnIndex); if (value != null) { jg.writeStartObject(); jg.write(rs.getColumn(columnIndex).getName(), rs.getString(columnIndex)); jg.writeEnd(); } } jg.writeEnd(); //End Row Array } jg.writeEnd(); //End data jg.writeEnd(); //End Full Object } catch (ResultSetException | PersistableException e) { log.info("Error creating JSON Stream: " + e.getMessage()); } finally { if (jg != null) { jg.close(); } if (rs != null && !rs.isClosed()) { try { rs.close(); } catch (ResultSetException e) { e.printStackTrace(); } } if (outputStream != null) { outputStream.close(); } } } }; return stream; }