Skip to content

Commit

Permalink
Added removeDeep function to JSONArray and JSONObject
Browse files Browse the repository at this point in the history
  • Loading branch information
LatvianModder committed May 2, 2024
1 parent 5bf6166 commit 4ccb604
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package dev.latvian.apps.webutils.json;

import org.jetbrains.annotations.Nullable;

@FunctionalInterface
public interface DeepRemovePredicate {
DeepRemovePredicate NONE = (key, index, value) -> false;

boolean remove(String key, int index, @Nullable Object value);
}
35 changes: 35 additions & 0 deletions src/main/java/dev/latvian/apps/webutils/json/JSONArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -185,4 +185,39 @@ public String toString() {
public String toPrettyString() {
return JSON.DEFAULT.writePretty(this);
}

public boolean removeDeep(DeepRemovePredicate predicate, boolean removeEmpty) {
boolean modified = false;
var it = iterator();
int index = 0;

while (it.hasNext()) {
var v = it.next();

if (predicate.remove("", index++, v)) {
modified = true;
it.remove();
} else if (v instanceof JSONObject o) {
if (o.removeDeep(predicate, removeEmpty)) {
modified = true;
}

if (removeEmpty && o.isEmpty()) {
modified = true;
it.remove();
}
} else if (v instanceof JSONArray a) {
if (a.removeDeep(predicate, removeEmpty)) {
modified = true;
}

if (removeEmpty && a.isEmpty()) {
modified = true;
it.remove();
}
}
}

return modified;
}
}
35 changes: 35 additions & 0 deletions src/main/java/dev/latvian/apps/webutils/json/JSONObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,39 @@ public String toString() {
public String toPrettyString() {
return JSON.DEFAULT.writePretty(this);
}

public boolean removeDeep(DeepRemovePredicate predicate, boolean removeEmpty) {
boolean modified = false;
var it = entrySet().iterator();

while (it.hasNext()) {
var e = it.next();
var v = e.getValue();

if (predicate.remove(e.getKey(), -1, v)) {
modified = true;
it.remove();
} else if (v instanceof JSONObject o) {
if (o.removeDeep(predicate, removeEmpty)) {
modified = true;
}

if (removeEmpty && o.isEmpty()) {
modified = true;
it.remove();
}
} else if (v instanceof JSONArray a) {
if (a.removeDeep(predicate, removeEmpty)) {
modified = true;
}

if (removeEmpty && a.isEmpty()) {
modified = true;
it.remove();
}
}
}

return modified;
}
}
2 changes: 2 additions & 0 deletions src/test/java/dev/latvian/apps/webutils/test/JSONTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public void readRemoteObject() throws Exception {
var content = "{\"hours\":672,\"total_launches\":7864423,\"hourly\":11700.028795489174,\"ml\":[{\"ml\":1,\"fraction\":0.9412095458242773,\"launches\":7402070},{\"ml\":2,\"fraction\":0.05879045417572275,\"launches\":462353}],\"mc\":[{\"mc\":2001,\"fraction\":0.6443314404629558,\"launches\":5067295},{\"mc\":1902,\"fraction\":0.3556624561013567,\"launches\":2797080},{\"mc\":2004,\"fraction\":6.103435687525964E-6,\"launches\":48}],\"mlmc\":[{\"ml\":1,\"mc\":2001,\"fraction\":0.6154869848684386,\"launches\":4840450},{\"ml\":1,\"mc\":1902,\"fraction\":0.3257164575201512,\"launches\":2561572},{\"ml\":2,\"mc\":1902,\"fraction\":0.029945998581205512,\"launches\":235508},{\"ml\":2,\"mc\":2001,\"fraction\":0.028844455594517232,\"launches\":226845},{\"ml\":1,\"mc\":2004,\"fraction\":6.103435687525964E-6,\"launches\":48}],\"launches\":[{\"version\":\"2001.6.4-build.114\",\"fraction\":0.26559062247796184,\"launches\":2088717},{\"version\":\"2001.6.4-build.120\",\"fraction\":0.21153312836809515,\"launches\":1663586},{\"version\":\"1902.6.2-build.45\",\"fraction\":0.1828411315108559,\"launches\":1437940},{\"version\":\"2001.6.4-build.127\",\"fraction\":0.0810527358459737,\"launches\":637433},{\"version\":\"1902.6.2-build.3\",\"fraction\":0.038601560470488426,\"launches\":303579},{\"version\":\"2001.6.4-build.95\",\"fraction\":0.03645264248883866,\"launches\":286679},{\"version\":\"1902.6.2-build.15\",\"fraction\":0.02442060911525232,\"launches\":192054},{\"version\":\"1902.6.2-build.50\",\"fraction\":0.018523800156731142,\"launches\":145679},{\"version\":\"2001.6.3-build.83\",\"fraction\":0.018382658206457105,\"launches\":144569},{\"version\":\"1902.6.2-build.27\",\"fraction\":0.01564005394928528,\"launches\":123000},{\"version\":\"\",\"fraction\":1.3987040117246999E-6,\"launches\":11}]}";
Log.info(content);
var json = JSON.DEFAULT.read(content).readObject();
json.removeDeep((key, index, value) -> key.equals("fraction") && ((Number) value).doubleValue() < 0.5D, true);

Log.info(Ansi.ofObject(json));
Log.info(JSON.DEFAULT.writePretty(json));

Expand Down

0 comments on commit 4ccb604

Please sign in to comment.