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 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);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值