Skip to content

Commit

Permalink
stage: new json serializer
Browse files Browse the repository at this point in the history
  • Loading branch information
sakurawald committed Jun 6, 2024
1 parent 788c34b commit d1602e3
Show file tree
Hide file tree
Showing 15 changed files with 138 additions and 12 deletions.
8 changes: 5 additions & 3 deletions src/main/java/io/github/sakurawald/Fuji.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,16 @@
import java.nio.file.Path;


// TODO: /spawn module
// TODO: custom tab list
// TODO: player nickname / prefix / suffix
// TODO: backup module
// TODO: warmup module
// TODO: placeholder module
// TODO: /tppos module
// TODO: command alias module (test priority with ZeroPermissionModule)
// TODO: playtime(every/for) rewards and rank like module
// TODO: kit module
// TODO: kit module -> spec-command
// TODO: luckperms context calculate module
// TODO: /invsee module

public class Fuji implements ModInitializer {
public static final String MOD_ID = "fuji";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import assets.fuji.Cat;
import com.google.gson.*;
import io.github.sakurawald.config.serializer.CommentTypeAdapterFactory;
import io.github.sakurawald.module.initializer.works.work_type.Work;
import io.github.sakurawald.util.ScheduleUtil;
import lombok.Cleanup;
Expand All @@ -27,6 +28,7 @@ public abstract class ConfigHandler<T> {
.setPrettyPrinting()
.disableHtmlEscaping()
.serializeNulls()
.registerTypeAdapterFactory(new CommentTypeAdapterFactory())
.registerTypeAdapter(Work.class, new Work.WorkTypeAdapter())
.create();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package io.github.sakurawald.config.model;

public class AbstractModel {
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import java.util.HashMap;

@SuppressWarnings("InnerClassMayBeStatic")
public class ChatModel {
public class ChatModel extends AbstractModel{

public Format format = new Format();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,22 @@


import com.mojang.authlib.properties.Property;
import io.github.sakurawald.config.serializer.Comment;
import io.github.sakurawald.module.initializer.command_alias.CommandAliasEntry;
import jdk.jfr.Description;
import net.fabricmc.loader.api.FabricLoader;
import org.spongepowered.asm.mixin.injection.Desc;

import java.lang.annotation.Documented;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@SuppressWarnings("ALL")
public class ConfigModel {
public class ConfigModel extends AbstractModel{

@Comment("this is common")
public Common common = new Common();
public Modules modules = new Modules();

Expand All @@ -21,6 +26,7 @@ public class Common {
public Quartz quartz = new Quartz();

public class Quartz {
// @Comment("the default logger level is warn")
public String logger_level = "WARN";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import io.github.sakurawald.module.initializer.head.HeadModule;

public class HeadModel {
public class HeadModel extends AbstractModel {
public HeadModule.EconomyType economyType = HeadModule.EconomyType.ITEM;
public String costType = "minecraft:emerald_block";
public int costAmount = 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import java.util.HashMap;
import java.util.Map;

public class HomeModel {
public class HomeModel extends AbstractModel {

public Map<String, Map<String, Position>> homes = new HashMap<>();
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import java.util.HashSet;

public class PvPModel {
public class PvPModel extends AbstractModel {

public HashSet<String> whitelist = new HashSet<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import java.util.ArrayList;
import java.util.List;

public class SchedulerModel {
public class SchedulerModel extends AbstractModel {

public List<ScheduleJob> scheduleJobs = new ArrayList<>() {
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

import java.util.HashMap;

public class SeenModel {
public class SeenModel extends AbstractModel {
public HashMap<String, Long> player2seen = new HashMap<>();
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import java.util.concurrent.CopyOnWriteArrayList;

public class WorksModel {
public class WorksModel extends AbstractModel {

public CopyOnWriteArrayList<Work> works = new CopyOnWriteArrayList<>();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package io.github.sakurawald.config.serializer;

import java.lang.annotation.*;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface Comment {
String value();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package io.github.sakurawald.config.serializer;

import com.google.gson.*;
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import io.github.sakurawald.config.handler.ConfigHandler;

import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;

public class CommentTypeAdapterFactory implements TypeAdapterFactory {
@Override
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {

TypeAdapter<T> delegateAdapter = gson.getDelegateAdapter(this, type);
Class<T> rawType = (Class<T>) type.getRawType();
Field[] fields = rawType.getDeclaredFields();

for (Field field : fields) {
Annotation[] annotations = field.getDeclaredAnnotations();
for (Annotation annotation : annotations) {
if (annotation.annotationType() == Comment.class) {
Comment commentAnnotation = (Comment) annotation;
field.setAccessible(true);

System.out.println("raw type is " + rawType.getName());
System.out.printf("==== find @Comment `%s` in field %s\n", commentAnnotation.value(), field.getName());

return new TypeAdapter<>() {
@Override
public void write(JsonWriter out, T value) throws IOException {
// JsonObject jsonObject = new JsonObject();
// jsonObject.addProperty("comment", commentAnnotation.value());
// new Gson().getAdapter(JsonObject.class).write(out, jsonObject);

System.out.println("value is " + value);

// out.beginObject();
delegateAdapter.write(out, value);
// out.endObject();
}

@Override
public T read(JsonReader in) throws IOException {
return delegateAdapter.read(in);
}
};
}
}
}

return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package io.github.sakurawald.config.serializer;

import com.google.gson.*;
import io.github.sakurawald.config.model.ConfigModel;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Type;

public class ConfigModelSerializer implements JsonSerializer<ConfigModel> {
@Override
public JsonObject serialize(ConfigModel config, Type typeOfSrc, JsonSerializationContext context) {
Gson gson = new Gson();
JsonObject jsonObject = gson.toJsonTree(config).getAsJsonObject();

Field[] fields = ConfigModel.class.getDeclaredFields();
for (Field field : fields) {
Annotation[] annotations = field.getDeclaredAnnotations();
for (Annotation annotation : annotations) {
if (annotation.annotationType() == Comment.class) {
field.setAccessible(true);
try {
Object value = field.get(config);
String fieldName = field.getName();
jsonObject.addProperty(fieldName + "_comment", ((Comment) annotation).value());
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
}

return jsonObject;
}

public static void main(String[] args) {
Gson gson = new GsonBuilder()
.registerTypeAdapter(ConfigModel.class, new ConfigModelSerializer())
.create();

ConfigModel config = new ConfigModel();
// 设置ConfigModel的字段值

String json = gson.toJson(config);
System.out.println(json);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
@FunctionalInterface
public interface HeightFinder {
/**
* Attempts to find a safe surface Y value for the specified X & Z values.
* Attempts to find a safe surface Y value for the specified X and Z values.
*
* @return A Y value corresponding to the player's feet pos
*/
Expand Down

0 comments on commit d1602e3

Please sign in to comment.