Example usage for javax.json Json createGeneratorFactory

List of usage examples for javax.json Json createGeneratorFactory

Introduction

In this page you can find the example usage for javax.json Json createGeneratorFactory.

Prototype

public static JsonGeneratorFactory createGeneratorFactory(Map<String, ?> config) 

Source Link

Document

Creates a generator factory for creating JsonGenerator objects.

Usage

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/*  w w  w.  j a v  a2  s .  c om*/
        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;
}

From source file:org.dcm4che3.tool.dcm2json.Dcm2Json.java

private JsonGenerator createGenerator(OutputStream out) {
    Map<String, ?> conf = new HashMap<String, Object>(2);
    if (indent)/*from   w  ww .j  a  v a2  s .com*/
        conf.put(JsonGenerator.PRETTY_PRINTING, null);
    return Json.createGeneratorFactory(conf).createGenerator(out);
}