| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 9370437 commit 09d37e5
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -36,24 +36,6 @@ of this software and associated documentation files (the "Software"), to deal | |||
| 36 | 36 | * @version 2016-01-30 | |
| 37 | 37 | */ | |
| 38 | 38 | 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 | - | ||
| 57 | 39 | /** | |
| 58 | 40 | * Parse XML values and store them in a JSONArray. | |
| 59 | 41 | * @param x The XMLTokener containing the source string. | |
@@ -212,9 +194,8 @@ private static Object parse( | |||
| 212 | 194 | if (ja == null) { | |
| 213 | 195 | if (arrayForm) { | |
| 214 | 196 | return newja; | |
| 215 | - } else { | ||
| 216 | - return newjo; | ||
| 217 | 197 | } | |
| 198 | + return newjo; | ||
| 218 | 199 | } | |
| 219 | 200 | ||
| 220 | 201 | // Content, between <...> and </...> | |
@@ -236,9 +217,8 @@ private static Object parse( | |||
| 236 | 217 | if (ja == null) { | |
| 237 | 218 | if (arrayForm) { | |
| 238 | 219 | return newja; | |
| 239 | - } else { | ||
| 240 | - return newjo; | ||
| 241 | 220 | } | |
| 221 | + return newjo; | ||
| 242 | 222 | } | |
| 243 | 223 | } | |
| 244 | 224 | } | |
@@ -264,10 +244,10 @@ private static Object parse( | |||
| 264 | 244 | * Comments, prologs, DTDs, and <code><[ [ ]]></code> are ignored. | |
| 265 | 245 | * @param string The source string. | |
| 266 | 246 | * @return A JSONArray containing the structured data from the XML string. | |
| 267 | - * @throws JSONException | ||
| 247 | + * @throws JSONException Thrown on error converting to a JSONArray | ||
| 268 | 248 | */ | |
| 269 | 249 | public static JSONArray toJSONArray(String string) throws JSONException { | |
| 270 | - return toJSONArray(new XMLTokener(string)); | ||
| 250 | + return (JSONArray)parse(new XMLTokener(string), true, null, false); | ||
| 271 | 251 | } | |
| 272 | 252 | ||
| 273 | 253 | ||
@@ -283,11 +263,13 @@ public static JSONArray toJSONArray(String string) throws JSONException { | |||
| 283 | 263 | * but just leaves it as a string. | |
| 284 | 264 | * Comments, prologs, DTDs, and <code><[ [ ]]></code> are ignored. | |
| 285 | 265 | * @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 | ||
| 286 | 268 | * @return A JSONArray containing the structured data from the XML string. | |
| 287 | - * @throws JSONException | ||
| 269 | + * @throws JSONException Thrown on error converting to a JSONArray | ||
| 288 | 270 | */ | |
| 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); | ||
| 291 | 273 | } | |
| 292 | 274 | ||
| 293 | 275 | ||
@@ -303,11 +285,13 @@ public static JSONArray toJsonML(String string) throws JSONException { | |||
| 303 | 285 | * but just leaves it as a string. | |
| 304 | 286 | * Comments, prologs, DTDs, and <code><[ [ ]]></code> are ignored. | |
| 305 | 287 | * @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 | ||
| 306 | 290 | * @return A JSONArray containing the structured data from the XML string. | |
| 307 | - * @throws JSONException | ||
| 291 | + * @throws JSONException Thrown on error converting to a JSONArray | ||
| 308 | 292 | */ | |
| 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); | ||
| 311 | 295 | } | |
| 312 | 296 | ||
| 313 | 297 | ||
@@ -321,13 +305,51 @@ public static JSONArray toJsonML(XMLTokener x) throws JSONException { | |||
| 321 | 305 | * Comments, prologs, DTDs, and <code><[ [ ]]></code> are ignored. | |
| 322 | 306 | * @param x An XMLTokener. | |
| 323 | 307 | * @return A JSONArray containing the structured data from the XML string. | |
| 324 | - * @throws JSONException | ||
| 308 | + * @throws JSONException Thrown on error converting to a JSONArray | ||
| 325 | 309 | */ | |
| 326 | 310 | public static JSONArray toJSONArray(XMLTokener x) throws JSONException { | |
| 327 | - return (JSONArray)parse(x, true, null); | ||
| 311 | + return (JSONArray)parse(x, true, null, false); | ||
| 328 | 312 | } | |
| 329 | 313 | ||
| 330 | 314 | ||
| 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 | + | ||
| 331 | 353 | /** | |
| 332 | 354 | * Convert a well-formed (but not necessarily valid) XML string into a | |
| 333 | 355 | * JSONObject using the JsonML transform. Each XML tag is represented as | |
@@ -339,10 +361,10 @@ public static JSONArray toJSONArray(XMLTokener x) throws JSONException { | |||
| 339 | 361 | * Comments, prologs, DTDs, and <code><[ [ ]]></code> are ignored. | |
| 340 | 362 | * @param x An XMLTokener of the XML source text. | |
| 341 | 363 | * @return A JSONObject containing the structured data from the XML string. | |
| 342 | - * @throws JSONException | ||
| 364 | + * @throws JSONException Thrown on error converting to a JSONObject | ||
| 343 | 365 | */ | |
| 344 | 366 | public static JSONObject toJSONObject(XMLTokener x) throws JSONException { | |
| 345 | - return (JSONObject)parse(x, false, null); | ||
| 367 | + return (JSONObject)parse(x, false, null, false); | ||
| 346 | 368 | } | |
| 347 | 369 | ||
| 348 | 370 | ||
@@ -355,20 +377,22 @@ public static JSONObject toJSONObject(XMLTokener x) throws JSONException { | |||
| 355 | 377 | * will be an array of strings and JsonML JSONObjects. | |
| 356 | 378 | ||
| 357 | 379 | * 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 | ||
| 359 | 383 | * @return A JSONObject containing the structured data from the XML string. | |
| 360 | - * @throws JSONException | ||
| 384 | + * @throws JSONException Thrown on error converting to a JSONObject | ||
| 361 | 385 | */ | |
| 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); | ||
| 364 | 388 | } | |
| 365 | 389 | ||
| 366 | 390 | ||
| 367 | 391 | /** | |
| 368 | 392 | * Reverse the JSONML transformation, making an XML text from a JSONArray. | |
| 369 | 393 | * @param ja A JSONArray. | |
| 370 | 394 | * @return An XML string. | |
| 371 | - * @throws JSONException | ||
| 395 | + * @throws JSONException Thrown on error converting to a string | ||
| 372 | 396 | */ | |
| 373 | 397 | public static String toString(JSONArray ja) throws JSONException { | |
| 374 | 398 | int i; | |
@@ -452,7 +476,7 @@ public static String toString(JSONArray ja) throws JSONException { | |||
| 452 | 476 | * The other properties are attributes with string values. | |
| 453 | 477 | * @param jo A JSONObject. | |
| 454 | 478 | * @return An XML string. | |
| 455 | - * @throws JSONException | ||
| 479 | + * @throws JSONException Thrown on error converting to a string | ||
| 456 | 480 | */ | |
| 457 | 481 | public static String toString(JSONObject jo) throws JSONException { | |
| 458 | 482 | StringBuilder sb = new StringBuilder(); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -110,7 +110,7 @@ public static String escape(String string) { | |||
| 110 | 110 | * | |
| 111 | 111 | * @param string | |
| 112 | 112 | * A string. | |
| 113 | - * @throws JSONException | ||
| 113 | + * @throws JSONException Thrown if the string contains whitespace or is empty. | ||
| 114 | 114 | */ | |
| 115 | 115 | public static void noSpace(String string) throws JSONException { | |
| 116 | 116 | int i, length = string.length(); | |
@@ -137,7 +137,7 @@ public static void noSpace(String string) throws JSONException { | |||
| 137 | 137 | * @return true if the close tag is processed. | |
| 138 | 138 | * @throws JSONException | |
| 139 | 139 | */ | |
| 140 | - private static boolean parse(XMLTokener x, JSONObject context, String name) | ||
| 140 | + private static boolean parse(XMLTokener x, JSONObject context, String name, boolean keepStrings) | ||
| 141 | 141 | throws JSONException { | |
| 142 | 142 | char c; | |
| 143 | 143 | int i; | |
@@ -238,7 +238,7 @@ private static boolean parse(XMLTokener x, JSONObject context, String name) | |||
| 238 | 238 | throw x.syntaxError("Missing value"); | |
| 239 | 239 | } | |
| 240 | 240 | jsonobject.accumulate(string, | |
| 241 | - JSONObject.stringToValue((String) token)); | ||
| 241 | + keepStrings ? token : JSONObject.stringToValue((String) token)); | ||
| 242 | 242 | token = null; | |
| 243 | 243 | } else { | |
| 244 | 244 | jsonobject.accumulate(string, ""); | |
@@ -270,12 +270,12 @@ private static boolean parse(XMLTokener x, JSONObject context, String name) | |||
| 270 | 270 | string = (String) token; | |
| 271 | 271 | if (string.length() > 0) { | |
| 272 | 272 | jsonobject.accumulate("content", | |
| 273 | - JSONObject.stringToValue(string)); | ||
| 273 | + keepStrings ? token : JSONObject.stringToValue(string)); | ||
| 274 | 274 | } | |
| 275 | 275 | ||
| 276 | 276 | } else if (token == LT) { | |
| 277 | 277 | // Nested element | |
| 278 | - if (parse(x, jsonobject, tagName)) { | ||
| 278 | + if (parse(x, jsonobject, tagName,keepStrings)) { | ||
| 279 | 279 | if (jsonobject.length() == 0) { | |
| 280 | 280 | context.accumulate(tagName, ""); | |
| 281 | 281 | } else if (jsonobject.length() == 1 | |
@@ -301,9 +301,10 @@ private static boolean parse(XMLTokener x, JSONObject context, String name) | |||
| 301 | 301 | * {@link JSONObject.stringToValue(String)} method. Use it instead. | |
| 302 | 302 | * | |
| 303 | 303 | * @deprecated Use {@link JSONObject#stringToValue(String)} instead. | |
| 304 | - * @param string | ||
| 304 | + * @param string String to convert | ||
| 305 | 305 | * @return JSON value of this string or the string | |
| 306 | 306 | */ | |
| 307 | + @Deprecated | ||
| 307 | 308 | public static Object stringToValue(String string) { | |
| 308 | 309 | return JSONObject.stringToValue(string); | |
| 309 | 310 | } | |
@@ -322,24 +323,49 @@ public static Object stringToValue(String string) { | |||
| 322 | 323 | * @param string | |
| 323 | 324 | * The source string. | |
| 324 | 325 | * @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 | ||
| 326 | 327 | */ | |
| 327 | 328 | 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><[ [ ]]></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 { | ||
| 328 | 355 | JSONObject jo = new JSONObject(); | |
| 329 | 356 | XMLTokener x = new XMLTokener(string); | |
| 330 | 357 | while (x.more() && x.skipPast("<")) { | |
| 331 | - parse(x, jo, null); | ||
| 358 | + parse(x, jo, null, keepStrings); | ||
| 332 | 359 | } | |
| 333 | 360 | return jo; | |
| 334 | 361 | } | |
| 335 | - | ||
| 336 | 362 | /** | |
| 337 | 363 | * Convert a JSONObject into a well-formed, element-normal XML string. | |
| 338 | 364 | * | |
| 339 | 365 | * @param object | |
| 340 | 366 | * A JSONObject. | |
| 341 | 367 | * @return A string. | |
| 342 | - * @throws JSONException | ||
| 368 | + * @throws JSONException Thrown if there is an error parsing the string | ||
| 343 | 369 | */ | |
| 344 | 370 | public static String toString(Object object) throws JSONException { | |
| 345 | 371 | return toString(object, null); | |
@@ -353,7 +379,7 @@ public static String toString(Object object) throws JSONException { | |||
| 353 | 379 | * @param tagName | |
| 354 | 380 | * The optional name of the enclosing tag. | |
| 355 | 381 | * @return A string. | |
| 356 | - * @throws JSONException | ||
| 382 | + * @throws JSONException Thrown if there is an error parsing the string | ||
| 357 | 383 | */ | |
| 358 | 384 | public static String toString(Object object, String tagName) | |
| 359 | 385 | throws JSONException { | |
| Back | FazBrowse Home | New Git URL |
0 commit comments