Example usage for javax.json.stream JsonParser getString

List of usage examples for javax.json.stream JsonParser getString

Introduction

In this page you can find the example usage for javax.json.stream JsonParser getString.

Prototype

String getString();

Source Link

Document

Returns a String for the name in a name/value pair, for a string value or a number value.

Usage

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

private static Object parseObject(JsonParser parser, BeanManager manager) {
    Event evt;//ww w . j  av a 2s .  c  o  m
    String keyName = "";

    do {
        evt = parser.next();

        switch (evt) {
        case START_OBJECT:
            Class objectClass = manager.getType(keyName);
            System.out.println("Nuevo Objeto: " + objectClass.getSimpleName());
            //Create new BeanManager for the new object with its class
            BeanManager objectManager = new BeanManager(objectClass);

            Object result = parseObject(parser, objectManager);

            manager.setProperty(keyName, result);
            break;

        case START_ARRAY:
            //Get element class of the collection
            Class elementClass = manager.getCollectionElementType(keyName);

            System.out.println("Nueva Colleccion de clase: " + elementClass.getSimpleName());

            //Get collection instance from the bean
            Collection collection = (Collection) manager.getProperty(keyName);

            //Create new collection manager with the given class
            CollectionManager collectionManager = new CollectionManager(collection, elementClass);

            collection = parseArray(parser, collectionManager);

            manager.setProperty(keyName, collection);
            break;

        case KEY_NAME:
            //Get attribute name
            keyName = parser.getString();
            System.out.println("KeyName: " + keyName);
            break;

        case VALUE_FALSE:
            manager.setProperty(keyName, false);
            System.out.println("Valor " + keyName + ": " + false);
            break;

        case VALUE_NULL:
            manager.setProperty(keyName, null);
            System.out.println("Valor " + keyName + ": null");
            break;

        case VALUE_NUMBER:
            //double or int
            if (parser.isIntegralNumber()) {
                int value = parser.getInt();
                manager.setProperty(keyName, value);
                System.out.println("Valor " + keyName + ": " + value);
            } else {
                double value = parser.getBigDecimal().doubleValue();
                manager.setProperty(keyName, value);
                System.out.println("Valor " + keyName + ": " + value);
            }
            break;

        case VALUE_STRING:
            String value = parser.getString();

            //Validate if the field is of type Date
            if (manager.getType(keyName) == Date.class) {
                Calendar cal = DatatypeConverter.parseDateTime(value);
                Date date = cal.getTime();
                manager.setProperty(keyName, date);

                System.out.println("Valor " + keyName + ": " + date);
            } else {
                manager.setProperty(keyName, value);
                System.out.println("Valor " + keyName + ": " + value);
            }
            break;
        case VALUE_TRUE:
            manager.setProperty(keyName, true);
            System.out.println("Valor " + keyName + ": " + true);
            break;
        }
    } while (evt != Event.END_OBJECT);

    return manager.getBean();
}

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

private static Collection parseArray(JsonParser parser, CollectionManager manager) {

    Event evt;/*from  ww w .j  a v a  2s . c o  m*/
    Class elementClass = manager.getElementClass();
    int pos = 0;

    do {
        evt = parser.next();

        switch (evt) {
        case START_OBJECT:
            //The most expected case of all in an array
            System.out.println(
                    "Nuevo Objeto: " + elementClass.getSimpleName() + " del arreglo en la pos: " + pos);

            BeanManager objectManager = new BeanManager(elementClass);

            Object result = parseObject(parser, objectManager);

            manager.addElement(result);
            pos++;

            break;

        //                case START_ARRAY:
        //                    //Only possible if array of arrays in json which is unlikely
        //                    //Also if in the pojo there is a recursive collection diferent
        //                    //from arraylist then there will be some problems.
        //                    
        ////                    System.out.println("Nueva Colleccion de clase: " + 
        ////                            elementClass.getSimpleName() + "dentro de otro "
        ////                            + "arreglo en la pos: " + pos);
        ////                    
        ////                    CollectionManager<T> collectionManager = new 
        ////                        CollectionManager<>(new ArrayList<>(), elementClass);
        ////                    
        ////                    Collection<T> collection = parseArray(parser, collectionManager);
        ////                    manager.addElement(collection);
        ////                    pos++;
        //                    break;
        //if it is an array of booleans.... but no.
        case VALUE_FALSE:
            System.out.println("Agregando false al arreglo en la pos: " + pos);

            manager.addElement(false);
            pos++;
            break;

        case VALUE_NULL:
            System.out.println("Agregando null al arreglo en la pos: " + pos);

            manager.addElement(null);
            pos++;
            break;

        case VALUE_NUMBER:
            //double or int
            if (parser.isIntegralNumber()) {
                int value = parser.getInt();
                System.out.println("Agregando " + value + " al arreglo en la pos: " + pos);

                manager.addElement(value);
                pos++;
            } else {
                double value = parser.getBigDecimal().doubleValue();
                System.out.println("Agregando " + value + " al arreglo en la pos: " + pos);

                manager.addElement(value);
                pos++;
            }
            break;

        case VALUE_STRING:
            String value = parser.getString();

            //Validate if the field is of type Date
            if (manager.getElementClass() == Date.class) {
                Calendar cal = DatatypeConverter.parseDateTime(value);
                Date date = cal.getTime();

                System.out.println("Agregando " + date + " al arreglo en la pos: " + pos);

                manager.addElement(date);
                pos++;
            } else {
                System.out.println("Agregando " + value + " al arreglo en la pos: " + pos);

                manager.addElement(value);
                pos++;
            }
            break;
        case VALUE_TRUE:
            System.out.println("Agregando true al arreglo en la pos: " + pos);

            manager.addElement(true);
            pos++;
            break;
        }
    } while (evt != Event.END_ARRAY);

    return manager.getCollection();
}