Beispiel #1
0
    @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");
      }
    }
Beispiel #2
0
 @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();
 }
Beispiel #3
0
 @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");
   }
 }