Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public enum GsonHelper {
- INSTANCE;
- public static final Gson GSON = new GsonBuilder()
- .disableHtmlEscaping()
- .registerTypeAdapter(Region.class, new RegionAdapter())
- .create();
- }
- public interface Region {
- String getName();
- UUID getUUID();
- Chunk getChunk();
- }
- public class RegionImpl implements Region {
- private final String name;
- private final UUID uuid;
- private final Chunk chunk;
- public RegionImpl(String name, UUID uuid, Chunk chunk) {
- this.name = name;
- this.uuid = uuid;
- this.chunk = chunk;
- }
- String toJson() {
- return GsonHelper.GSON.toJson(this, Region.class);
- }
- Region fromJson(String json) {
- return GsonHelper.GSON.fromJson(json, Region.class);
- }
- }
- public static class RegionAdapter implements JsonSerializer<Region>, JsonDeserializer<Region> {
- @Override
- public Region deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
- final var object = json.getAsJsonObject();
- String name = object.get("name").getAsString();
- UUID uuid = context.deserialize(object.get("uuid"), UUID.class); // implements custom UUIDAdapter and register
- final String worldName = object.get("worldName").getAsString();
- // problem with getting chunk for creating region
- return new RegionImpl(name, uuid, todo: chunk);
- }
- @Override
- public JsonElement serialize(Region src, Type typeOfSrc, JsonSerializationContext context) {
- JsonObject object = new JsonObject();
- object.addProperty("name", src.getName());
- object.add("uuid", context.serialize(src.getUUID(), UUID.class));
- object.addProperty("worldName", src.getChunk().getWorld().getName());
- return object;
- }
- }
Add Comment
Please, Sign In to add comment