Example usage for javax.json.stream JsonGenerator writeNull

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

Introduction

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

Prototype

JsonGenerator writeNull();

Source Link

Document

Writes a JSON null 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  .  j  a va  2  s  .  c  om
    //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();
        }
    }
}