diff --git a/src/de/ralleytn/simple/json/JSONArray.java b/src/de/ralleytn/simple/json/JSONArray.java index eb61a75..e78fee3 100644 --- a/src/de/ralleytn/simple/json/JSONArray.java +++ b/src/de/ralleytn/simple/json/JSONArray.java @@ -220,7 +220,7 @@ * Represents a JSON array. * @author FangYidong(fangyidong@yahoo.com.cn) * @author Ralph Niemitz/RalleYTN(ralph.niemitz@gmx.de) - * @version 1.0.0 + * @version 1.1.0 * @since 1.0.0 */ public class JSONArray extends ArrayList implements JSONAware, JSONStreamAware { @@ -1309,4 +1309,42 @@ public JSONArray[] toArrayArray() { return array; } + + /** + * @param rootName name of the root element + * @return this JSON array as XML + * @since 1.1.0 + */ + public String toXML(String rootName) { + + StringBuilder builder = new StringBuilder(); + builder.append('<'); + builder.append(rootName); + builder.append(" length="); + builder.append(this.size()); + builder.append('>'); + + for(Object element : this) { + + if(element instanceof JSONObject) {builder.append(((JSONObject)element).toXML("item")); + } else if(element instanceof JSONArray) {builder.append(((JSONArray)element).toXML("item")); + } else { + + builder.append(""); + + if(element != null) { + + builder.append(String.valueOf(element)); + } + + builder.append(""); + } + } + + builder.append("'); + + return builder.toString(); + } } diff --git a/src/de/ralleytn/simple/json/JSONFormatter.java b/src/de/ralleytn/simple/json/JSONFormatter.java index 8018d2a..15c6022 100644 --- a/src/de/ralleytn/simple/json/JSONFormatter.java +++ b/src/de/ralleytn/simple/json/JSONFormatter.java @@ -213,7 +213,7 @@ * @version 1.0.0 * @since 1.0.0 */ -public final class JSONFormatter { +public class JSONFormatter { private JSONFormatter() {} diff --git a/src/de/ralleytn/simple/json/JSONObject.java b/src/de/ralleytn/simple/json/JSONObject.java index e42bc8b..9dcb294 100644 --- a/src/de/ralleytn/simple/json/JSONObject.java +++ b/src/de/ralleytn/simple/json/JSONObject.java @@ -218,7 +218,7 @@ * Represents a JSON object. * @author FangYidong(fangyidong@yahoo.com.cn) * @author Ralph Niemitz/RalleYTN(ralph.niemitz@gmx.de) - * @version 1.0.0 + * @version 1.1.0 * @since 1.0.0 */ public class JSONObject extends LinkedHashMap implements JSONAware, JSONStreamAware { @@ -335,6 +335,25 @@ public static final String toJSONString(Map map) { } } + /** + * @return a new {@linkplain JSONObject} without any {@code null} values + * @since 1.1.0 + */ + public JSONObject compact() { + + JSONObject object = new JSONObject(); + + this.forEach((key, value) -> { + + if(value != null) { + + object.put(key, value); + } + }); + + return object; + } + @Override public String toJSONString() { @@ -519,4 +538,45 @@ public T getEnum(String key, Class type) { return JSONValue.getEnum(this.get(key), type); } + + /** + * @param rootName the name of the root element + * @return this JSON Object in XML + * @since 1.1.0 + */ + public String toXML(String rootName) { + + StringBuilder builder = new StringBuilder(); + + builder.append('<'); + builder.append(rootName); + builder.append('>'); + + this.forEach((key, value) -> { + + if(value instanceof JSONObject) {builder.append(((JSONObject)value).toXML(key.toString())); + } else if(value instanceof JSONArray) {builder.append(((JSONArray)value).toXML(key.toString())); + } else { + + builder.append('<'); + builder.append(key); + builder.append('>'); + + if(value != null) { + + builder.append(String.valueOf(value)); + } + + builder.append("'); + } + }); + + builder.append("'); + + return builder.toString(); + } }