当前位置:网站首页>Several ways of gson's @jsonadapter annotation
Several ways of gson's @jsonadapter annotation
2022-07-01 06:29:00 【ydfind】
Gson Of @JsonAdater Several ways of annotation
summary
You can customize TypeAdapter and TypeAdapterFactory The way , Customize gson Serialization and deserialization rules for ,TypeAdapterFactory You can get the context gson、TokenType type ;
Or by inheritance JsonReader Rewrite the code , stay beginArray and endArray Try to skip array Of string Form left and right Double quotes ", gson.fromJson(myJsonReader, Type) It can also be realized When parsing, it will automatically String Turn into List, But with annotations @JsonAdapter In a more formal way ;
Problem description
json character string :
{
"cityIds": "[1,2,1001,13131]",
"types": "[{\"name\": \"biz\",\"details\": [1]}]"
}
java Object definitions :
@Data
public static class RequestParams {
// json The string corresponds to String
private List<TypeItem> types;
private List<Integer> cityIds;
}
@Data
public static class TypeItem {
private String name;
private List<Integer> details;
}
You can see json Inside cityIds and types All are String, and pojo Inside, it is List, Use gson Of fromJson take json To pojo There will be an error
Mode one
@JsonAdapter(StringCollectionTypeAdapterFactory.class)
private List<TagItem> tags;
@JsonAdapter(StringCollectionTypeAdapterFactory.class)
private List<Integer> cityIds;
public static class StringCollectionTypeAdapterFactory implements TypeAdapterFactory {
@Override
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) {
// in order to write Can be used when elementTypeAdapter
Type type = typeToken.getType();
Class<? super T> rawType = typeToken.getRawType();
if (!Collection.class.isAssignableFrom(rawType)) {
return null;
}
Type elementType = $Gson$Types.getCollectionElementType(type, rawType);
TypeAdapter<?> elementTypeAdapter = gson.getAdapter(TypeToken.get(elementType));
TypeAdapter<T> result = new Adapter(gson, elementTypeAdapter, typeToken);
return result;
}
private static final class Adapter<E> extends TypeAdapter<Collection<E>> {
private final TypeAdapter<E> elementTypeAdapter;
private final Gson gson;
private final TypeToken listType;
public Adapter(Gson context, TypeAdapter<E> elementTypeAdapter, TypeToken listType) {
this.elementTypeAdapter = elementTypeAdapter;
this.gson = context;
this.listType = listType;
}
@Override public Collection<E> read(JsonReader in) throws IOException {
if (in.peek() == JsonToken.NULL) {
in.nextNull();
return null;
}
List<E> list = gson.fromJson(in.nextString(), listType.getType());
return list;
}
// write Then you can put array Of string Format , Become... Again array
@Override public void write(JsonWriter out, Collection<E> collection) throws IOException {
if (collection == null) {
out.nullValue();
return;
}
out.beginArray();
for (E element : collection) {
elementTypeAdapter.write(out, element);
}
out.endArray();
}
}
}
Mode two -write Original sample
public static class StringCollectionTypeAdapterFactory1 implements TypeAdapterFactory {
@Override
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) {
return new Adapter(gson, typeToken);
}
private static final class Adapter<E> extends TypeAdapter<Collection<E>> {
private final Gson gson;
private final TypeToken listType;
public Adapter(Gson context, TypeToken listType) {
this.gson = context;
this.listType = listType;
}
@Override public Collection<E> read(JsonReader in) throws IOException {
if (in.peek() == JsonToken.NULL) {
in.nextNull();
return null;
}
List<E> list = gson.fromJson(in.nextString(), listType.getType());
return list;
}
@Override public void write(JsonWriter out, Collection<E> collection) throws IOException {
if (collection == null) {
out.nullValue();
return;
}
out.value(gson.toJson(collection));
}
}
}
Mode three - Simple writing
private static class CollectionStringTypeAdapterFactory implements TypeAdapterFactory {
@Override
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
return new TypeAdapter<T>() {
@Override
public void write(JsonWriter out, T value) throws IOException {
if (value == null) {
out.nullValue();
return;
}
gson.getAdapter(type).write(out, value);
}
@Override
public T read(JsonReader in) throws IOException {
if (in.peek() == JsonToken.NULL) {
return null;
}
if (in.peek() == JsonToken.BEGIN_ARRAY) {
return gson.getAdapter(type).read(in);
}
T collection = gson.fromJson(in.nextString(), type.getType());
return collection;
}
};
}
}
The test cases are as follows :
@Test
public void testGson1() {
Gson gson = new Gson();
RequestParams requestParam;
String json;
// 1. Automatically put string Converted to attribute List
json = "{\"id\": \"000000\",\"types\": \"[{\\\"name\\\":\\\"name1\\\",\\\"list\\\":[1,2]},{\\\"name\\\":\\\"name2\\\",\\\"list\\\":[3,4]}]\",\"keywordIds\": \"[12,13]\"}";
System.out.println(json);
requestParam = gson.fromJson(json, new TypeToken<RequestParams>() {}.getType());
Assert.assertTrue(requestParam.getTypes().get(0).getClass().getName().indexOf("TypeItem") >= 0);
System.out.println(gson.toJson(requestParam));
// 2.jsonArray It can also be changed to List
json = "{\"id\": \"000000\",\"keywordIds\": [12,13],\"types\": [{\"name\":\"name1\",\"list\":[1,2]},{\"name\":\"name2\",\"list\":[3,4]}]}";
requestParam = gson.fromJson(json, new TypeToken<RequestParams>() {}.getType());
Assert.assertTrue(requestParam.getTypes().get(0).getClass().getName().indexOf("TypeItem") >= 0);
System.out.println(gson.toJson(requestParam));
// 3. How to wrap lines
json = "{\n" +
"\t\"id\": \"000000\",\n" +
"\t\"keywordIds\": [12,13],\n" +
"\t\"types\": [{\"name\":\"name1\",\"list\":[1,2]},{\"name\":\"name2\",\"list\":[3,4]}]\n" +
"}";
requestParam = gson.fromJson(json, new TypeToken<RequestParams>() {}.getType());
Assert.assertTrue(requestParam.getTypes().get(0).getClass().getName().indexOf("TypeItem") >= 0);
System.out.println(gson.toJson(requestParam));
// 4. Could you please List Inside Integer become String Well
json = "{\n" +
"\t\"id\": \"000000\",\n" +
"\t\"keywordIds\": [12,13],\n" +
"\t\"types\": [{\"name\":\"name1\",\"list1\":[1,2]},{\"name\":\"name2\",\"list1\":[3,4]}]\n" +
"}";
requestParam = gson.fromJson(json, new TypeToken<RequestParams>() {}.getType());
Assert.assertTrue(requestParam.getTypes().get(0).getClass().getName().indexOf("TypeItem") >= 0);
System.out.println(gson.toJson(requestParam));
// 5. Could you please List Inside Integer become String
json = "{\n" +
"\t\"id\": \"000000\",\n" +
"\t\"keywordIds1\": [12,13],\n" +
"\t\"types\": [{\"name\":\"name1\",\"list1\":[1,2]},{\"name\":\"name2\",\"list1\":[3,4]}]\n" +
"}";
requestParam = gson.fromJson(json, new TypeToken<RequestParams>() {}.getType());
Assert.assertTrue(requestParam.getTypes().get(0).getClass().getName().indexOf("TypeItem") >= 0);
System.out.println(gson.toJson(requestParam));
// 6. Automatically put string Converted to attribute List, also list Inside Integer Turn into String
json = "{\"id\": \"000000\",\"types\": \"[{\\\"name\\\":\\\"name1\\\",\\\"list1\\\":[1,2]},{\\\"name\\\":\\\"name2\\\",\\\"list1\\\":[3,4]}]\",\"keywordIds1\": \"[12,13]\"}";
System.out.println(json);
requestParam = gson.fromJson(json, new TypeToken<RequestParams>() {}.getType());
Assert.assertTrue(requestParam.getTypes().get(0).getClass().getName().indexOf("TypeItem") >= 0);
System.out.println(gson.toJson(requestParam));
}
@Data
public static class RequestParams {
private String id;
@JsonAdapter(CollectionStringTypeAdapterFactory.class)
private List<TypeItem> types;
@JsonAdapter(CollectionStringTypeAdapterFactory.class)
private List<Integer> keywordIds;
@JsonAdapter(CollectionStringTypeAdapterFactory.class)
private List<String> keywordIds1;
}
@Data
public static class TypeItem {
private String name;
private List<Integer> list;
private List<String> list1;
}
边栏推荐
- HCM Beginner (III) - quickly enter pa70 and pa71 to browse employee information PA10
- 请求模块(requests)
- Draw a directed graph based on input
- 码力十足学量化|如何在财务报告寻找合适的财务公告
- C语言课设工资管理系统(大作业)
- What are the functions of LAN monitoring software
- 网络爬虫
- C语言课设销售管理系统设计(大作业)
- Embedded system
- [self use of advanced mathematics in postgraduate entrance examination] advanced mathematics Chapter 1 thinking map in basic stage
猜你喜欢
![[enterprise data security] upgrade backup strategy to ensure enterprise data security](/img/59/e44c6533aa546e8854ef434aa64113.png)
[enterprise data security] upgrade backup strategy to ensure enterprise data security

