If XModel
class is not big you can write your custom deserialiser as below where you have control over incoming element:
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.JsonPrimitive;
import java.io.File;
import java.io.FileReader;
import java.lang.reflect.Type;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
public class GsonApp {
public static void main(String[] args) throws Exception {
File jsonFile = new File("./resource/test.json").getAbsoluteFile();
Gson gson = new GsonBuilder()
.registerTypeAdapter(XModel.class, new XModelJsonDeserializer())
.create();
System.out.println(gson.fromJson(new FileReader(jsonFile), XModel.class));
}
}
class XModelJsonDeserializer implements JsonDeserializer<XModel> {
private final Set<String> TRUE_STRINGS = new HashSet<>(Arrays.asList("true", "1", "yes"));
@Override
public XModel deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
XModel response = new XModel();
JsonObject jsonResponse = (JsonObject) json;
response.setName(jsonResponse.get("name").getAsString());
// other fields
JsonElement dataElement = jsonResponse.get("isValid");
if (dataElement.isJsonNull()) {
response.setValid(false);
} else if (dataElement.isJsonPrimitive()) {
JsonPrimitive jsonPrimitive = dataElement.getAsJsonPrimitive();
if (jsonPrimitive.isBoolean()) {
response.setValid(jsonPrimitive.getAsBoolean());
} else if (jsonPrimitive.isNumber()) {
response.setValid(jsonPrimitive.getAsNumber().intValue() == 1);
} else if (jsonPrimitive.isString()) {
response.setValid(TRUE_STRINGS.contains(jsonPrimitive.getAsString()));
}
System.out.println("Json data is primitive: " + dataElement.getAsString());
} else if (dataElement.isJsonObject() || dataElement.isJsonArray()) {
response.setValid(true); //?!?!
}
return response;
}
}
For below JSON
payload:
{
"name" : "john doe",
"isValid" : true
}
above program prints:
Json data is primitive: true
XModel{name='john doe', isValid=true}
For JSON
payload:
{
"name" : "john doe",
"isValid" : 1
}
prints:
Json data is primitive: 1
XModel{name='john doe', isValid=true}
Your model is clear because all work is done on deserialiser level.
A little bit much precise solution would be to serialise primitive
only. Let's assume that model looks like below:
class XModel {
private String name;
@JsonAdapter(value = BooleanJsonDeserializer.class)
private boolean isValid;
// getters, setters
}
and our BooleanJsonDeserializer
deserialiser looks like below:
class BooleanJsonDeserializer implements JsonDeserializer<Boolean> {
private final Set<String> TRUE_STRINGS = new HashSet<>(Arrays.asList("true", "1", "yes"));
@Override
public Boolean deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
System.out.println(json);
JsonPrimitive jsonPrimitive = json.getAsJsonPrimitive();
if (jsonPrimitive.isBoolean()) {
return jsonPrimitive.getAsBoolean();
} else if (jsonPrimitive.isNumber()) {
return jsonPrimitive.getAsNumber().intValue() == 1;
} else if (jsonPrimitive.isString()) {
return TRUE_STRINGS.contains(jsonPrimitive.getAsString().toLowerCase());
}
return false;
}
}
You need to only annotate every boolean
property with this adapter in your model and it is ready to handle: 1
, True
, etc.