Example usage for javax.json.stream JsonParser next

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

Introduction

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

Prototype

Event next();

Source Link

Document

Returns the event for the next parsing state.

Usage

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

private static Object parseObject(JsonParser parser, BeanManager manager) {
    Event evt;/*from   w w  w  . j  ava 2  s .  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;//ww w . j av  a  2s .  com
    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();
}

From source file:ASSINGMENT4.ServletProducts.java

@POST
@Consumes("application/json")
public void doPost(String str) {
    JsonParser parser = Json.createParser(new StringReader(str));
    Map<String, String> mapKeyValue = new HashMap<>();
    String key = "", val;
    while (parser.hasNext()) {
        JsonParser.Event evt = parser.next();
        switch (evt) {
        case KEY_NAME:
            key = parser.getString();/*  ww w  . ja  v a 2 s.  c o m*/
            break;
        case VALUE_STRING:
            val = parser.getString();
            mapKeyValue.put(key, val);
            break;
        case VALUE_NUMBER:
            val = Integer.toString(parser.getInt());
            mapKeyValue.put(key, val);
            break;
        }
    }
    System.out.println(mapKeyValue);
    doPostOrPutOrDelete("INSERT INTO product (product_name,description, quantity) VALUES ( ?, ?, ?)",
            mapKeyValue.get("product_name"), mapKeyValue.get("description"), mapKeyValue.get("quantity"));
}

From source file:ASSINGMENT4.ServletProducts.java

@PUT
@Path("{product_id}")
@Consumes("application/json")
public void doPut(@PathParam("product_id") String id, String str) {
    JsonParser parser = Json.createParser(new StringReader(str));
    Map<String, String> mapKayValue = new HashMap<>();
    String key = "", val;
    while (parser.hasNext()) {
        JsonParser.Event evt = parser.next();
        switch (evt) {
        case KEY_NAME:
            key = parser.getString();//from   w  w w.  j a v a2  s.  c  om
            break;
        case VALUE_STRING:
            val = parser.getString();
            mapKayValue.put(key, val);
            break;
        case VALUE_NUMBER:
            val = parser.getString();
            mapKayValue.put(key, val);
            break;
        }
    }
    System.out.println(mapKayValue);
    doPostOrPutOrDelete("UPDATE PRODUCT SET name = ?, description = ?, quantity = ? WHERE product_id = ?",
            mapKayValue.get("product_name"), mapKayValue.get("description"), mapKayValue.get("quantity"), id);

}

From source file:com.assignment.Product.java

@POST
@Consumes("application/json")
public void doPost(String str) {
    JsonParser parser = Json.createParser(new StringReader(str));
    Map<String, String> mapKeyValue = new HashMap<>();
    String key = "", val;
    while (parser.hasNext()) {
        JsonParser.Event evt = parser.next();
        switch (evt) {
        case KEY_NAME:
            key = parser.getString();//from   w  ww. j  a  va  2 s  . c  o  m
            break;
        case VALUE_STRING:
            val = parser.getString();
            mapKeyValue.put(key, val);
            break;
        case VALUE_NUMBER:
            val = Integer.toString(parser.getInt());
            mapKeyValue.put(key, val);
            break;
        }
    }
    System.out.println(mapKeyValue);
    doPostOrPutOrDelete("INSERT INTO products (name, description, quantity) VALUES ( ?, ?, ?)",
            mapKeyValue.get("name"), mapKeyValue.get("description"), mapKeyValue.get("quantity"));
}

From source file:com.assignment.Product.java

@PUT
@Path("{id}")
@Consumes("application/json")
public void doPut(@PathParam("id") String id, String str) {
    JsonParser parser = Json.createParser(new StringReader(str));
    Map<String, String> mapKayValue = new HashMap<>();
    String key = "", val;
    while (parser.hasNext()) {
        JsonParser.Event evt = parser.next();
        switch (evt) {
        case KEY_NAME:
            key = parser.getString();/*w w w  . j a v  a  2 s.  com*/
            break;
        case VALUE_STRING:
            val = parser.getString();
            mapKayValue.put(key, val);
            break;
        case VALUE_NUMBER:
            val = parser.getString();
            mapKayValue.put(key, val);
            break;
        }
    }
    System.out.println(mapKayValue);
    doPostOrPutOrDelete("UPDATE PRODUCTS SET name = ?, description = ?, quantity = ? WHERE productID = ?",
            mapKayValue.get("name"), mapKayValue.get("description"), mapKayValue.get("quantity"), id);

}

