Skip to content

Commit

Permalink
Merge pull request #160 from johnjaylward/FixXMLNPE
Browse files Browse the repository at this point in the history
Fixes possible NullPointerException in XML.toString(object, tagName)
  • Loading branch information
stleary committed Oct 18, 2015
2 parents b0191a6 + 637c1fe commit 09b6af4
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions XML.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.json;

/*
Copyright (c) 2002 JSON.org
Copyright (c) 2015 JSON.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -30,7 +30,7 @@ of this software and associated documentation files (the "Software"), to deal
* This provides static methods to convert an XML text into a JSONObject,
* and to covert a JSONObject into an XML text.
* @author JSON.org
* @version 2014-05-03
* @version 2015-10-14
*/
public class XML {

Expand Down Expand Up @@ -468,23 +468,25 @@ public static String toString(Object object, String tagName)
// XML does not have good support for arrays. If an array appears in a place
// where XML is lacking, synthesize an <array> element.

} else {
}
if(object!=null){
if (object.getClass().isArray()) {
object = new JSONArray(object);
}

if (object instanceof JSONArray) {
ja = (JSONArray)object;
length = ja.length();
for (i = 0; i < length; i += 1) {
sb.append(toString(ja.opt(i), tagName == null ? "array" : tagName));
}
return sb.toString();
} else {
string = (object == null) ? "null" : escape(object.toString());
return (tagName == null) ? "\"" + string + "\"" :
(string.length() == 0) ? "<" + tagName + "/>" :
"<" + tagName + ">" + string + "</" + tagName + ">";
}
}
string = (object == null) ? "null" : escape(object.toString());
return (tagName == null) ? "\"" + string + "\"" :
(string.length() == 0) ? "<" + tagName + "/>" :
"<" + tagName + ">" + string + "</" + tagName + ">";

}
}

0 comments on commit 09b6af4

Please sign in to comment.