Skip to content

Commit

Permalink
Added a few methods
Browse files Browse the repository at this point in the history
Signed-off-by: Ralph Niemitz <[email protected]>
  • Loading branch information
RalleYTN committed Dec 20, 2017
1 parent 79df5eb commit 6fe8cf6
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 3 deletions.
40 changes: 39 additions & 1 deletion src/de/ralleytn/simple/json/JSONArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@
* Represents a JSON array.
* @author FangYidong([email protected])
* @author Ralph Niemitz/RalleYTN([email protected])
* @version 1.0.0
* @version 1.1.0
* @since 1.0.0
*/
public class JSONArray extends ArrayList<Object> implements JSONAware, JSONStreamAware {
Expand Down Expand Up @@ -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("<item>");

if(element != null) {

builder.append(String.valueOf(element));
}

builder.append("</item>");
}
}

builder.append("</");
builder.append(rootName);
builder.append('>');

return builder.toString();
}
}
2 changes: 1 addition & 1 deletion src/de/ralleytn/simple/json/JSONFormatter.java
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@
* @version 1.0.0
* @since 1.0.0
*/
public final class JSONFormatter {
public class JSONFormatter {

private JSONFormatter() {}

Expand Down
62 changes: 61 additions & 1 deletion src/de/ralleytn/simple/json/JSONObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@
* Represents a JSON object.
* @author FangYidong([email protected])
* @author Ralph Niemitz/RalleYTN([email protected])
* @version 1.0.0
* @version 1.1.0
* @since 1.0.0
*/
public class JSONObject extends LinkedHashMap<Object, Object> implements JSONAware, JSONStreamAware {
Expand Down Expand Up @@ -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() {

Expand Down Expand Up @@ -519,4 +538,45 @@ public <T extends Enum>T getEnum(String key, Class<T> 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(key);
builder.append('>');
}
});

builder.append("</");
builder.append(rootName);
builder.append('>');

return builder.toString();
}
}

0 comments on commit 6fe8cf6

Please sign in to comment.