@@ -36,24 +36,6 @@ of this software and associated documentation files (the "Software"), to deal
3636 * @version 2016-01-30
3737 */
3838public class JSONML {
39-
40- /**
41- * Parse XML values and store them in a JSONArray.
42- * @param x The XMLTokener containing the source string.
43- * @param arrayForm true if array form, false if object form.
44- * @param ja The JSONArray that is containing the current tag or null
45- * if we are at the outermost level.
46- * @return A JSONArray if the value is the outermost tag, otherwise null.
47- * @throws JSONException
48- */
49- private static Object parse (
50- XMLTokener x ,
51- boolean arrayForm ,
52- JSONArray ja
53- ) throws JSONException {
54- return parse (x , arrayForm , ja , false );
55- }
56-
5739 /**
5840 * Parse XML values and store them in a JSONArray.
5941 * @param x The XMLTokener containing the source string.
@@ -212,9 +194,8 @@ private static Object parse(
212194 if (ja == null ) {
213195 if (arrayForm ) {
214196 return newja ;
215- } else {
216- return newjo ;
217197 }
198+ return newjo ;
218199 }
219200
220201// Content, between <...> and </...>
@@ -236,9 +217,8 @@ private static Object parse(
236217 if (ja == null ) {
237218 if (arrayForm ) {
238219 return newja ;
239- } else {
240- return newjo ;
241220 }
221+ return newjo ;
242222 }
243223 }
244224 }
@@ -264,10 +244,10 @@ private static Object parse(
264244 * Comments, prologs, DTDs, and <code><[ [ ]]></code> are ignored.
265245 * @param string The source string.
266246 * @return A JSONArray containing the structured data from the XML string.
267- * @throws JSONException
247+ * @throws JSONException Thrown on error converting to a JSONArray
268248 */
269249 public static JSONArray toJSONArray (String string ) throws JSONException {
270- return toJSONArray ( new XMLTokener (string ));
250+ return ( JSONArray ) parse ( new XMLTokener (string ), true , null , false );
271251 }
272252
273253
@@ -283,11 +263,13 @@ public static JSONArray toJSONArray(String string) throws JSONException {
283263 * but just leaves it as a string.
284264 * Comments, prologs, DTDs, and <code><[ [ ]]></code> are ignored.
285265 * @param string The source string.
266+ * @param keepStrings If true, then values will not be coerced into boolean
267+ * or numeric values and will instead be left as strings
286268 * @return A JSONArray containing the structured data from the XML string.
287- * @throws JSONException
269+ * @throws JSONException Thrown on error converting to a JSONArray
288270 */
289- public static JSONArray toJsonML (String string ) throws JSONException {
290- return toJsonML ( new XMLTokener (string ));
271+ public static JSONArray toJSONArray (String string , boolean keepStrings ) throws JSONException {
272+ return ( JSONArray ) parse ( new XMLTokener (string ), true , null , keepStrings );
291273 }
292274
293275
@@ -303,11 +285,13 @@ public static JSONArray toJsonML(String string) throws JSONException {
303285 * but just leaves it as a string.
304286 * Comments, prologs, DTDs, and <code><[ [ ]]></code> are ignored.
305287 * @param x An XMLTokener.
288+ * @param keepStrings If true, then values will not be coerced into boolean
289+ * or numeric values and will instead be left as strings
306290 * @return A JSONArray containing the structured data from the XML string.
307- * @throws JSONException
291+ * @throws JSONException Thrown on error converting to a JSONArray
308292 */
309- public static JSONArray toJsonML (XMLTokener x ) throws JSONException {
310- return (JSONArray )parse (x , true , null , true );
293+ public static JSONArray toJSONArray (XMLTokener x , boolean keepStrings ) throws JSONException {
294+ return (JSONArray )parse (x , true , null , keepStrings );
311295 }
312296
313297
@@ -321,13 +305,51 @@ public static JSONArray toJsonML(XMLTokener x) throws JSONException {
321305 * Comments, prologs, DTDs, and <code><[ [ ]]></code> are ignored.
322306 * @param x An XMLTokener.
323307 * @return A JSONArray containing the structured data from the XML string.
324- * @throws JSONException
308+ * @throws JSONException Thrown on error converting to a JSONArray
325309 */
326310 public static JSONArray toJSONArray (XMLTokener x ) throws JSONException {
327- return (JSONArray )parse (x , true , null );
311+ return (JSONArray )parse (x , true , null , false );
328312 }
329313
330314
315+ /**
316+ * Convert a well-formed (but not necessarily valid) XML string into a
317+ * JSONObject using the JsonML transform. Each XML tag is represented as
318+ * a JSONObject with a "tagName" property. If the tag has attributes, then
319+ * the attributes will be in the JSONObject as properties. If the tag
320+ * contains children, the object will have a "childNodes" property which
321+ * will be an array of strings and JsonML JSONObjects.
322+
323+ * Comments, prologs, DTDs, and <code><[ [ ]]></code> are ignored.
324+ * @param string The XML source text.
325+ * @return A JSONObject containing the structured data from the XML string.
326+ * @throws JSONException Thrown on error converting to a JSONObject
327+ */
328+ public static JSONObject toJSONObject (String string ) throws JSONException {
329+ return (JSONObject )parse (new XMLTokener (string ), false , null , false );
330+ }
331+
332+
333+ /**
334+ * Convert a well-formed (but not necessarily valid) XML string into a
335+ * JSONObject using the JsonML transform. Each XML tag is represented as
336+ * a JSONObject with a "tagName" property. If the tag has attributes, then
337+ * the attributes will be in the JSONObject as properties. If the tag
338+ * contains children, the object will have a "childNodes" property which
339+ * will be an array of strings and JsonML JSONObjects.
340+
341+ * Comments, prologs, DTDs, and <code><[ [ ]]></code> are ignored.
342+ * @param string The XML source text.
343+ * @param keepStrings If true, then values will not be coerced into boolean
344+ * or numeric values and will instead be left as strings
345+ * @return A JSONObject containing the structured data from the XML string.
346+ * @throws JSONException Thrown on error converting to a JSONObject
347+ */
348+ public static JSONObject toJSONObject (String string , boolean keepStrings ) throws JSONException {
349+ return (JSONObject )parse (new XMLTokener (string ), false , null , keepStrings );
350+ }
351+
352+
331353 /**
332354 * Convert a well-formed (but not necessarily valid) XML string into a
333355 * JSONObject using the JsonML transform. Each XML tag is represented as
@@ -339,10 +361,10 @@ public static JSONArray toJSONArray(XMLTokener x) throws JSONException {
339361 * Comments, prologs, DTDs, and <code><[ [ ]]></code> are ignored.
340362 * @param x An XMLTokener of the XML source text.
341363 * @return A JSONObject containing the structured data from the XML string.
342- * @throws JSONException
364+ * @throws JSONException Thrown on error converting to a JSONObject
343365 */
344366 public static JSONObject toJSONObject (XMLTokener x ) throws JSONException {
345- return (JSONObject )parse (x , false , null );
367+ return (JSONObject )parse (x , false , null , false );
346368 }
347369
348370
@@ -355,20 +377,22 @@ public static JSONObject toJSONObject(XMLTokener x) throws JSONException {
355377 * will be an array of strings and JsonML JSONObjects.
356378
357379 * Comments, prologs, DTDs, and <code><[ [ ]]></code> are ignored.
358- * @param string The XML source text.
380+ * @param x An XMLTokener of the XML source text.
381+ * @param keepStrings If true, then values will not be coerced into boolean
382+ * or numeric values and will instead be left as strings
359383 * @return A JSONObject containing the structured data from the XML string.
360- * @throws JSONException
384+ * @throws JSONException Thrown on error converting to a JSONObject
361385 */
362- public static JSONObject toJSONObject (String string ) throws JSONException {
363- return toJSONObject ( new XMLTokener ( string ) );
386+ public static JSONObject toJSONObject (XMLTokener x , boolean keepStrings ) throws JSONException {
387+ return ( JSONObject ) parse ( x , false , null , keepStrings );
364388 }
365389
366390
367391 /**
368392 * Reverse the JSONML transformation, making an XML text from a JSONArray.
369393 * @param ja A JSONArray.
370394 * @return An XML string.
371- * @throws JSONException
395+ * @throws JSONException Thrown on error converting to a string
372396 */
373397 public static String toString (JSONArray ja ) throws JSONException {
374398 int i ;
@@ -452,7 +476,7 @@ public static String toString(JSONArray ja) throws JSONException {
452476 * The other properties are attributes with string values.
453477 * @param jo A JSONObject.
454478 * @return An XML string.
455- * @throws JSONException
479+ * @throws JSONException Thrown on error converting to a string
456480 */
457481 public static String toString (JSONObject jo ) throws JSONException {
458482 StringBuilder sb = new StringBuilder ();
0 commit comments