-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Ralph Niemitz <[email protected]>
- Loading branch information
Showing
3 changed files
with
101 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 { | ||
|
@@ -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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 { | ||
|
@@ -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 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(); | ||
} | ||
} |