TypeAdapter nullSafe skipping custom handling code.
I have written IntegerTypeAdapter and use as annotation to achieve below output, but written code in gson library completely not allowing null handling with annotation configuration.
If I uncomment line 1,2,4 and comment line 3, then still i don't achieve my desire output.
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?
Desired Output:
- catlives - "" (As used annotation and have null value)
- phone - 89 (As used annotation and have Integer value)
- rank - (No key present because not using annotation and have null value)
@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", "[email protected]", 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": "[email protected]",
"catlives": "",
"phone": 89,
"city": "new",
"rank": "",
"likesMice": true
}
Looking below output with annotation:
Special care with two Integer fields (catlives & phone) only if value is null it write empty string. Whereas for other Integer field when value null then work like default (skip key-value pair).
{
"name": "cat007",
"breed": "YUI",
"email": "[email protected]",
"catlives": "",
"phone": 89,
"city": "new",
"likesMice": true
}
CodePudding user response:
You need to set @JsonAdapter#nullSafe
to false
, otherwise Gson by default makes your specified adapter null-safe. For example:
@JsonAdapter(value = EmptyIfNullIntegerTypeAdapter.class, nullSafe = false)
private Integer catlives;
CodePudding user response:
- JsonAdapter property nullSafe = false
solved my problem.
@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;