为Jackson Mapper提供多种方法来反序列化同一个对象 [英] Providing Jackson Mapper multiple ways to deserialize the same object

查看:99
本文介绍了为Jackson Mapper提供多种方法来反序列化同一个对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试反序列化两种类型的json:

I'm trying to deserialize two types of json:

{
    name: "bob",
    worksAt: {
        name: "Bobs department store",
        location: "downtown"
    },
    age: 46
}

{
    name: "Tom",
    worksAt: "company:Bobs department store",
    age: 27
}

进入这些对象:

第一种方法创建两个新对象,第二种方法方式根据字符串的内容从数据库中请求对象。

The first way creates two new objects, the second way requests the object from the database based on the contents of a string.

有点像杰克逊映射器如何将任意字符串反序列化为对象,对于这样的对象:

sort of like how jackson mapper can deserialize an arbitrary string into an object, for objects like this:

public class Company{
    public String name;
    public Employee[] employees
    public Company(){}
    public Company(String json){
        //turn string into object using whatever encoding you want blah blah blah...
    }
}

问题是我需要两者。我需要它来处理对象和字符串。两者都可以从相同的输入到达。

The trouble is I need both. I need it to handle objects and strings. Both could arrive from the same input.

我试过的第一个想法是转换器

The first think I tried was making a Converter

它说这些创建委托类型以传递给反序列化器,但即使数据类型不是字符串,也始终应用转换器。所以这不起作用。

It says these create a delegate type to pass to the deserializer, but the converter is always applied even when the datatype isn't a string. So that didn't work.

我也尝试了一个普通的反序列化器,但我找不到一种方法来推迟BeanDeserializer。 beanDeserializer非常复杂,我无法手动实例化它。我也看不到延迟使用jackson mapper中的默认反序列化器。

I've also tried a normal deserializer, but I can't find a way to defer to the BeanDeserializer. The beanDeserializer is so complicated that I can't manually instantiate it. I also see no way to defer to a default deserializer in jackson mapper.

我是否必须重新实现jackson mappers反序列化才能执行此操作?解串器有没有办法说我不能这样做,使用默认实现。?

Do I have to re-implement jackson mappers deserialization to do this? Is there any way for a deserializer to say "I can't do this, use the default implementation."?

编辑:一些进一步的进展。基于Jackson Mapper源代码,看起来你可以像这样实例化bean反序列化器:

Some further progress. Based on the Jackson Mapper source code, it looks like you can instatiate bean deserializers like this:

DeserializationConfig config = ctxt.getConfig();
JavaType type = config.constructType(_valueClass);
BeanDescription introspect = config.introspect(type);
JsonDeserializer<Object> beanDeserializer = ctxt.getFactory().createBeanDeserializer(ctxt, type , introspect);

但由于某种原因,所有_beanProperties都为其_valueDeserializer设置了FailingDeserializer,整个过程失败了。所以我不知道为什么会这样......

but for some reason all the _beanProperties have the FailingDeserializer set for their _valueDeserializer and the whole thing fails. So I have no idea why that happens...

推荐答案

你试过编写自定义反序列化程序?这使您可以最大程度地控制杰克逊如何反序列化对象。您可以尝试以一种方式反序列化,如果出现错误,请尝试其他方式。

Have you tried writing a custom deserializer? This gives you the most control on how Jackson deserializes the object. You may be able to try to deserialize one way, and if there's an error, try another way.

Jackson也可以处理多态反序列化,虽然这需要对json进行少量更改以包含类型信息,听起来您的问题约束可能不允许这样做。

Jackson can also handle polymorphic deserialization, though this would require a small change to the json to include type information, and it sounds like your problem constraints might not allow that.

这篇关于为Jackson Mapper提供多种方法来反序列化同一个对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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