+ public static JsonArray removeNull(JsonArray array) {
+ JsonArrayBuilder builder = Json.createArrayBuilder();
+ int i = 0;
+ for (Iterator<JsonValue> it = array.iterator(); it.hasNext(); ++i) {
+ JsonValue value = it.next();
+ switch (value.getValueType()) {
+ case ARRAY:
+ JsonArray a = removeNull(array.getJsonArray(i));
+ if (!a.isEmpty())
+ builder.add(a);
+ break;
+ case OBJECT:
+ JsonObject object = removeNull(array.getJsonObject(i));
+ if (!object.isEmpty())
+ builder.add(object);
+ break;
+ case STRING:
+ String s = array.getString(i);
+ if (s != null && !s.isEmpty())
+ builder.add(s);
+ break;
+ case NUMBER:
+ builder.add(array.getJsonNumber(i));
+ break;
+ case TRUE:
+ case FALSE:
+ builder.add(array.getBoolean(i));
+ break;
+ case NULL:
+ break;
+ }
+ }
+ return builder.build();
+ }
+
+ public static JsonObject removeNull(JsonObject obj) {
+ JsonObjectBuilder builder = Json.createObjectBuilder();
+ for (Iterator<Entry<String, JsonValue>> it = obj.entrySet().iterator(); it.hasNext();) {
+ Entry<String, JsonValue> e = it.next();
+ String key = e.getKey();
+ JsonValue value = e.getValue();
+ switch (value.getValueType()) {
+ case ARRAY:
+ JsonArray array = removeNull(obj.getJsonArray(key));
+ if (!array.isEmpty())
+ builder.add(key, array);
+ break;
+ case OBJECT:
+ JsonObject object = removeNull(obj.getJsonObject(key));
+ if (!object.isEmpty())
+ builder.add(key, object);
+ break;
+ case STRING:
+ String s = obj.getString(key);
+ if (s != null && !s.isEmpty())
+ builder.add(key, s);
+ break;
+ case NUMBER:
+ builder.add(key, obj.getJsonNumber(key));
+ break;
+ case TRUE:
+ case FALSE:
+ builder.add(key, obj.getBoolean(key));
+ break;
+ case NULL:
+ break;
+ }
+ }
+ return builder.build();
+ }
+