创建一个类型意识到杰克逊解串器 [英] Create a type aware Jackson deserializer

查看:236
本文介绍了创建一个类型意识到杰克逊解串器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想配置采取不同取决于注解的字段的目标类型杰克逊解串器。

 公共类车{
    @JsonSerialize(使用= IdSerializer.class)
    @JsonDeserialize(使用= IdDeserializer.class)
    字符串ID
}公共类总线{
    @JsonSerialize(使用= IdSerializer.class)
    @JsonDeserialize(使用= IdDeserializer.class)
    的ID
}

杰克逊串行器知道从它所变换数据类型,因此这是工作

 公共类IdSerializer扩展JsonSerializer<对象> {
    @覆盖
    公共无效序列化(对象的值,JsonGenerator jsonGen,SerializerProvider提供商)抛出IOException        //值是注解的字段类
        如果(价值的instanceof字符串)
            jsonGen.writeObject(...);
        否则,如果(价值的instanceof标识)
            jsonGen.writeObject(...);
        其他
            抛出新抛出:IllegalArgumentException();
    }
}

杰克逊解串器似乎不知道目标类型在其中它会转换数据:

 公共类IdDeserializer扩展JsonDeserializer<对象> {
    @覆盖
    公共对象反序列化(JsonParser JP,DeserializationContext上下文)抛出IOException        //什么是注解的字段类?
    }
}


解决方案

在串行器,您可以添加有关,这将有助于你在反序列化类型的额外信息。

从建立你的贴IdSerializer ...

 公共类IdSerializer扩展JsonSerializer<对象> {    @覆盖
    公共无效序列化(对象的值,JsonGenerator jsonGen,SerializerProvider提供商)抛出IOException        //值是注解的字段类
        如果(价值的instanceof字符串){
            jsonGen.writeStartObject();
            jsonGen.writeFieldName(ID);
            jsonGen.writeObject(值);
            jsonGen.writeFieldName(输入);
            jsonGen.writeString(字符串);
            jsonGen.writeEndObject();
        }
        否则,如果(价值的instanceof标识){
            的ID =(ID)值;
            jsonGen.writeStartObject();
            jsonGen.writeFieldName(ID);
            jsonGen.writeString(id.getStuff());
            jsonGen.writeFieldName(输入);
            jsonGen.writeString(ID);
            jsonGen.writeEndObject();
        }
        其他{
            抛出新抛出:IllegalArgumentException();
        }
    }
}

在你解串器,您可以分析这种类型字段并返回正确的对象
类型。

I want to configure a Jackson deserializer that act differently depending on the target type of the annotated field.

public class Car {
    @JsonSerialize(using=IdSerializer.class)
    @JsonDeserialize(using=IdDeserializer.class)
    String id
}

public class Bus {
    @JsonSerialize(using=IdSerializer.class)
    @JsonDeserialize(using=IdDeserializer.class)
    Id id
}

Jackson serializers know the type from which it is converting data, so this is working:

public class IdSerializer extends JsonSerializer<Object> {
    @Override
    public void serialize(Object value, JsonGenerator jsonGen, SerializerProvider provider) throws IOException {

        // value is the annotated field class
        if(value instanceof String)
            jsonGen.writeObject(...);
        else if (value instanceof Id)
            jsonGen.writeObject(...);
        else 
            throw new IllegalArgumentException();
    }
}

Jackson deserializers seem to do not know the target type into which it will convert data:

public class IdDeserializer extends JsonDeserializer<Object> {
    @Override
    public Object deserialize(JsonParser jp, DeserializationContext context) throws IOException {

        // what is the annotated field class?
    }
}

解决方案

In the serializer, you could add extra information about the type that will help you during deserialization.

Building from your posted IdSerializer...

public class IdSerializer extends JsonSerializer<Object> {

    @Override
    public void serialize(Object value, JsonGenerator jsonGen, SerializerProvider provider) throws IOException {

        // value is the annotated field class
        if(value instanceof String){
            jsonGen.writeStartObject();
            jsonGen.writeFieldName("id");
            jsonGen.writeObject(value);
            jsonGen.writeFieldName("type");
            jsonGen.writeString("String");
            jsonGen.writeEndObject();
        }
        else if (value instanceof Id){
            Id id = (Id) value;
            jsonGen.writeStartObject();
            jsonGen.writeFieldName("id");
            jsonGen.writeString(id.getStuff());
            jsonGen.writeFieldName("type");
            jsonGen.writeString("Id");
            jsonGen.writeEndObject();
        }
        else{
            throw new IllegalArgumentException();
        }
    }
}

In your deserializer, you can parse this 'type' field and return an Object of the proper type.

这篇关于创建一个类型意识到杰克逊解串器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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