Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
replace with JSONPointer with one simple test case passed.
  • Loading branch information
SiyaoIsHiding committed Jan 26, 2023
commit afe301a508c0cb4f9477376180f1b3ea50897235
77 changes: 50 additions & 27 deletions src/main/java/org/json/XML.java
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ private static boolean parse(XMLTokener x, JSONObject context, String name, XMLP

// Jane

public static boolean parseWithPath(XMLTokener x, JSONObject context, String name, XMLParserConfiguration config, List<String> currPath, List<String> targetPath, JSONObject to)
public static boolean parseWithPath(XMLTokener x, JSONObject context, String name, XMLParserConfiguration config, List<String> currPath, List<String> targetPath, JSONObject toReplace)
throws JSONException {
char c;
int i;
Expand Down Expand Up @@ -478,7 +478,11 @@ public static boolean parseWithPath(XMLTokener x, JSONObject context, String nam
string = x.nextCDATA();
if (string.length() > 0) {
currPath.add(config.getcDataTagName());
context.accumulate(config.getcDataTagName(), string);
if (isTwoPathSame(currPath,targetPath)){
jsonObject.put(string, toReplace);
}else{
context.accumulate(config.getcDataTagName(), string);
}
currPath.remove(currPath.size()-1);
}
return false;
Expand Down Expand Up @@ -526,11 +530,18 @@ public static boolean parseWithPath(XMLTokener x, JSONObject context, String nam

} else {
tagName = (String) token;

token = null;
jsonObject = new JSONObject();
boolean nilAttributeFound = false;
xmlXsiTypeConverter = null;
currPath.add(tagName);
if (isTwoPathSame(currPath, targetPath)){
context.put(tagName, toReplace);
x.skipPast("</" + tagName + ">");
currPath.remove(currPath.size()-1);
return false;
}

for (;;) {
if (token == null) {
token = x.nextToken();
Expand All @@ -539,8 +550,9 @@ public static boolean parseWithPath(XMLTokener x, JSONObject context, String nam
if (token instanceof String) {
string = (String) token;
token = x.nextToken();
currPath.add(tagName);

if (token == EQ) {

token = x.nextToken();
if (!(token instanceof String)) {
throw x.syntaxError("Missing value");
Expand All @@ -554,24 +566,29 @@ public static boolean parseWithPath(XMLTokener x, JSONObject context, String nam
&& TYPE_ATTR.equals(string)) {
xmlXsiTypeConverter = config.getXsiTypeMap().get(token);
} else if (!nilAttributeFound) {
currPath.add(string);
if (isTwoPathSame(currPath,targetPath)){
System.out.println("Target found");
jsonObject.put(string, toReplace);
}else{
jsonObject.accumulate(string,
config.isKeepStrings()
? ((String) token)
: stringToValue((String) token));
}
currPath.add(string);
jsonObject.accumulate(string,
config.isKeepStrings()
? ((String) token)
: stringToValue((String) token));
currPath.remove(currPath.size()-1);
}
token = null;
} else {
currPath.add(string);
jsonObject.accumulate(string, "");
if (isTwoPathSame(currPath,targetPath)){
jsonObject.accumulate(string, toReplace);
}else{
jsonObject.accumulate(string, "");
}
currPath.remove(currPath.size()-1);
}

currPath.remove(currPath.size()-1);
// currPath.remove(currPath.size()-1);

} else if (token == SLASH) {
// Empty tag <.../>
Expand All @@ -580,24 +597,31 @@ public static boolean parseWithPath(XMLTokener x, JSONObject context, String nam
}
if (config.getForceList().contains(tagName)) {
// Force the value to be an array
if (nilAttributeFound) {
// currPath.add(tagName);
if (isTwoPathSame(currPath,targetPath)){
context.put(tagName, toReplace);
}else if (nilAttributeFound) {
context.append(tagName, JSONObject.NULL);
} else if (jsonObject.length() > 0) {
context.append(tagName, jsonObject);
} else {
context.put(tagName, new JSONArray());
}
// currPath.remove(currPath.size()-1);
} else {
currPath.add(tagName);
if (nilAttributeFound) {
// currPath.add(tagName);
if (isTwoPathSame(currPath,targetPath)){
context.put(tagName, toReplace);
}else if (nilAttributeFound) {
context.accumulate(tagName, JSONObject.NULL);
} else if (jsonObject.length() > 0) {
context.accumulate(tagName, jsonObject);
} else {
context.accumulate(tagName, "");
}
currPath.remove(currPath.size()-1);
// currPath.remove(currPath.size()-1);
}
currPath.remove(currPath.size()-1);
return false;

} else if (token == GT) {
Expand All @@ -612,21 +636,27 @@ public static boolean parseWithPath(XMLTokener x, JSONObject context, String nam
} else if (token instanceof String) {
string = (String) token;
if (string.length() > 0) {
currPath.add(config.getcDataTagName());
// currPath.add(tagName);
// should not replace here
// if(isTwoPathSame(currPath, targetPath)){
// context.put(tagName, toReplace);
// x.skipPast("</"+tagName+">");
// return false;
// }
if(xmlXsiTypeConverter != null) {
jsonObject.accumulate(config.getcDataTagName(),
stringToValue(string, xmlXsiTypeConverter));
} else {
jsonObject.accumulate(config.getcDataTagName(),
config.isKeepStrings() ? string : stringToValue(string));
}
currPath.remove(currPath.size()-1);
// currPath.remove(currPath.size()-1);
}

} else if (token == LT) {
// Nested element
currPath.add(tagName);
if (parseWithPath(x, jsonObject, tagName, config, currPath, targetPath, to)) {
// currPath.add(tagName);
if (parseWithPath(x, jsonObject, tagName, config, currPath, targetPath, toReplace)) {
if (config.getForceList().contains(tagName)) {
// Force the value to be an array

Expand All @@ -638,10 +668,7 @@ public static boolean parseWithPath(XMLTokener x, JSONObject context, String nam
} else {
context.append(tagName, jsonObject);
}
System.out.println(currPath.toString() + jsonObject.toString());
// currPath.remove(currPath.size()-1);
} else {
// currPath.add(tagName);
if (jsonObject.length() == 0) {
context.accumulate(tagName, "");
} else if (jsonObject.length() == 1
Expand All @@ -650,13 +677,9 @@ public static boolean parseWithPath(XMLTokener x, JSONObject context, String nam
} else {
context.accumulate(tagName, jsonObject);
}
System.out.println(currPath.toString() + jsonObject.toString());
// currPath.remove(currPath.size()-1);
}
currPath.remove(currPath.size()-1);
return false;
}else{
currPath.remove(currPath.size()-1);
}

}
Expand Down
6 changes: 3 additions & 3 deletions src/test/java/org/json/junit/XMLTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -303,18 +303,18 @@ public void testParseWithPath(){

String expectedStr =
"{\"addresses\":{\"address\":{\"street\":\"[CDATA[Baker street 5]\","+
"\"name\":\"Joe Tester\",\"NothingHere\":\"\",TrueValue:true,\n"+
"\"name\":\"Joe Tester\",\"NothingHere\":\"\",TrueValue:{},\n"+
"\"FalseValue\":false,\"NullValue\":null,\"PositiveValue\":42,\n"+
"\"NegativeValue\":-23,\"DoubleValue\":-23.45,\"Nan\":-23x.45,\n"+
"\"ArrayOfNum\":\"1, 2, 3, 4.1, 5.2\"\n"+
"},\"xsi:noNamespaceSchemaLocation\":"+
"\"test.xsd\",\"xmlns:xsi\":\"http://www.w3.org/2001/"+
"XMLSchema-instance\"}}";

JSONObject expectedJsonObject = new JSONObject(expectedStr);
Reader reader = new StringReader(xmlStr);
JSONPointer pointer = new JSONPointer("/addresses/address/TrueValue");
JSONObject jsonObject = XML.toJSONObject(reader, pointer, new JSONObject());
JSONObject actualJsonObject = XML.toJSONObject(reader, pointer, new JSONObject());
Util.compareActualVsExpectedJsonObjects(actualJsonObject,expectedJsonObject);
}


Expand Down