-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
788c34b
commit d1602e3
Showing
15 changed files
with
138 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
src/main/java/io/github/sakurawald/config/model/AbstractModel.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package io.github.sakurawald.config.model; | ||
|
||
public class AbstractModel { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
src/main/java/io/github/sakurawald/config/serializer/Comment.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
56 changes: 56 additions & 0 deletions
56
src/main/java/io/github/sakurawald/config/serializer/CommentTypeAdapterFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
src/main/java/io/github/sakurawald/config/serializer/ConfigModelSerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters