4 Copyright (c) 2015 JSON.org
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
13 The above copyright notice and this permission notice shall be included in all
14 copies or substantial portions of the Software.
16 The Software shall be used for Good, not Evil.
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27 import java.io.Reader;
28 import java.io.StringReader;
29 import java.util.Iterator;
32 * This provides static methods to convert an XML text into a JSONObject, and to
33 * covert a JSONObject into an XML text.
38 @SuppressWarnings("boxing")
40 /** The Character '&'. */
41 public static final Character AMP = '&';
43 /** The Character '''. */
44 public static final Character APOS = '\'';
46 /** The Character '!'. */
47 public static final Character BANG = '!';
49 /** The Character '='. */
50 public static final Character EQ = '=';
52 /** The Character '>'. */
53 public static final Character GT = '>';
55 /** The Character '<'. */
56 public static final Character LT = '<';
58 /** The Character '?'. */
59 public static final Character QUEST = '?';
61 /** The Character '"'. */
62 public static final Character QUOT = '"';
64 /** The Character '/'. */
65 public static final Character SLASH = '/';
68 * Creates an iterator for navigating Code Points in a string instead of
69 * characters. Once Java7 support is dropped, this can be replaced with
73 * which is available in Java8 and above.
76 * "http://stackoverflow.com/a/21791059/6030888">http://stackoverflow.com/a/21791059/6030888</a>
78 private static Iterable<Integer> codePointIterator(final String string) {
79 return new Iterable<Integer>() {
81 public Iterator<Integer> iterator() {
82 return new Iterator<Integer>() {
83 private int nextIndex = 0;
84 private int length = string.length();
87 public boolean hasNext() {
88 return this.nextIndex < this.length;
92 public Integer next() {
93 int result = string.codePointAt(this.nextIndex);
94 this.nextIndex += Character.charCount(result);
99 public void remove() {
100 throw new UnsupportedOperationException();
108 * Replace special characters with XML escapes:
111 * & <small>(ampersand)</small> is replaced by &amp;
112 * < <small>(less than)</small> is replaced by &lt;
113 * > <small>(greater than)</small> is replaced by &gt;
114 * " <small>(double quote)</small> is replaced by &quot;
115 * ' <small>(single quote / apostrophe)</small> is replaced by &apos;
119 * The string to be escaped.
120 * @return The escaped string.
122 public static String escape(String string) {
123 StringBuilder sb = new StringBuilder(string.length());
124 for (final int cp : codePointIterator(string)) {
142 if (mustEscape(cp)) {
144 sb.append(Integer.toHexString(cp));
147 sb.appendCodePoint(cp);
151 return sb.toString();
155 * @param cp code point to test
156 * @return true if the code point is not valid for an XML
158 private static boolean mustEscape(int cp) {
159 /* Valid range from https://www.w3.org/TR/REC-xml/#charsets
161 * #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF]
163 * any Unicode character, excluding the surrogate blocks, FFFE, and FFFF.
165 // isISOControl is true when (cp >= 0 && cp <= 0x1F) || (cp >= 0x7F && cp <= 0x9F)
166 // all ISO control characters are out of range except tabs and new lines
167 return (Character.isISOControl(cp)
172 // valid the range of acceptable characters that aren't control
173 (cp >= 0x20 && cp <= 0xD7FF)
174 || (cp >= 0xE000 && cp <= 0xFFFD)
175 || (cp >= 0x10000 && cp <= 0x10FFFF)
181 * Removes XML escapes from the string.
184 * string to remove escapes from
185 * @return string with converted entities
187 public static String unescape(String string) {
188 StringBuilder sb = new StringBuilder(string.length());
189 for (int i = 0, length = string.length(); i < length; i++) {
190 char c = string.charAt(i);
192 final int semic = string.indexOf(';', i);
194 final String entity = string.substring(i + 1, semic);
195 sb.append(XMLTokener.unescapeEntity(entity));
196 // skip past the entity we just parsed.
197 i += entity.length() + 1;
199 // this shouldn't happen in most cases since the parser
200 // errors on unclosed entries.
204 // not part of an entity
208 return sb.toString();
212 * Throw an exception if the string contains whitespace. Whitespace is not
213 * allowed in tagNames and attributes.
217 * @throws JSONException Thrown if the string contains whitespace or is empty.
219 public static void noSpace(String string) throws JSONException {
220 int i, length = string.length();
222 throw new JSONException("Empty string.");
224 for (i = 0; i < length; i += 1) {
225 if (Character.isWhitespace(string.charAt(i))) {
226 throw new JSONException("'" + string
227 + "' contains a space character.");
233 * Scan the content following the named tag, attaching it to the context.
236 * The XMLTokener containing the source string.
238 * The JSONObject that will include the new material.
241 * @return true if the close tag is processed.
242 * @throws JSONException
244 private static boolean parse(XMLTokener x, JSONObject context, String name, boolean keepStrings)
245 throws JSONException {
248 JSONObject jsonobject = null;
253 // Test for and skip past these forms:
258 // Report errors for these forms:
263 token = x.nextToken();
270 if (x.next() == '-') {
275 } else if (c == '[') {
276 token = x.nextToken();
277 if ("CDATA".equals(token)) {
278 if (x.next() == '[') {
279 string = x.nextCDATA();
280 if (string.length() > 0) {
281 context.accumulate("content", string);
286 throw x.syntaxError("Expected 'CDATA['");
290 token = x.nextMeta();
292 throw x.syntaxError("Missing '>' after '<!'.");
293 } else if (token == LT) {
295 } else if (token == GT) {
300 } else if (token == QUEST) {
305 } else if (token == SLASH) {
309 token = x.nextToken();
311 throw x.syntaxError("Mismatched close tag " + token);
313 if (!token.equals(name)) {
314 throw x.syntaxError("Mismatched " + name + " and " + token);
316 if (x.nextToken() != GT) {
317 throw x.syntaxError("Misshaped close tag");
321 } else if (token instanceof Character) {
322 throw x.syntaxError("Misshaped tag");
327 tagName = (String) token;
329 jsonobject = new JSONObject();
332 token = x.nextToken();
335 if (token instanceof String) {
336 string = (String) token;
337 token = x.nextToken();
339 token = x.nextToken();
340 if (!(token instanceof String)) {
341 throw x.syntaxError("Missing value");
343 jsonobject.accumulate(string,
344 keepStrings ? ((String)token) : stringToValue((String) token));
347 jsonobject.accumulate(string, "");
351 } else if (token == SLASH) {
353 if (x.nextToken() != GT) {
354 throw x.syntaxError("Misshaped tag");
356 if (jsonobject.length() > 0) {
357 context.accumulate(tagName, jsonobject);
359 context.accumulate(tagName, "");
363 } else if (token == GT) {
364 // Content, between <...> and </...>
366 token = x.nextContent();
368 if (tagName != null) {
369 throw x.syntaxError("Unclosed tag " + tagName);
372 } else if (token instanceof String) {
373 string = (String) token;
374 if (string.length() > 0) {
375 jsonobject.accumulate("content",
376 keepStrings ? string : stringToValue(string));
379 } else if (token == LT) {
381 if (parse(x, jsonobject, tagName,keepStrings)) {
382 if (jsonobject.length() == 0) {
383 context.accumulate(tagName, "");
384 } else if (jsonobject.length() == 1
385 && jsonobject.opt("content") != null) {
386 context.accumulate(tagName,
387 jsonobject.opt("content"));
389 context.accumulate(tagName, jsonobject);
396 throw x.syntaxError("Misshaped tag");
403 * This method is the same as {@link JSONObject#stringToValue(String)}.
405 * @param string String to convert
406 * @return JSON value of this string or the string
408 // To maintain compatibility with the Android API, this method is a direct copy of
409 // the one in JSONObject. Changes made here should be reflected there.
410 public static Object stringToValue(String string) {
411 if (string.equals("")) {
414 if (string.equalsIgnoreCase("true")) {
417 if (string.equalsIgnoreCase("false")) {
418 return Boolean.FALSE;
420 if (string.equalsIgnoreCase("null")) {
421 return JSONObject.NULL;
425 * If it might be a number, try converting it. If a number cannot be
426 * produced, then the value will just be a string.
429 char initial = string.charAt(0);
430 if ((initial >= '0' && initial <= '9') || initial == '-') {
432 // if we want full Big Number support this block can be replaced with:
433 // return stringToNumber(string);
434 if (string.indexOf('.') > -1 || string.indexOf('e') > -1
435 || string.indexOf('E') > -1 || "-0".equals(string)) {
436 Double d = Double.valueOf(string);
437 if (!d.isInfinite() && !d.isNaN()) {
441 Long myLong = Long.valueOf(string);
442 if (string.equals(myLong.toString())) {
443 if (myLong.longValue() == myLong.intValue()) {
444 return Integer.valueOf(myLong.intValue());
449 } catch (Exception ignore) {
456 * Convert a well-formed (but not necessarily valid) XML string into a
457 * JSONObject. Some information may be lost in this transformation because
458 * JSON is a data format and XML is a document format. XML uses elements,
459 * attributes, and content text, while JSON uses unordered collections of
460 * name/value pairs and arrays of values. JSON does not does not like to
461 * distinguish between elements and attributes. Sequences of similar
462 * elements are represented as JSONArrays. Content text may be placed in a
463 * "content" member. Comments, prologs, DTDs, and <code><[ [ ]]></code>
468 * @return A JSONObject containing the structured data from the XML string.
469 * @throws JSONException Thrown if there is an errors while parsing the string
471 public static JSONObject toJSONObject(String string) throws JSONException {
472 return toJSONObject(string, false);
476 * Convert a well-formed (but not necessarily valid) XML into a
477 * JSONObject. Some information may be lost in this transformation because
478 * JSON is a data format and XML is a document format. XML uses elements,
479 * attributes, and content text, while JSON uses unordered collections of
480 * name/value pairs and arrays of values. JSON does not does not like to
481 * distinguish between elements and attributes. Sequences of similar
482 * elements are represented as JSONArrays. Content text may be placed in a
483 * "content" member. Comments, prologs, DTDs, and <code><[ [ ]]></code>
486 * @param reader The XML source reader.
487 * @return A JSONObject containing the structured data from the XML string.
488 * @throws JSONException Thrown if there is an errors while parsing the string
490 public static JSONObject toJSONObject(Reader reader) throws JSONException {
491 return toJSONObject(reader, false);
495 * Convert a well-formed (but not necessarily valid) XML into a
496 * JSONObject. Some information may be lost in this transformation because
497 * JSON is a data format and XML is a document format. XML uses elements,
498 * attributes, and content text, while JSON uses unordered collections of
499 * name/value pairs and arrays of values. JSON does not does not like to
500 * distinguish between elements and attributes. Sequences of similar
501 * elements are represented as JSONArrays. Content text may be placed in a
502 * "content" member. Comments, prologs, DTDs, and <code><[ [ ]]></code>
505 * All values are converted as strings, for 1, 01, 29.0 will not be coerced to
506 * numbers but will instead be the exact value as seen in the XML document.
508 * @param reader The XML source reader.
509 * @param keepStrings If true, then values will not be coerced into boolean
510 * or numeric values and will instead be left as strings
511 * @return A JSONObject containing the structured data from the XML string.
512 * @throws JSONException Thrown if there is an errors while parsing the string
514 public static JSONObject toJSONObject(Reader reader, boolean keepStrings) throws JSONException {
515 JSONObject jo = new JSONObject();
516 XMLTokener x = new XMLTokener(reader);
520 parse(x, jo, null, keepStrings);
527 * Convert a well-formed (but not necessarily valid) XML string into a
528 * JSONObject. Some information may be lost in this transformation because
529 * JSON is a data format and XML is a document format. XML uses elements,
530 * attributes, and content text, while JSON uses unordered collections of
531 * name/value pairs and arrays of values. JSON does not does not like to
532 * distinguish between elements and attributes. Sequences of similar
533 * elements are represented as JSONArrays. Content text may be placed in a
534 * "content" member. Comments, prologs, DTDs, and <code><[ [ ]]></code>
537 * All values are converted as strings, for 1, 01, 29.0 will not be coerced to
538 * numbers but will instead be the exact value as seen in the XML document.
542 * @param keepStrings If true, then values will not be coerced into boolean
543 * or numeric values and will instead be left as strings
544 * @return A JSONObject containing the structured data from the XML string.
545 * @throws JSONException Thrown if there is an errors while parsing the string
547 public static JSONObject toJSONObject(String string, boolean keepStrings) throws JSONException {
548 return toJSONObject(new StringReader(string), keepStrings);
552 * Convert a JSONObject into a well-formed, element-normal XML string.
557 * @throws JSONException Thrown if there is an error parsing the string
559 public static String toString(Object object) throws JSONException {
560 return toString(object, null);
564 * Convert a JSONObject into a well-formed, element-normal XML string.
569 * The optional name of the enclosing tag.
571 * @throws JSONException Thrown if there is an error parsing the string
573 public static String toString(final Object object, final String tagName)
574 throws JSONException {
575 StringBuilder sb = new StringBuilder();
580 if (object instanceof JSONObject) {
583 if (tagName != null) {
589 // Loop thru the keys.
590 // don't use the new entrySet accessor to maintain Android Support
591 jo = (JSONObject) object;
592 for (final String key : jo.keySet()) {
593 Object value = jo.opt(key);
596 } else if (value.getClass().isArray()) {
597 value = new JSONArray(value);
600 // Emit content in body
601 if ("content".equals(key)) {
602 if (value instanceof JSONArray) {
603 ja = (JSONArray) value;
604 int jaLength = ja.length();
605 // don't use the new iterator API to maintain support for Android
606 for (int i = 0; i < jaLength; i++) {
610 Object val = ja.opt(i);
611 sb.append(escape(val.toString()));
614 sb.append(escape(value.toString()));
617 // Emit an array of similar keys
619 } else if (value instanceof JSONArray) {
620 ja = (JSONArray) value;
621 int jaLength = ja.length();
622 // don't use the new iterator API to maintain support for Android
623 for (int i = 0; i < jaLength; i++) {
624 Object val = ja.opt(i);
625 if (val instanceof JSONArray) {
629 sb.append(toString(val));
634 sb.append(toString(val, key));
637 } else if ("".equals(value)) {
642 // Emit a new tag <k>
645 sb.append(toString(value, key));
648 if (tagName != null) {
650 // Emit the </tagname> close tag
655 return sb.toString();
659 if (object != null && (object instanceof JSONArray || object.getClass().isArray())) {
660 if(object.getClass().isArray()) {
661 ja = new JSONArray(object);
663 ja = (JSONArray) object;
665 int jaLength = ja.length();
666 // don't use the new iterator API to maintain support for Android
667 for (int i = 0; i < jaLength; i++) {
668 Object val = ja.opt(i);
669 // XML does not have good support for arrays. If an array
670 // appears in a place where XML is lacking, synthesize an
672 sb.append(toString(val, tagName == null ? "array" : tagName));
674 return sb.toString();
677 string = (object == null) ? "null" : escape(object.toString());
678 return (tagName == null) ? "\"" + string + "\""
679 : (string.length() == 0) ? "<" + tagName + "/>" : "<" + tagName
680 + ">" + string + "</" + tagName + ">";