multi gson

This commit is contained in:
mcrcortex
2025-04-25 12:15:50 +10:00
parent 03e79db0ba
commit 987427b893

View File

@@ -0,0 +1,72 @@
package me.cortex.voxy.common.util;
import com.google.gson.*;
import java.lang.reflect.Modifier;
import java.util.*;
public class MultiGson {
private final List<Class<?>> classes;
private final Gson GSON = new GsonBuilder()
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
.setPrettyPrinting()
.excludeFieldsWithModifiers(Modifier.PRIVATE)
.create();
private MultiGson(List<Class<?>> classes) {
this.classes = classes;
}
public String toJson(Object... objects) {
Object[] map = new Object[this.classes.size()];
if (map.length != objects.length) {
throw new IllegalArgumentException("Incorrect number of input args");
}
for (var obj : objects) {
if (obj == null) {
throw new IllegalArgumentException();
}
int i = this.classes.indexOf(obj.getClass());
if (i == -1) {
throw new IllegalArgumentException("Unknown object class: " + obj.getClass());
}
if (map[i] != null) {
throw new IllegalArgumentException("Duplicate entry classes");
}
}
var json = new JsonObject();
for (Object entry : map) {
GSON.toJsonTree(entry).getAsJsonObject().asMap().forEach((i,j) -> {
if (json.has(i)) {
throw new IllegalArgumentException("Duplicate name inside unified json: " + i);
}
json.add(i, j);
});
}
return GSON.toJson(json);
}
public Map<Class<?>, Object> fromJson(String json) {
var obj = GSON.fromJson(json, JsonObject.class);
LinkedHashMap<Class<?>, Object> objects = new LinkedHashMap<>();
for (var cls : this.classes) {
objects.put(cls, GSON.fromJson(obj, cls));
}
return objects;
}
public static class Builder {
private final LinkedHashSet<Class<?>> classes = new LinkedHashSet<>();
public Builder add(Class<?> clz) {
if (!this.classes.add(clz)) {
throw new IllegalArgumentException("Class has already been added");
}
return this;
}
public MultiGson build() {
return new MultiGson(new ArrayList<>(this.classes));
}
}
}