List of usage examples for javax.json.stream JsonGenerator writeStartObject
JsonGenerator writeStartObject(String name);
From source file:iing.uabc.edu.mx.persistencia.util.JSON.java
private static void stringifyObject(JsonGenerator generator, BeanManager manager) { String keyName;//from www . j a v a 2 s . com Field[] fields = manager.getFields(); //Read every field and transform it to a json property string for (Field field : fields) { Class fieldType = manager.getType(field.getName()); keyName = field.getName(); Object value = manager.getProperty(keyName); System.out.println("KeyName: " + keyName); System.out.println("Valor " + keyName + ": " + value); if (value == null) { //Set to null the property generator.writeNull(keyName); continue; } //Is a String if (fieldType == String.class) { generator.write(keyName, String.valueOf(value)); } //Is a Date else if (fieldType == Date.class) { String date = DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT.format(value); generator.write(keyName, date); } //Is a integer else if (fieldType == Integer.class || fieldType == Integer.TYPE) { generator.write(keyName, (int) value); } //Is a double else if (fieldType == Double.class || fieldType == Double.TYPE) { generator.write(keyName, (double) value); } //Is boolean else if (fieldType == Boolean.class || fieldType == Boolean.TYPE) { generator.write(keyName, (boolean) value); } //Is a collection else if (value 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); stringifyArray(generator, collectionManager); generator.writeEnd(); } else { //Is a object... probably BeanManager objectManager = new BeanManager(value); System.out.println("Nuevo Objecto {}: " + value.getClass().getSimpleName()); generator.writeStartObject(keyName); stringifyObject(generator, objectManager); generator.writeEnd(); } } }
From source file:nl.nn.adapterframework.align.content.JsonDocumentContainer.java
protected void generate(JsonGenerator g, String key, Object item) { if (item == null) { if (key != null) g.writeNull(key);// w w w. j a va 2 s.co m else g.writeNull(); } else if (item instanceof String) { if (key != null) g.write(key, (String) item); else g.write((String) item); } else if (item instanceof Map) { if (key != null) g.writeStartObject(key); else g.writeStartObject(); for (Entry<String, Object> entry : ((Map<String, Object>) item).entrySet()) { generate(g, entry.getKey(), entry.getValue()); } g.writeEnd(); } else if (item instanceof List) { if (key != null) g.writeStartArray(key); else g.writeStartArray(); for (Object subitem : (List) item) { generate(g, null, subitem); } g.writeEnd(); } else { throw new NotImplementedException("cannot handle class [" + item.getClass().getName() + "]"); } }