/** * Construct a JSONObject from a subset of another JSONObject. An array of strings is used to * identify the keys that should be copied. Missing keys are ignored. * * @param jo A JSONObject. * @param names An array of strings. * @throws JSONException * @exception JSONException If a value is a non-finite number or if a name is duplicated. */ public JSONObject(JSONObject jo, String[] names) { this(); for (int i = 0; i < names.length; i += 1) { try { this.putOnce(names[i], jo.opt(names[i])); } catch (Exception ignore) { } } }
/** * Convert a JSONObject into a well-formed, element-normal XML string. * * @param object A JSONObject. * @param tagName The optional name of the enclosing tag. * @return A string. * @throws JSONException */ public static String toString(Object object, String tagName) throws JSONException { StringBuffer sb = new StringBuffer(); int i; JSONArray ja; JSONObject jo; String key; Iterator keys; int length; String string; Object value; if (object instanceof JSONObject) { // Emit <tagName> if (tagName != null) { sb.append('<'); sb.append(tagName); sb.append('>'); } // Loop thru the keys. jo = (JSONObject) object; keys = jo.keys(); while (keys.hasNext()) { key = keys.next().toString(); value = jo.opt(key); if (value == null) { value = ""; } if (value instanceof String) { string = (String) value; } else { string = null; } // Emit content in body if ("content".equals(key)) { if (value instanceof JSONArray) { ja = (JSONArray) value; length = ja.length(); for (i = 0; i < length; i += 1) { if (i > 0) { sb.append('\n'); } sb.append(escape(ja.get(i).toString())); } } else { sb.append(escape(value.toString())); } // Emit an array of similar keys } else if (value instanceof JSONArray) { ja = (JSONArray) value; length = ja.length(); for (i = 0; i < length; i += 1) { value = ja.get(i); if (value instanceof JSONArray) { sb.append('<'); sb.append(key); sb.append('>'); sb.append(toString(value)); sb.append("</"); sb.append(key); sb.append('>'); } else { sb.append(toString(value, key)); } } } else if ("".equals(value)) { sb.append('<'); sb.append(key); sb.append("/>"); // Emit a new tag <k> } else { sb.append(toString(value, key)); } } if (tagName != null) { // Emit the </tagname> close tag sb.append("</"); sb.append(tagName); sb.append('>'); } return sb.toString(); // XML does not have good support for arrays. If an array appears in a place // where XML is lacking, synthesize an <array> element. } else { if (object.getClass().isArray()) { object = new JSONArray(object); } if (object instanceof JSONArray) { ja = (JSONArray) object; length = ja.length(); for (i = 0; i < length; i += 1) { sb.append(toString(ja.opt(i), tagName == null ? "array" : tagName)); } return sb.toString(); } else { string = (object == null) ? "null" : escape(object.toString()); return (tagName == null) ? "\"" + string + "\"" : (string.length() == 0) ? "<" + tagName + "/>" : "<" + tagName + ">" + string + "</" + tagName + ">"; } } }
/** * Scan the content following the named tag, attaching it to the context. * * @param x The XMLTokener containing the source string. * @param context The JSONObject that will include the new material. * @param name The tag name. * @return true if the close tag is processed. * @throws JSONException */ private static boolean parse(XMLTokener x, JSONObject context, String name) throws JSONException { char c; int i; JSONObject jsonobject = null; String string; String tagName; Object token; // Test for and skip past these forms: // <!-- ... --> // <! ... > // <![ ... ]]> // <? ... ?> // Report errors for these forms: // <> // <= // << token = x.nextToken(); // <! if (token == BANG) { c = x.next(); if (c == '-') { if (x.next() == '-') { x.skipPast("-->"); return false; } x.back(); } else if (c == '[') { token = x.nextToken(); if ("CDATA".equals(token)) { if (x.next() == '[') { string = x.nextCDATA(); if (string.length() > 0) { context.accumulate("content", string); } return false; } } throw x.syntaxError("Expected 'CDATA['"); } i = 1; do { token = x.nextMeta(); if (token == null) { throw x.syntaxError("Missing '>' after '<!'."); } else if (token == LT) { i += 1; } else if (token == GT) { i -= 1; } } while (i > 0); return false; } else if (token == QUEST) { // <? x.skipPast("?>"); return false; } else if (token == SLASH) { // Close tag </ token = x.nextToken(); if (name == null) { throw x.syntaxError("Mismatched close tag " + token); } if (!token.equals(name)) { throw x.syntaxError("Mismatched " + name + " and " + token); } if (x.nextToken() != GT) { throw x.syntaxError("Misshaped close tag"); } return true; } else if (token instanceof Character) { throw x.syntaxError("Misshaped tag"); // Open tag < } else { tagName = (String) token; token = null; jsonobject = new JSONObject(); for (; ; ) { if (token == null) { token = x.nextToken(); } // attribute = value if (token instanceof String) { string = (String) token; token = x.nextToken(); if (token == EQ) { token = x.nextToken(); if (!(token instanceof String)) { throw x.syntaxError("Missing value"); } jsonobject.accumulate(string, XML.stringToValue((String) token)); token = null; } else { jsonobject.accumulate(string, ""); } // Empty tag <.../> } else if (token == SLASH) { if (x.nextToken() != GT) { throw x.syntaxError("Misshaped tag"); } if (jsonobject.length() > 0) { context.accumulate(tagName, jsonobject); } else { context.accumulate(tagName, ""); } return false; // Content, between <...> and </...> } else if (token == GT) { for (; ; ) { token = x.nextContent(); if (token == null) { if (tagName != null) { throw x.syntaxError("Unclosed tag " + tagName); } return false; } else if (token instanceof String) { string = (String) token; if (string.length() > 0) { jsonobject.accumulate("content", XML.stringToValue(string)); } // Nested element } else if (token == LT) { if (parse(x, jsonobject, tagName)) { if (jsonobject.length() == 0) { context.accumulate(tagName, ""); } else if (jsonobject.length() == 1 && jsonobject.opt("content") != null) { context.accumulate(tagName, jsonobject.opt("content")); } else { context.accumulate(tagName, jsonobject); } return false; } } } } else { throw x.syntaxError("Misshaped tag"); } } } }
/** * Convert a JSONObject into a well-formed, element-normal XML string. * * @param o A JSONObject. * @param tagName The optional name of the enclosing tag. * @return A string. * @throws JSONException */ public static String toString(Object o, String tagName) throws JSONException { StringBuffer b = new StringBuffer(); int i; JSONArray ja; JSONObject jo; String k; Iterator<?> keys; int len; String s; Object v; if (o instanceof JSONObject) { // Emit <tagName> if (tagName != null) { b.append('<'); b.append(tagName); b.append('>'); } // Loop thru the keys. jo = (JSONObject) o; keys = jo.keys(); while (keys.hasNext()) { k = keys.next().toString(); v = jo.opt(k); if (v == null) { v = ""; } if (v instanceof String) { s = (String) v; } else { s = null; } // Emit content in body if (k.equals("content")) { if (v instanceof JSONArray) { ja = (JSONArray) v; len = ja.length(); for (i = 0; i < len; i += 1) { if (i > 0) { b.append('\n'); } b.append(escape(ja.get(i).toString())); } } else { b.append(escape(v.toString())); } // Emit an array of similar keys } else if (v instanceof JSONArray) { ja = (JSONArray) v; len = ja.length(); for (i = 0; i < len; i += 1) { v = ja.get(i); if (v instanceof JSONArray) { b.append('<'); b.append(k); b.append('>'); b.append(toString(v)); b.append("</"); b.append(k); b.append('>'); } else { b.append(toString(v, k)); } } } else if (v.equals("")) { b.append('<'); b.append(k); b.append("/>"); // Emit a new tag <k> } else { b.append(toString(v, k)); } } if (tagName != null) { // Emit the </tagname> close tag b.append("</"); b.append(tagName); b.append('>'); } return b.toString(); // XML does not have good support for arrays. If an array appears in a place // where XML is lacking, synthesize an <array> element. } else if (o instanceof JSONArray) { ja = (JSONArray) o; len = ja.length(); for (i = 0; i < len; ++i) { v = ja.opt(i); b.append(toString(v, (tagName == null) ? "array" : tagName)); } return b.toString(); } else { s = (o == null) ? "null" : escape(o.toString()); return (tagName == null) ? "\"" + s + "\"" : (s.length() == 0) ? "<" + tagName + "/>" : "<" + tagName + ">" + s + "</" + tagName + ">"; } }
/** * Scan the content following the named tag, attaching it to the context. * * @param x The XMLTokener containing the source string. * @param context The JSONObject that will include the new material. * @param name The tag name. * @return true if the close tag is processed. * @throws JSONException */ private static boolean parse(XMLTokener x, JSONObject context, String name) throws JSONException { char c; int i; String n; JSONObject o = null; String s; Object t; // Test for and skip past these forms: // <!-- ... --> // <! ... > // <![ ... ]]> // <? ... ?> // Report errors for these forms: // <> // <= // << t = x.nextToken(); // <! if (t == BANG) { c = x.next(); if (c == '-') { if (x.next() == '-') { x.skipPast("-->"); return false; } x.back(); } else if (c == '[') { t = x.nextToken(); if (t.equals("CDATA")) { if (x.next() == '[') { s = x.nextCDATA(); if (s.length() > 0) { context.accumulate("content", s); } return false; } } throw x.syntaxError("Expected 'CDATA['"); } i = 1; do { t = x.nextMeta(); if (t == null) { throw x.syntaxError("Missing '>' after '<!'."); } else if (t == LT) { i += 1; } else if (t == GT) { i -= 1; } } while (i > 0); return false; } else if (t == QUEST) { // <? x.skipPast("?>"); return false; } else if (t == SLASH) { // Close tag </ t = x.nextToken(); if (name == null) { throw x.syntaxError("Mismatched close tag" + t); } if (!t.equals(name)) { throw x.syntaxError("Mismatched " + name + " and " + t); } if (x.nextToken() != GT) { throw x.syntaxError("Misshaped close tag"); } return true; } else if (t instanceof Character) { throw x.syntaxError("Misshaped tag"); // Open tag < } else { n = (String) t; t = null; o = new JSONObject(); for (; ; ) { if (t == null) { t = x.nextToken(); } // attribute = value if (t instanceof String) { s = (String) t; t = x.nextToken(); if (t == EQ) { t = x.nextToken(); if (!(t instanceof String)) { throw x.syntaxError("Missing value"); } o.accumulate(s, JSONObject.stringToValue((String) t)); t = null; } else { o.accumulate(s, ""); } // Empty tag <.../> } else if (t == SLASH) { if (x.nextToken() != GT) { throw x.syntaxError("Misshaped tag"); } if (o.length() > 0) { context.accumulate(n, o); } else { context.accumulate(n, ""); } return false; // Content, between <...> and </...> } else if (t == GT) { for (; ; ) { t = x.nextContent(); if (t == null) { if (n != null) { throw x.syntaxError("Unclosed tag " + n); } return false; } else if (t instanceof String) { s = (String) t; if (s.length() > 0) { o.accumulate("content", JSONObject.stringToValue(s)); } // Nested element } else if (t == LT) { if (parse(x, o, n)) { if (o.length() == 0) { context.accumulate(n, ""); } else if (o.length() == 1 && o.opt("content") != null) { context.accumulate(n, o.opt("content")); } else { context.accumulate(n, o); } return false; } } } } else { throw x.syntaxError("Misshaped tag"); } } } }