
com.natpryce.snodge.internal.JsonWalk Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of snodge-standalone Show documentation
Show all versions of snodge-standalone Show documentation
A small, extensible Java library to randomly mutate JSON documents. Useful for fuzz testing.
package com.natpryce.snodge.internal;
import com.google.gson.JsonElement;
import com.natpryce.snodge.JsonPath;
import java.util.Map;
import java.util.stream.Stream;
import static com.natpryce.snodge.internal.JsonFunctions.arrayEntries;
import static java.util.stream.Stream.concat;
public class JsonWalk {
public static Stream walk(JsonElement start) {
return walk(start, JsonPath.root);
}
private static Stream walk(JsonElement element, JsonPath elementPath) {
return concat(Stream.of(elementPath), walkChildren(element, elementPath));
}
private static Stream walkChildren(JsonElement element, JsonPath elementPath) {
if (element.isJsonObject()) {
return walkChildren(elementPath, element.getAsJsonObject().entrySet().stream());
} else if (element.isJsonArray()) {
return walkChildren(elementPath, arrayEntries(element.getAsJsonArray()));
} else {
return Stream.empty();
}
}
private static Stream walkChildren(final JsonPath parentPath, Stream> children) {
return children.flatMap(child -> walk(child.getValue(), parentPath.extend(child.getKey())));
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy