如何序列化可选< T> Gson的课程? [英] How to serialize Optional<T> classes with Gson?

查看:90
本文介绍了如何序列化可选< T> Gson的课程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  private final String messageBundle; 
private final List< String> messageParams;
private final String actionBundle;
private final Map< String,String>数据;
private final可选< Pair< Integer,TimeUnit>> TTL;
private final可选< Integer> badgeNumber;
private final可选< String> collapseKey;

该对象位于一个库中,我宁愿不对其进行序列化修改,而想以避免创建另一个DTO的成本。



如何序列化/反序列化可选属性?可选没有默认构造函数(都不是apache commons Pair),但是我不能使用InstanceCreator,并且不太了解如何创建一个TypeAdapter,它只是将序列化委托给底层的Optional元素。

解决方案

Ilya的解决方案忽略了类型参数,因此它在一般情况下无法正常工作。我的解决方案很复杂,因为需要区分 null Optional.absent() - 否则您

  public class GsonOptionalDeserializer< T> 
实现了JsonSerializer< Optional< T>> ;, JsonDeserializer< Optional< T>> {

@Override
public可选< T>反序列化(JsonElement json,类型typeOfT,JsonDeserializationContext上下文)
抛出JsonParseException {
final JsonArray asJsonArray = json.getAsJsonArray();
final JsonElement jsonElement = asJsonArray.get(0);
final T value = context.deserialize(jsonElement,((ParameterizedType)typeOfT).getActualTypeArguments()[0]);
return Optional.fromNullable(value);

$ b @Override
public JsonElement serialize(可选< T> src,type typeOfSrc,JsonSerializationContext context){
final JsonElement element = context.serialize(src。 orNull());
final JsonArray result = new JsonArray();
result.add(element);
返回结果;
}
}


I have an object with the following attributes.

private final String messageBundle;
private final List<String> messageParams;
private final String actionBundle;
private final Map<String, String> data;
private final Optional<Pair<Integer,TimeUnit>> ttl;
private final Optional<Integer> badgeNumber;
private final Optional<String> collapseKey;

The object is in a library, i would rather not modify it just for serialization purpose, and would like to avoid the cost of creating another DTO.

How can i serialize / unserialize Optional attributes? Optional doesn't have a default constructor (neither apache commons Pair), but i can't use the InstanceCreator, and don't really understand how to create a TypeAdapter that would simply delegate the serialization to the underlying Optional content.

解决方案

The solution by Ilya ignores type parameters, so it can't really work in the general case. My solution is rather complicated, because of the need to distinguish between null and Optional.absent() -- otherwise you could strip away the encapsulation as a list.

public class GsonOptionalDeserializer<T>
implements JsonSerializer<Optional<T>>, JsonDeserializer<Optional<T>> {

    @Override
    public Optional<T> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
            throws JsonParseException {
        final JsonArray asJsonArray = json.getAsJsonArray();
        final JsonElement jsonElement = asJsonArray.get(0);
        final T value = context.deserialize(jsonElement, ((ParameterizedType) typeOfT).getActualTypeArguments()[0]);
        return Optional.fromNullable(value);
    }

    @Override
    public JsonElement serialize(Optional<T> src, Type typeOfSrc, JsonSerializationContext context) {
        final JsonElement element = context.serialize(src.orNull());
        final JsonArray result = new JsonArray();
        result.add(element);
        return result;
    }
}

这篇关于如何序列化可选&lt; T&gt; Gson的课程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