public static JsonValue getOrCreateChild(JsonValue root, String key, ValueType type) { JsonValue tree = root.get(key); if (tree == null) { tree = new JsonValue(type); tree.setName(key); if (root.size == 0) { root.child = tree; } else { JsonValue prev = root.get(root.size - 1); prev.setNext(tree); tree.setPrev(prev); } ++root.size; } return tree; }
private SectionAccessor getSection(JsonValue root, String section, boolean createIfDoesntExist) { JsonValue tree = null; if (createIfDoesntExist) { tree = JsonUtils.getOrCreateChild(root, section, ValueType.object); } else { tree = root.get(section); } return new SectionAccessor(tree); }
public void set(String key, String value) { JsonValue tree = JsonUtils.getOrCreateChild(root, key, ValueType.stringValue); tree.set(value); }
protected void startObject (String name) { JsonValue value = new JsonValue(ValueType.object); if (current != null) addChild(name, value); elements.add(value); current = value; }
protected void startArray (String name) { JsonValue value = new JsonValue(ValueType.array); if (current != null) addChild(name, value); elements.add(value); current = value; }
private void writeObject (JsonValue root, UBJsonWriter writer) throws IOException { if (root.type() == ValueType.array) { if (root.name() != null) { writer.array(root.name()); } else { writer.array(); } } else { if (root.name() != null) { writer.object(root.name()); } else { writer.object(); } } JsonValue child = root.child(); while (child != null) { switch (child.type()) { case booleanValue: if (child.name()!=null){ writer.set(child.name(), child.asBoolean()); } else{ writer.value(child.asBoolean()); } break; case doubleValue: if (child.name()!=null){ writer.set(child.name(), child.asDouble()); } else{ writer.value(child.asDouble()); } break; case longValue: if (child.name()!=null){ writer.set(child.name(), child.asLong()); } else{ writer.value(child.asLong()); } break; case stringValue: if (child.name()!=null){ writer.set(child.name(), child.asString()); } else{ writer.value(child.asString()); } break; case nullValue: if (child.name()!=null){ writer.set(child.name()); } break; case array: case object: writeObject(child, writer); break; } child = child.next(); } writer.pop(); }