1
0
Fork 0
mirror of https://github.com/ethauvin/JSON-java.git synced 2025-06-17 07:50:52 -07:00

Merge pull request #160 from johnjaylward/FixXMLNPE

Fixes possible NullPointerException in XML.toString(object, tagName)
This commit is contained in:
Sean Leary 2015-10-18 10:21:36 -05:00
commit 09b6af4712

View file

@ -1,7 +1,7 @@
package org.json; 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 Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal of this software and associated documentation files (the "Software"), to deal
@ -30,7 +30,7 @@ import java.util.Iterator;
* This provides static methods to convert an XML text into a JSONObject, * This provides static methods to convert an XML text into a JSONObject,
* and to covert a JSONObject into an XML text. * and to covert a JSONObject into an XML text.
* @author JSON.org * @author JSON.org
* @version 2014-05-03 * @version 2015-10-14
*/ */
public class XML { public class XML {
@ -468,10 +468,12 @@ public class XML {
// XML does not have good support for arrays. If an array appears in a place // XML does not have good support for arrays. If an array appears in a place
// where XML is lacking, synthesize an <array> element. // where XML is lacking, synthesize an <array> element.
} else { }
if(object!=null){
if (object.getClass().isArray()) { if (object.getClass().isArray()) {
object = new JSONArray(object); object = new JSONArray(object);
} }
if (object instanceof JSONArray) { if (object instanceof JSONArray) {
ja = (JSONArray)object; ja = (JSONArray)object;
length = ja.length(); length = ja.length();
@ -479,12 +481,12 @@ public class XML {
sb.append(toString(ja.opt(i), tagName == null ? "array" : tagName)); sb.append(toString(ja.opt(i), tagName == null ? "array" : tagName));
} }
return sb.toString(); return sb.toString();
} else { }
}
string = (object == null) ? "null" : escape(object.toString()); string = (object == null) ? "null" : escape(object.toString());
return (tagName == null) ? "\"" + string + "\"" : return (tagName == null) ? "\"" + string + "\"" :
(string.length() == 0) ? "<" + tagName + "/>" : (string.length() == 0) ? "<" + tagName + "/>" :
"<" + tagName + ">" + string + "</" + tagName + ">"; "<" + tagName + ">" + string + "</" + tagName + ">";
}
}
} }
} }