Skip to content

Commit 09d37e5

Browse files
author
John J. Aylward
committed
Cleans up the JSONML changes and adds similar changes to the XML class
1 parent 9370437 commit 09d37e5

File tree

2 files changed

+101
-51
lines changed

2 files changed

+101
-51
lines changed

JSONML.java

Lines changed: 64 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -36,24 +36,6 @@ of this software and associated documentation files (the "Software"), to deal
3636
* @version 2016-01-30
3737
*/
3838
public 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>&lt;[ [ ]]></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>&lt;[ [ ]]></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>&lt;[ [ ]]></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>&lt;[ [ ]]></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>&lt;[ [ ]]></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>&lt;[ [ ]]></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>&lt;[ [ ]]></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>&lt;[ [ ]]></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();

XML.java

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public static String escape(String string) {
110110
*
111111
* @param string
112112
* A string.
113-
* @throws JSONException
113+
* @throws JSONException Thrown if the string contains whitespace or is empty.
114114
*/
115115
public static void noSpace(String string) throws JSONException {
116116
int i, length = string.length();
@@ -137,7 +137,7 @@ public static void noSpace(String string) throws JSONException {
137137
* @return true if the close tag is processed.
138138
* @throws JSONException
139139
*/
140-
private static boolean parse(XMLTokener x, JSONObject context, String name)
140+
private static boolean parse(XMLTokener x, JSONObject context, String name, boolean keepStrings)
141141
throws JSONException {
142142
char c;
143143
int i;
@@ -238,7 +238,7 @@ private static boolean parse(XMLTokener x, JSONObject context, String name)
238238
throw x.syntaxError("Missing value");
239239
}
240240
jsonobject.accumulate(string,
241-
JSONObject.stringToValue((String) token));
241+
keepStrings ? token : JSONObject.stringToValue((String) token));
242242
token = null;
243243
} else {
244244
jsonobject.accumulate(string, "");
@@ -270,12 +270,12 @@ private static boolean parse(XMLTokener x, JSONObject context, String name)
270270
string = (String) token;
271271
if (string.length() > 0) {
272272
jsonobject.accumulate("content",
273-
JSONObject.stringToValue(string));
273+
keepStrings ? token : JSONObject.stringToValue(string));
274274
}
275275

276276
} else if (token == LT) {
277277
// Nested element
278-
if (parse(x, jsonobject, tagName)) {
278+
if (parse(x, jsonobject, tagName,keepStrings)) {
279279
if (jsonobject.length() == 0) {
280280
context.accumulate(tagName, "");
281281
} else if (jsonobject.length() == 1
@@ -301,9 +301,10 @@ private static boolean parse(XMLTokener x, JSONObject context, String name)
301301
* {@link JSONObject.stringToValue(String)} method. Use it instead.
302302
*
303303
* @deprecated Use {@link JSONObject#stringToValue(String)} instead.
304-
* @param string
304+
* @param string String to convert
305305
* @return JSON value of this string or the string
306306
*/
307+
@Deprecated
307308
public static Object stringToValue(String string) {
308309
return JSONObject.stringToValue(string);
309310
}
@@ -322,24 +323,49 @@ public static Object stringToValue(String string) {
322323
* @param string
323324
* The source string.
324325
* @return A JSONObject containing the structured data from the XML string.
325-
* @throws JSONException
326+
* @throws JSONException Thrown if there is an errors while parsing the string
326327
*/
327328
public static JSONObject toJSONObject(String string) throws JSONException {
329+
return toJSONObject(string, false);
330+
}
331+
332+
333+
/**
334+
* Convert a well-formed (but not necessarily valid) XML string into a
335+
* JSONObject. Some information may be lost in this transformation because
336+
* JSON is a data format and XML is a document format. XML uses elements,
337+
* attributes, and content text, while JSON uses unordered collections of
338+
* name/value pairs and arrays of values. JSON does not does not like to
339+
* distinguish between elements and attributes. Sequences of similar
340+
* elements are represented as JSONArrays. Content text may be placed in a
341+
* "content" member. Comments, prologs, DTDs, and <code>&lt;[ [ ]]></code>
342+
* are ignored.
343+
*
344+
* All values are converted as strings, for 1, 01, 29.0 will not be coerced to
345+
* numbers but will instead be the exact value as seen in the XML document.
346+
*
347+
* @param string
348+
* The source string.
349+
* @param keepStrings If true, then values will not be coerced into boolean
350+
* or numeric values and will instead be left as strings
351+
* @return A JSONObject containing the structured data from the XML string.
352+
* @throws JSONException Thrown if there is an errors while parsing the string
353+
*/
354+
public static JSONObject toJSONObject(String string, boolean keepStrings) throws JSONException {
328355
JSONObject jo = new JSONObject();
329356
XMLTokener x = new XMLTokener(string);
330357
while (x.more() && x.skipPast("<")) {
331-
parse(x, jo, null);
358+
parse(x, jo, null, keepStrings);
332359
}
333360
return jo;
334361
}
335-
336362
/**
337363
* Convert a JSONObject into a well-formed, element-normal XML string.
338364
*
339365
* @param object
340366
* A JSONObject.
341367
* @return A string.
342-
* @throws JSONException
368+
* @throws JSONException Thrown if there is an error parsing the string
343369
*/
344370
public static String toString(Object object) throws JSONException {
345371
return toString(object, null);
@@ -353,7 +379,7 @@ public static String toString(Object object) throws JSONException {
353379
* @param tagName
354380
* The optional name of the enclosing tag.
355381
* @return A string.
356-
* @throws JSONException
382+
* @throws JSONException Thrown if there is an error parsing the string
357383
*/
358384
public static String toString(Object object, String tagName)
359385
throws JSONException {

0 commit comments

Comments
 (0)