From source file:Servlet.product.java

@POST
@Consumes("application/json")
public void doPost(String str) {
    JsonParser parser = Json.createParser(new StringReader(str));
    Map<String, String> map = new HashMap<>();
    String name = "", value;

    while (parser.hasNext()) {
        JsonParser.Event event = parser.next();
        switch (event) {
        case KEY_NAME:
            name = parser.getString();/*w w  w .ja va  2 s . c  o  m*/
            break;
        case VALUE_STRING:
            value = parser.getString();
            map.put(name, value);
            break;
        case VALUE_NUMBER:
            value = Integer.toString(parser.getInt());
            map.put(name, value);
            break;
        }
    }
    System.out.println(map);

    String na = map.get("name");
    String des = map.get("description");
    String qunt = map.get("quantity");

    doUpdate("INSERT INTO product (name,description,quantity)VALUES (?,?,?)", na, des, qunt);
}

From source file:Servlet.product.java

@PUT
@Path("{id}")
@Consumes("application/json")
public void doPut(@PathParam("id") String id, String str) {
    JsonParser parser = Json.createParser(new StringReader(str));
    Map<String, String> map = new HashMap<>();
    String name = "", value;

    while (parser.hasNext()) {
        JsonParser.Event event = parser.next();
        switch (event) {
        case KEY_NAME:
            name = parser.getString();//  w  ww.jav a 2  s  . c o m
            break;
        case VALUE_STRING:
            value = parser.getString();
            map.put(name, value);
            break;
        case VALUE_NUMBER:
            value = Integer.toString(parser.getInt());
            map.put(name, value);
            break;
        }
    }
    System.out.println(map);

    String na = map.get("name");
    String des = map.get("description");
    String qunt = map.get("quantity");

    doUpdate("update product set productID = ?, name = ?, description = ?, quantity = ? where productID = ?",
            id, na, des, qunt, id);
}

From source file:servlets.SampleServlet.java

@POST
@Consumes("application/json")
public void doPost(String st) {
    JsonParser parser = Json.createParser(new StringReader(st));
    Map<String, String> ad = new HashMap<>();
    String Name = "", value;

    while (parser.hasNext()) {
        JsonParser.Event e = parser.next();
        switch (e) {
        case KEY_NAME:
            Name = parser.getString();
            break;
        case VALUE_STRING:

            value = parser.getString();//from ww  w.  j  av  a 2 s.c o m
            ad.put(Name, value);
            break;
        case VALUE_NUMBER:
            value = Integer.toString(parser.getInt());
            ad.put(Name, value);
            break;
        }
    }
    System.out.println(ad);
    String Name1 = ad.get("Name");
    String Description = ad.get("Description");
    String Quantity = ad.get("Quantity");
    doUpdate("INSERT INTO PRODUCT ( Name, Description, Quantity) values ( ?, ?, ?)", Name1, Description,
            Quantity);
}

From source file:servlets.SampleServlet.java

@PUT
@Path("{productID}")
@Consumes("application/json")
public void doPut(@PathParam("productID") String productID, String st) {
    JsonParser parser = Json.createParser(new StringReader(st));
    Map<String, String> ad = new HashMap<>();
    String Name = "", value;
    while (parser.hasNext()) {
        JsonParser.Event e = parser.next();
        switch (e) {
        case KEY_NAME:
            Name = parser.getString();
            break;
        case VALUE_STRING:
            value = parser.getString();/*  w  w w.j  av a2 s. c om*/
            ad.put(Name, value);
            break;
        case VALUE_NUMBER:
            value = Integer.toString(parser.getInt());
            ad.put(Name, value);
            break;
        }
    }
    System.out.println(ad);

    String Name1 = ad.get("Name");
    String Description = ad.get("Description");
    String Quantity = ad.get("Quantity");
    doUpdate("UPDATE PRODUCT SET productId = ?, Name = ?, Description = ?, Quantity = ? WHERE productID = ?",
            productID, Name1, Description, Quantity, productID);

}