public static void dumpJSON(JsonValue tree, String key, String depthPrefix) { if (key != null) logger.info(depthPrefix + "Key " + key + ": "); switch (tree.getValueType()) { case OBJECT: logger.info(depthPrefix + "OBJECT"); JsonObject object = (JsonObject) tree; for (String name : object.keySet()) dumpJSON(object.get(name), name, depthPrefix + " "); break; case ARRAY: logger.info(depthPrefix + "ARRAY"); JsonArray array = (JsonArray) tree; for (JsonValue val : array) dumpJSON(val, null, depthPrefix + " "); break; case STRING: JsonString st = (JsonString) tree; logger.info(depthPrefix + "STRING " + st.getString()); break; case NUMBER: JsonNumber num = (JsonNumber) tree; logger.info(depthPrefix + "NUMBER " + num.toString()); break; case TRUE: case FALSE: case NULL: logger.info(depthPrefix + tree.getValueType().toString()); break; } }
/* @see com.ibm.jaql.json.type.JsonValue#compareTo(java.lang.Object) */ @Override public int compareTo(Object x) { JsonNumber other = (JsonNumber) x; JsonType otherType = other.getEncoding().getType(); switch (otherType) { case LONG: return compare(this, (JsonLong) other); case DOUBLE: return compare(this, (JsonDouble) other); case DECFLOAT: return compare(this, (JsonDecimal) other); default: throw new IllegalStateException("unknown numeric type " + otherType); } }
/** * http://docs.oracle.com/middleware/1213/wls/WLPRG/java-api-for-json-proc.htm#WLPRG1061 * * @param tree * @param key */ public static Object navigateTree(JsonValue tree, String key) { Object ret = null; if (key != null) System.out.print("Key " + key + ": "); switch (tree.getValueType()) { case OBJECT: System.out.println("OBJECT"); JsonObject object = (JsonObject) tree; for (String name : object.keySet()) navigateTree(object.get(name), name); break; case ARRAY: System.out.println("ARRAY"); JsonArray array = (JsonArray) tree; for (JsonValue val : array) navigateTree(val, null); break; case STRING: JsonString st = (JsonString) tree; System.out.println("STRING " + st.getString()); break; case NUMBER: JsonNumber num = (JsonNumber) tree; System.out.println("NUMBER " + num.toString()); break; case TRUE: System.out.println("TRUE"); case FALSE: System.out.println("FALSE"); case NULL: System.out.println(tree.getValueType().toString()); break; } return ret; }
public static int navigateTree(JsonValue tree, String skip) { int ret = 0; switch (tree.getValueType()) { case OBJECT: // System.out.println("OBJECT"); JsonArrayBuilder dirtyResult = Json.createArrayBuilder(); JsonObject object = (JsonObject) tree; for (String name : object.keySet()) { if (object.get(name).getValueType() == JsonValue.ValueType.STRING && object.getString(name).equals(skip)) { return 0; } dirtyResult.add(object.get(name)); } ret = ret + navigateTree(dirtyResult.build(), skip); break; case ARRAY: // System.out.println("ARRAY"); JsonArray array = (JsonArray) tree; for (JsonValue val : array) ret = ret + navigateTree(val, skip); break; case STRING: JsonString st = (JsonString) tree; // System.out.println("STRING " + st.getString()); break; case NUMBER: JsonNumber num = (JsonNumber) tree; return num.intValue(); case TRUE: case FALSE: case NULL: // System.out.println(tree.getValueType().toString()); break; } return ret; }
@Override public void visit(JsonNumber value) { out.append(value.toString()); }