Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package me.catcoder.plugins.ubans.util;
- import com.google.common.collect.Iterators;
- import com.google.gson.JsonArray;
- import com.google.gson.JsonElement;
- import com.google.gson.JsonObject;
- import com.google.gson.JsonPrimitive;
- import java.util.Iterator;
- import java.util.Objects;
- import java.util.Optional;
- import java.util.function.Function;
- import java.util.function.Predicate;
- import java.util.regex.Pattern;
- /**
- * Wrapper for JsonObject with recursive path walking and null safety.
- *
- * @author CatCoder
- */
- public class JsonWrapper {
- private static final Pattern PATH_REGEX = Pattern.compile("\\.");
- private final JsonObject root;
- public JsonWrapper(JsonObject root) {
- this.root = Objects.requireNonNull(root, "object");
- }
- public JsonObject root() {
- return root;
- }
- // PRIMITIVES
- public <T> JsonValueTransformer<T> primitives(
- Function<JsonPrimitive, T> func,
- Predicate<JsonPrimitive> filter,
- Function<T, JsonElement> valueExtractor) {
- return new SimpleJsonValueTransformer<>((path) -> element(path)
- .filter(JsonElement::isJsonPrimitive)
- .map(JsonElement::getAsJsonPrimitive)
- .filter(filter).map(func), valueExtractor);
- }
- public JsonValueTransformer<String> strings() {
- return primitives(JsonPrimitive::getAsString, JsonPrimitive::isString, JsonPrimitive::new);
- }
- public JsonValueTransformer<Number> numbers() {
- return primitives(JsonPrimitive::getAsNumber, JsonPrimitive::isNumber, JsonPrimitive::new);
- }
- public JsonValueTransformer<Boolean> booleans() {
- return primitives(JsonPrimitive::getAsBoolean, JsonPrimitive::isBoolean, JsonPrimitive::new);
- }
- // PRIMITIVES
- public JsonValueTransformer<JsonObject> objects() {
- return new SimpleJsonValueTransformer<>((path) -> element(path)
- .filter(JsonElement::isJsonObject)
- .map(JsonElement::getAsJsonObject), (v) -> v);
- }
- public JsonValueTransformer<JsonArray> arrays() {
- return new SimpleJsonValueTransformer<>((path) -> element(path)
- .filter(JsonElement::isJsonArray)
- .map(JsonElement::getAsJsonArray), (v) -> v);
- }
- public Optional<JsonWrapper> sub(String path) {
- return element(path)
- .filter(JsonElement::isJsonObject)
- .map(JsonElement::getAsJsonObject)
- .map(JsonWrapper::new);
- }
- public int set(String path, JsonElement value) {
- Objects.requireNonNull(path, "path");
- Iterator<String> pathIterator = Iterators.forArray(PATH_REGEX.split(path));
- JsonObject currentObject = root;
- String lastPath = null;
- int depth = 0;
- // Build tree
- while (pathIterator.hasNext()) {
- if (lastPath != null) {
- JsonElement next = currentObject.get(lastPath);
- if (next == null) {
- next = new JsonObject();
- currentObject.add(lastPath, next);
- }
- currentObject = (JsonObject) next;
- depth++;
- }
- lastPath = pathIterator.next();
- }
- if (lastPath != null) {
- currentObject.add(lastPath, value);
- }
- return depth;
- }
- public Optional<JsonElement> element(String path) {
- Iterator<String> pathIterator = Iterators.forArray(PATH_REGEX.split(path));
- // Iterator cannot be empty, so we allowed to do this
- String prevPath = pathIterator.next();
- JsonElement lastElement = root.get(prevPath);
- if (lastElement == null) {
- return Optional.empty();
- }
- while (pathIterator.hasNext()) {
- if (lastElement == null) {
- break;
- }
- prevPath = pathIterator.next();
- lastElement = lastElement.getAsJsonObject().get(prevPath);
- }
- return Optional.ofNullable(lastElement);
- }
- private class SimpleJsonValueTransformer<T> implements JsonValueTransformer<T> {
- private final Function<String, Optional<T>> getter;
- private final Function<T, JsonElement> valueExtractor;
- public SimpleJsonValueTransformer(Function<String, Optional<T>> getter,
- Function<T, JsonElement> valueExtractor) {
- this.getter = getter;
- this.valueExtractor = valueExtractor;
- }
- @Override
- public Optional<T> transform(String path) {
- return getter.apply(path);
- }
- @Override
- public int set(String path, T value) {
- return JsonWrapper.this.set(path, valueExtractor.apply(value));
- }
- }
- public interface JsonValueTransformer<T> {
- Optional<T> transform(String path);
- @SuppressWarnings("OptionalGetWithoutIsPresent")
- default T unwrap(String path) {
- return transform(path).get();
- }
- int set(String path, T value);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement