java控制序列化版本,Java GSON自定义反序列化器版本控制支持
I created a custom deserializer for my Person class. This is just for demonstration purpose. My actual class is more complicated.The json file contains the version of the format and after that an arra
I created a custom deserializer for my Person class. This is just for demonstration purpose. My actual class is more complicated.
The json file contains the version of the format and after that an array of persons.
On version 1.0 i have a name and age for every person.
On version 2.0 i have a name, age and additionally the gender.
My problem here is that i do not have access to the version in the custom deserializer, or at least didn't figure out how to get it yet.
Is there any other way to do this without jsonObj.get("gender").has();
or if (jsonObj.get("gender") != null)?
GSON Version: 2.8.5
Deserializer:
private class PersonDeserializer implements JsonDeserializer
{
@Override
public PersonDatapoint deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException
{
JsonObject jsonObj = json.getAsJsonObject();
// version 1.0
Person person = new Person(jsonObj.get("name").getAsString());
person.setAge(jsonObj.get("age").getAsInt());
// version 2.0
// how to determine version field in json File?
// person.setGender(jsonObj.get("gender").getAsString());
return person;
}
}
JSON File v1.0:
{
"version": "1.0",
"persons": [
{
"name": "Alex",
"age": 30
},
{
"name": "John",
"age": 31
},
{
"name": "Elise",
"age": 32
}]
}
JSON File v2.0:
{
"version": "2.0",
"persons": [
{
"name": "Alex",
"age": 30,
"gender": "male"
},
{
"name": "John",
"age": 31,
"gender": "male"
},
{
"name": "Elise",
"age": 32,
"gender": "female"
}]
}
解决方案
To have access to version field do not make JsonDeserializer for person but for the whole JSON you have. So let us assume your JSON is some kind of a response that could be presented with a Java class like:
@Getter @Setter
public class Response {
private Double version;
private Person[] persons; // versioned stuff
}
Your class Person might be like:
@Getter @Setter
public class Person {
@Since(1.0) // these annotations are put just for demonstrative purpose but are
// actually also functional, see deserializer
private String name;
@Since(1.0)
private Integer age;
@Since(2.0)
private String gender;
}
Note: This Person is a bit bad example class since it could easily be deserialized without any custom deserializer from v1.0 or v2.0 JSON. Deserializng from v1.0 would just leave gender null. Anyway your custom deserializer - making use of JSON field version - might then look like:
public class ResponseDeserializer implements JsonDeserializer {
@Override
public Response deserialize(JsonElement json, Type typeOfT,
JsonDeserializationContext context)
throws JsonParseException {
// This is how to get the version
Double version = json.getAsJsonObject().get("version").getAsDouble();
// and below is jsut an example what you could do with version
Gson gson = new GsonBuilder()
.setVersion(version) // this is where @Since might be handy
.setPrettyPrinting()
.create();
return gson.fromJson(json, Response.class);
}
}
开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!
更多推荐
所有评论(0)