HCM Beginner (IV) - time

关于变量是否线程安全的问题

idea 好用插件汇总!!!

C language course set up salary management system (big homework)

C语言课设学生考勤系统(大作业)

概率论学习笔记

ForkJoin和Stream流测试

Redis安装到Windows系统上的详细步骤
![阿里OSS Postman Invalid according to Policy: Policy Condition failed: [“starts-with“, “$key“, “test/“]](/img/3c/7684b7c594f7871471f89007294703.png)
阿里OSS Postman Invalid according to Policy: Policy Condition failed: [“starts-with“, “$key“, “test/“]
随机推荐
C language course set up student elective course system (big homework)
关于变量是否线程安全的问题
Self confidence is indispensable for technology
局域网监控软件有哪些功能
异常检测方法梳理,看这篇就够了!
【LeetCode】Day91-存在重复元素
连续四年入选Gartner魔力象限,ManageEngine卓豪是如何做到的?
@Transactional的传播属性REQUIRES_NEW深入理解
B-tree series
[ManageEngine Zhuohao] helps Huangshi Aikang hospital realize intelligent batch network equipment configuration management
SQL中DML语句(数据操作语言)
Code power is full of quantitative learning | how to find a suitable financial announcement in the financial report
网络爬虫
数据库笔记
【#Unity Shader#自定义材质面板_第二篇】
C语言课设学生信息管理系统(大作业)
Comment imprimer le tableau original
libpng12.so. 0: cannot open shared object file: no such file or directory
Mysql 表分区创建方法
TCL statements in SQL (transaction control statements)