Example usage for javax.json.stream JsonGenerator write

List of usage examples for javax.json.stream JsonGenerator write

Introduction

In this page you can find the example usage for javax.json.stream JsonGenerator write.

Prototype

JsonGenerator write(boolean value);

Source Link

Document

Writes a JSON true or false value within the current array, field or root context.

Usage

From source file:iing.uabc.edu.mx.persistencia.util.JSON.java

public static void stringifyArray(JsonGenerator generator, CollectionManager manager) {

    //Get size of the array to start stringifying elements
    //Note that the elements of this array has to be of the same class
    Collection collection = manager.getCollection();
    Class elementType = manager.getElementClass();

    System.out.println("Coleccion de tamanio " + collection.size());

    int i = 0;//from w  ww .  ja  v a 2  s. co m
    //Read every element and transform  it to a json array element string
    for (Iterator it = collection.iterator(); it.hasNext();) {
        Object element = it.next();

        System.out.println("Elemento: [" + i + "], Valor: " + element);

        if (element == null) {
            //Set to null the property
            generator.writeNull();
            continue;
        }

        //Is a String
        if (elementType == String.class) {
            generator.write(String.valueOf(element));
        } //Is a Date
        else if (elementType == Date.class) {
            String date = DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT.format(element);

            generator.write(date);

        } //Is a integer
        else if (elementType == Integer.class || elementType == Integer.TYPE) {
            generator.write((int) element);
        } //Is a double
        else if (elementType == Double.class || elementType == Double.TYPE) {
            generator.write((double) element);
        } //Is boolean
        else if (elementType == Boolean.class || elementType == Boolean.TYPE) {
            generator.write((boolean) element);

        } //Is a collection (Not implemented)
        else if (element instanceof Collection) {
            //                Class elementClass = manager.getCollectionElementType(keyName);
            //
            //                System.out.println("Nueva Colleccion [] de clase: "
            //                        + elementClass.getSimpleName());
            //
            //                generator.writeStartArray(keyName);
            //
            //                //Create new collection manager with the given class
            //                CollectionManager collectionManager
            //                        = new CollectionManager((Collection) value, elementClass);
            //
            //                generator.writeEnd();
        } else {
            //Is a object... probably
            BeanManager objectManager = new BeanManager(element);

            System.out.println("Nuevo Objecto {}: " + element.getClass().getSimpleName());
            generator.writeStartObject();

            stringifyObject(generator, objectManager);

            generator.writeEnd();
        }
    }
}

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. ja  va  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;
}