@SuppressWarnings("rawtypes") @Override protected Object createNode(Object key, Object value) { if (getCurrent() == null && stack.peek() == BuilderMode.OBJECT) { throw new IllegalArgumentException( "only call to [element { }] is allowed when creating array"); } try { int retVal = 0; if (stack.peek().equals(BuilderMode.ARRAY)) { writeObject(); retVal = 1; } if (value instanceof Collection) { Collection c = (Collection) value; writer.key(String.valueOf(key)); handleCollectionRecurse(c); } else { writer.key(String.valueOf(key)); json.convertAnother(value); // .value(value); } return retVal != 0 ? retVal : null; } catch (JSONException e) { throw new IllegalArgumentException("invalid element"); } }
/** * @param o * @throws ConverterException */ public void value(Object o) throws ConverterException { o = config.getProxyHandler().unwrapIfProxy(o); try { if (o == null || o.equals(JSONObject.NULL)) { writer.value(null); } else if (o instanceof CharSequence) { writer.value(o); } else if (o instanceof Class<?>) { writer.value(((Class<?>) o).getName()); } else if ((o.getClass().isPrimitive() && !o.getClass().equals(byte[].class)) || o instanceof Number || o instanceof Boolean) { writer.value(o); } else { if (referenceStack.contains(o)) { handleCircularRelationship(o); } else { referenceStack.push(o); ObjectMarshaller<JSON> marshaller = config.getMarshaller(o); if (marshaller == null) { throw new ConverterException( "Unconvertable Object of class: " + o.getClass().getName()); } marshaller.marshalObject(o, this); referenceStack.pop(); } } } catch (ConverterException ce) { throw ce; } catch (JSONException e) { throw new ConverterException(e); } }
@SuppressWarnings("rawtypes") private void handleCollectionRecurse(Collection c) throws JSONException { writer.array(); for (Object element : c) { if (element instanceof Collection) { handleCollectionRecurse((Collection) element); } else { json.convertAnother(element); } } writer.endArray(); }
@Override protected Object createNode(Object name) { int retVal = 1; try { if (start) { start = false; writeObject(); } else { if (getCurrent() == null && stack.peek() == BuilderMode.OBJECT) { throw new IllegalArgumentException( "only call to [element { }] is allowed when creating array"); } if (stack.peek() == BuilderMode.ARRAY) { writeObject(); retVal = 2; } writer.key(String.valueOf(name)).array(); stack.push(BuilderMode.ARRAY); } } catch (JSONException e) { throw new IllegalArgumentException("invalid element"); } return retVal; }
@SuppressWarnings("rawtypes") @Override protected Object createNode(Object key, Map valueMap) { try { if (stack.peek().equals(BuilderMode.OBJECT)) writer.key(String.valueOf(key)); writer.object(); for (Object o : valueMap.entrySet()) { Map.Entry element = (Map.Entry) o; writer.key(String.valueOf(element.getKey())); // .value(element.getValue()); json.convertAnother(element.getValue()); } writer.endObject(); return null; } catch (JSONException e) { throw new IllegalArgumentException("invalid element"); } }
@Override protected void nodeCompleted(Object parent, Object node) { Object last = null; if (node == null) { return; } try { int i = ((Integer) node); while (i-- > 0) { last = stack.pop(); if (BuilderMode.ARRAY == last) writer.endArray(); if (BuilderMode.OBJECT == last) writer.endObject(); } } catch (JSONException e) { throw new IllegalArgumentException("invalid element on the stack"); } }
private void writeObject() throws JSONException { writer.object(); stack.push(BuilderMode.OBJECT); }
public void property(String key, Object value) throws JSONException, ConverterException { writer.key(key); value(value); }