Java内容重新整理删除过期的东西

This commit is contained in:
estom
2025-09-14 03:49:42 -04:00
parent 9b8524ff80
commit 885b795e45
413 changed files with 643 additions and 1340 deletions

View File

@@ -0,0 +1,42 @@
package cn.aofeng.demo.json.gson;
import java.lang.reflect.Type;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
/**
* 自定义序列化。
*
* @author <a href="mailto:aofengblog@163.com">聂勇</a>
*/
public class CustomSerialize {
public static void main(String[] args) {
GsonBuilder builder = new GsonBuilder();
builder.registerTypeAdapter(Person.class, new PersonSerializer());
Gson gson = builder.create();
Person obj = new Person("aofeng", 32);
System.out.println( gson.toJson(obj) ); // 输出结果:{"PersonName":"aofeng","PersonAge":32}
}
public static class PersonSerializer implements JsonSerializer<Person> {
@Override
public JsonElement serialize(Person obj, Type type,
JsonSerializationContext context) {
JsonObject jo = new JsonObject();
jo.addProperty("PersonName", obj.getName());
jo.addProperty("PersonAge", obj.getAge());
return jo;
}
} // end of PersonSerializer
}