Skip to content

Commit ee958fa

Browse files
committed
XML attributes put in place with @attr and #content,testcases to be fixed
1 parent f196975 commit ee958fa

File tree

5 files changed

+114
-72
lines changed

5 files changed

+114
-72
lines changed
Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
1-
#Wed Nov 28 11:21:03 SGT 2012
2-
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
31
eclipse.preferences.version=1
4-
org.eclipse.jdt.core.compiler.source=1.6
2+
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
3+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
4+
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
55
org.eclipse.jdt.core.compiler.compliance=1.6
6+
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
7+
org.eclipse.jdt.core.compiler.debug.localVariable=generate
8+
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
9+
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
10+
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
11+
org.eclipse.jdt.core.compiler.source=1.6

src/main/java/org/json/XML.java

Lines changed: 71 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ of this software and associated documentation files (the "Software"), to deal
3131
* This provides static methods to convert an XML text into a JSONObject,
3232
* and to covert a JSONObject into an XML text.
3333
* @author JSON.org
34-
* @version 2011-02-11
34+
* @version 2012-10-26
3535
*/
3636
public class XML {
3737

38-
/** The Character '&'. */
38+
/** The Character '&'. */
3939
public static final Character AMP = new Character('&');
4040

4141
/** The Character '''. */
@@ -50,7 +50,7 @@ public class XML {
5050
/** The Character '>'. */
5151
public static final Character GT = new Character('>');
5252

53-
/** The Character '<'. */
53+
/** The Character '&lt;'. */
5454
public static final Character LT = new Character('<');
5555

5656
/** The Character '?'. */
@@ -164,7 +164,7 @@ private static boolean parse(XMLTokener x, JSONObject context,
164164
if (x.next() == '[') {
165165
string = x.nextCDATA();
166166
if (string.length() > 0) {
167-
context.accumulate("content", string);
167+
context.accumulate("#content", string);
168168
}
169169
return false;
170170
}
@@ -229,11 +229,11 @@ private static boolean parse(XMLTokener x, JSONObject context,
229229
if (!(token instanceof String)) {
230230
throw x.syntaxError("Missing value");
231231
}
232-
jsonobject.accumulate(string,
232+
jsonobject.accumulate('@'+string,
233233
XML.stringToValue((String)token));
234234
token = null;
235235
} else {
236-
jsonobject.accumulate(string, "");
236+
jsonobject.accumulate('@'+string, "");
237237
}
238238

239239
// Empty tag <.../>
@@ -262,7 +262,7 @@ private static boolean parse(XMLTokener x, JSONObject context,
262262
} else if (token instanceof String) {
263263
string = (String)token;
264264
if (string.length() > 0) {
265-
jsonobject.accumulate("content",
265+
jsonobject.accumulate("#content",
266266
XML.stringToValue(string));
267267
}
268268

@@ -273,9 +273,9 @@ private static boolean parse(XMLTokener x, JSONObject context,
273273
if (jsonobject.length() == 0) {
274274
context.accumulate(tagName, "");
275275
} else if (jsonobject.length() == 1 &&
276-
jsonobject.opt("content") != null) {
276+
jsonobject.opt("#content") != null) {
277277
context.accumulate(tagName,
278-
jsonobject.opt("content"));
278+
jsonobject.opt("#content"));
279279
} else {
280280
context.accumulate(tagName, jsonobject);
281281
}
@@ -313,37 +313,37 @@ public static Object stringToValue(String string) {
313313
if ("null".equalsIgnoreCase(string)) {
314314
return JSONObject.NULL;
315315
}
316-
if ("0".equals(string)) {
317-
return new Integer(0);
318-
}
319-
320-
// If it might be a number, try converting it. If that doesn't work,
321-
// return the string.
322-
323-
try {
324-
char initial = string.charAt(0);
325-
boolean negative = false;
326-
if (initial == '-') {
327-
initial = string.charAt(1);
328-
negative = true;
329-
}
330-
if (initial == '0' && string.charAt(negative ? 2 : 1) == '0') {
331-
return string;
332-
}
333-
if ((initial >= '0' && initial <= '9')) {
334-
if (string.indexOf('.') >= 0) {
335-
return Double.valueOf(string);
336-
} else if (string.indexOf('e') < 0 && string.indexOf('E') < 0) {
337-
Long myLong = new Long(string);
338-
if (myLong.longValue() == myLong.intValue()) {
339-
return new Integer(myLong.intValue());
340-
} else {
341-
return myLong;
342-
}
343-
}
344-
}
345-
} catch (Exception ignore) {
346-
}
316+
// if ("0".equals(string)) {
317+
// return new Integer(0);
318+
// }
319+
//
320+
//// If it might be a number, try converting it. If that doesn't work,
321+
//// return the string.
322+
//
323+
// try {
324+
// char initial = string.charAt(0);
325+
// boolean negative = false;
326+
// if (initial == '-') {
327+
// initial = string.charAt(1);
328+
// negative = true;
329+
// }
330+
// if (initial == '0' && string.charAt(negative ? 2 : 1) == '0') {
331+
// return string;
332+
// }
333+
// if ((initial >= '0' && initial <= '9')) {
334+
// if (string.indexOf('.') >= 0) {
335+
// return Double.valueOf(string);
336+
// } else if (string.indexOf('e') < 0 && string.indexOf('E') < 0) {
337+
// Long myLong = new Long(string);
338+
// if (myLong.longValue() == myLong.intValue()) {
339+
// return new Integer(myLong.intValue());
340+
// } else {
341+
// return myLong;
342+
// }
343+
// }
344+
// }
345+
// } catch (Exception ignore) {
346+
// }
347347
return string;
348348
}
349349

@@ -401,14 +401,16 @@ public static String toString(Object object, String tagName)
401401
int length;
402402
String string;
403403
Object value;
404+
boolean isClosedTag = false;
405+
404406
if (object instanceof JSONObject) {
405407

406408
// Emit <tagName>
407409

408410
if (tagName != null) {
409411
sb.append('<');
410412
sb.append(tagName);
411-
sb.append('>');
413+
// sb.append('>');
412414
}
413415

414416
// Loop thru the keys.
@@ -427,9 +429,18 @@ public static String toString(Object object, String tagName)
427429
string = null;
428430
}
429431

432+
//Emit @attributes samarjit
433+
if (key.charAt(0) == '@') {
434+
sb.append(" "+key.substring(1));
435+
sb.append("=\"");
436+
sb.append(escape(value.toString()));
437+
sb.append("\"");
438+
}else
430439
// Emit content in body
431440

432-
if ("content".equals(key)) {
441+
if ("#content".equals(key)) {
442+
isClosedTag = true;
443+
sb.append(">");
433444
if (value instanceof JSONArray) {
434445
ja = (JSONArray)value;
435446
length = ja.length();
@@ -446,6 +457,11 @@ public static String toString(Object object, String tagName)
446457
// Emit an array of similar keys
447458

448459
} else if (value instanceof JSONArray) {
460+
if(!isClosedTag && tagName != null){
461+
sb.append(">");
462+
isClosedTag = true;
463+
}
464+
449465
ja = (JSONArray)value;
450466
length = ja.length();
451467
for (i = 0; i < length; i += 1) {
@@ -463,19 +479,31 @@ public static String toString(Object object, String tagName)
463479
}
464480
}
465481
} else if ("".equals(value)) {
482+
if(!isClosedTag ){
483+
sb.append(">");
484+
isClosedTag = true;
485+
}
466486
sb.append('<');
467487
sb.append(key);
468488
sb.append("/>");
469489

470490
// Emit a new tag <k>
471491

472492
} else {
493+
if(!isClosedTag && tagName != null){
494+
sb.append(">");
495+
isClosedTag = true;
496+
}
473497
sb.append(toString(value, key));
474498
}
475499
}
476500
if (tagName != null) {
477501

478502
// Emit the </tagname> close tag
503+
if(!isClosedTag ){
504+
sb.append(">");
505+
isClosedTag = true;
506+
}
479507

480508
sb.append("</");
481509
sb.append(tagName);
@@ -505,4 +533,4 @@ public static String toString(Object object, String tagName)
505533
}
506534
}
507535
}
508-
}
536+
}

src/test/java/org/json/tests/SampleResourceBundle_en_US.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,6 @@ public Object[][] getContents() {
2424
{ "ASCII", "American Standard Code for Information Interchange" },
2525
{ "JAVA.desc", "Just Another Vague Acronym" },
2626
{ "JAVA.data", "Sweet language" },
27-
{ "JSON", "JavaScript Object Notation" },
27+
{ "JSON", "JavaScript Object Notation" }
2828
};
2929
}

src/test/java/org/json/tests/TestJSONObject.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -735,6 +735,7 @@ public void testConstructor_SubClass()
735735
//"{\"l\":5748548957230984584,\"m\":true,\"big\":false,\"j\":\"3\",\"k\":10.03,\"ZERO\":0,\"i\":3}",
736736
"{\"i\":3,\"j\":\"3\",\"k\":10.03,\"l\":5748548957230984584,\"m\":true,\"ZERO\":0,\"big\":false}",
737737
jsonobject.toString());
738+
738739
}
739740

740741
/**

0 commit comments

Comments
 (0)