Gson TypeAdapter 不适用于带有注释 JsonAdapter 的 null
Gson TypeAdapter not working for null with annotation JsonAdapter
TypeAdapter nullSafe 跳过自定义处理代码。
我已经编写了 IntegerTypeAdapter 并用作注释来实现以下输出,但是在 gson 库中编写的代码完全不允许使用注释配置进行空处理。
如果我取消注释第 1、2、4 行和注释第 3 行,那么我仍然没有达到我想要的输出。
When i debug library code at line number 189 (given screenshot image)
not reaching to my custom Adapter code so null handling not working
with annotation. Is there any alternative way to achieve desired
output based on annotation configuration?
期望的输出:
- catlives - ""(用作注释且具有空值)
- phone - 89(使用注释并具有整数值)
- rank -(不存在键,因为未使用注释且具有空值)
@AllArgsConstructor
@Getter
@Setter
class CatDetails {
private String name;
private String breed;
private String email;
//@JsonAdapter(EmptyIfNullIntegerTypeAdapter.class) //1
private Integer catlives;
//@JsonAdapter(EmptyIfNullIntegerTypeAdapter.class) //2
private Integer phone;
private String city;
private Integer rank;
private Boolean likesMice;
}
public class EmptyIfNullIntegerTypeAdapter extends TypeAdapter<Integer> {
@Override
public void write(JsonWriter out, Integer value) throws IOException {
if (value == null) {
out.value("");
return;
}
out.value(value);
}
@Override
public Integer read(JsonReader in) throws IOException {
if (in.peek() == JsonToken.NULL) {
in.nextNull();
return 0;
}
return in.nextInt();
}
}
public class MyApp {
public static void main(String... args) {
CatDetails user = new CatDetails("cat007", "YUI", "abc.cat@gmail.com", null,89, "new",null, true);
Gson gson = new GsonBuilder().setPrettyPrinting().registerTypeAdapter(Integer.class, new EmptyIfNullIntegerTypeAdapter()).create(); //3
//Gson gson = new GsonBuilder().setPrettyPrinting().create(); //4
String json = gson.toJson(user);
System.out.println(json);
}
}
Currently code giving below Output:
{
"name": "cat007",
"breed": "YUI",
"email": "abc.cat@gmail.com",
"catlives": "",
"phone": 89,
"city": "new",
"rank": "",
"likesMice": true
}
查看下面带有注释的输出:
仅对两个整数字段(catlives & phone)特别注意如果值为空,则写入空字符串。而对于其他整数字段,当值为空时则工作像默认值(跳过键值对)。
{
"name": "cat007",
"breed": "YUI",
"email": "abc.cat@gmail.com",
"catlives": "",
"phone": 89,
"city": "new",
"likesMice": true
}
您需要将 @JsonAdapter#nullSafe
设置为 false
,否则默认情况下 Gson 会生成您指定的适配器 null-safe。例如:
@JsonAdapter(value = EmptyIfNullIntegerTypeAdapter.class, nullSafe = false)
private Integer catlives;
- JsonAdapter 属性 nullSafe = false
解决了我的问题。
@JsonAdapter(value = EmptyIfNullIntegerTypeAdapter.class, nullSafe = false)
private Integer catlives;
But the same does not work with JsonSerializer.
public class EmptyIfNullTypeAdapter<T> implements JsonSerializer<T> {
@Override
public JsonElement serialize(T src, Type typeOfSrc, JsonSerializationContext context) {
if (src == null) {
return new JsonPrimitive("");
} else {
switch (typeOfSrc.getTypeName()) {
case "java.lang.Integer":
return new JsonPrimitive(Integer.parseInt(src.toString()));
case "java.lang.Long":
return new JsonPrimitive(Long.parseLong(src.toString()));
case "java.lang.Double":
return new JsonPrimitive(Double.parseDouble(src.toString()));
default:
return new JsonPrimitive(src.toString());
}
}
}
}
@JsonAdapter(value=EmptyIfNullTypeAdapter.class, nullSafe=false)
private Integer catlives;
@JsonAdapter(value=EmptyIfNullTypeAdapter.class, nullSafe=false)
private Integer phone;
我已经编写了 IntegerTypeAdapter 并用作注释来实现以下输出,但是在 gson 库中编写的代码完全不允许使用注释配置进行空处理。
如果我取消注释第 1、2、4 行和注释第 3 行,那么我仍然没有达到我想要的输出。
When i debug library code at line number 189 (given screenshot image) not reaching to my custom Adapter code so null handling not working with annotation. Is there any alternative way to achieve desired output based on annotation configuration?
期望的输出:
- catlives - ""(用作注释且具有空值)
- phone - 89(使用注释并具有整数值)
- rank -(不存在键,因为未使用注释且具有空值)
@AllArgsConstructor
@Getter
@Setter
class CatDetails {
private String name;
private String breed;
private String email;
//@JsonAdapter(EmptyIfNullIntegerTypeAdapter.class) //1
private Integer catlives;
//@JsonAdapter(EmptyIfNullIntegerTypeAdapter.class) //2
private Integer phone;
private String city;
private Integer rank;
private Boolean likesMice;
}
public class EmptyIfNullIntegerTypeAdapter extends TypeAdapter<Integer> {
@Override
public void write(JsonWriter out, Integer value) throws IOException {
if (value == null) {
out.value("");
return;
}
out.value(value);
}
@Override
public Integer read(JsonReader in) throws IOException {
if (in.peek() == JsonToken.NULL) {
in.nextNull();
return 0;
}
return in.nextInt();
}
}
public class MyApp {
public static void main(String... args) {
CatDetails user = new CatDetails("cat007", "YUI", "abc.cat@gmail.com", null,89, "new",null, true);
Gson gson = new GsonBuilder().setPrettyPrinting().registerTypeAdapter(Integer.class, new EmptyIfNullIntegerTypeAdapter()).create(); //3
//Gson gson = new GsonBuilder().setPrettyPrinting().create(); //4
String json = gson.toJson(user);
System.out.println(json);
}
}
Currently code giving below Output:
{
"name": "cat007",
"breed": "YUI",
"email": "abc.cat@gmail.com",
"catlives": "",
"phone": 89,
"city": "new",
"rank": "",
"likesMice": true
}
查看下面带有注释的输出:
仅对两个整数字段(catlives & phone)特别注意如果值为空,则写入空字符串。而对于其他整数字段,当值为空时则工作像默认值(跳过键值对)。
{
"name": "cat007",
"breed": "YUI",
"email": "abc.cat@gmail.com",
"catlives": "",
"phone": 89,
"city": "new",
"likesMice": true
}
您需要将 @JsonAdapter#nullSafe
设置为 false
,否则默认情况下 Gson 会生成您指定的适配器 null-safe。例如:
@JsonAdapter(value = EmptyIfNullIntegerTypeAdapter.class, nullSafe = false)
private Integer catlives;
- JsonAdapter 属性 nullSafe = false
解决了我的问题。
@JsonAdapter(value = EmptyIfNullIntegerTypeAdapter.class, nullSafe = false)
private Integer catlives;
But the same does not work with JsonSerializer.
public class EmptyIfNullTypeAdapter<T> implements JsonSerializer<T> {
@Override
public JsonElement serialize(T src, Type typeOfSrc, JsonSerializationContext context) {
if (src == null) {
return new JsonPrimitive("");
} else {
switch (typeOfSrc.getTypeName()) {
case "java.lang.Integer":
return new JsonPrimitive(Integer.parseInt(src.toString()));
case "java.lang.Long":
return new JsonPrimitive(Long.parseLong(src.toString()));
case "java.lang.Double":
return new JsonPrimitive(Double.parseDouble(src.toString()));
default:
return new JsonPrimitive(src.toString());
}
}
}
}
@JsonAdapter(value=EmptyIfNullTypeAdapter.class, nullSafe=false)
private Integer catlives;
@JsonAdapter(value=EmptyIfNullTypeAdapter.class, nullSafe=false)
private Integer phone;