泽西岛解析JSON / Jackson子类型反序列化的规则 [英] Rules for Jersey to parse JSON/ Jackson Subtype deserialisation

查看:130
本文介绍了泽西岛解析JSON / Jackson子类型反序列化的规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到JSON的方式:

I receive JSONs the way:

@POST
@Path("log")
public Map<String, List<OperationResult>> log(Stats stats) {
  ..
}

JSON的示例:

{
  "eventType": 1
  "params": {
    "field1" : 10
  }
}

{
  "eventType": 2
  "params": {
    "field2" : "ten"
  }
}

我有一个类结构(它们是由jsonschema2pojo,假设没关系):

I have a class structure (they are generated by jsonschema2pojo, suppose it does not matter):

interface Params;
class Params1 implements Params{ public int field1; }
class Params2 implements Params{ public String field2; }

class Stats {
  public int eventType;
  public Params params;
}

如何让Jersey解析JSON,以便在eventType = 1时使用stats .params成为Params1和Params2的一个实例?

How can I make Jersey to parse JSONs so that if eventType = 1 then stats.params becomes an instance of Params1 and else of Params2?

推荐答案

今天早上我花了一些时间来解决这个问题。一个有趣的用例。我想出了怎么做,但我不得不稍微改变你的json。这不是绝对必要的,但是类型转换不是您的问题的一部分,因此我们可以在需要时进行跟进:)

I spent some time to work this out this morning. An interesting usecase. I figured out how to do that, but I had to change your json slightly. This is not strictly necessary, however type-conversion wasn't partof your question, so we can do a follow up if needed :)

您的json:

artur@pandaadb:~/tmp/test$ cat 1.json 
{
  "eventType": "1",
  "params": {
    "field1" : 10
  }
}
artur@pandaadb:~/tmp/test$ cat 2.json 
{
  "eventType": "2",
  "params": {
    "field2" : "10"
  }
}

我正在使用这两个文件来执行请求。请注意,我将eventType更改为String而不是数字。我稍后会指出这一点。

I am using those 2 files to do the request. Note that I changed the eventType t be a String rather than a number. I will point that out later.

您的模型对象:

public class Stats {

    @JsonProperty
    int eventType;


    public Params params;

    @JsonTypeInfo(use=JsonTypeInfo.Id.NAME, include=JsonTypeInfo.As.EXTERNAL_PROPERTY, property="eventType")
    @JsonSubTypes({ @Type(value = Param1.class, name = "1"), @Type(value = Param2.class, name = "2") })
    public void setParams(Params params) {
        this.params = params;
    }
}

我正在使用JsonTypeInfo,这是这样做的:

I am using JsonTypeInfo, here's what this does:

JsonTypeInfo.Id.NAME 

逻辑类型名称,在您的情况下,它是属性eventType

logical type name, in your case it is the property "eventType"

JsonTypeInfo.As.EXTERNAL_PROPERTY 

表示外部属性用于反序列化上下文。 只能在属性上使用它,而不能作为Params本身的类注释。这就是为什么我注释setter方法而不是接口类。

Means that an external property is used for deserialisation context. You can only use that on the property, not as a class annotation on Params itself. That is why I annotate the setter method instead of the interface class.

property="eventType" 

简单告诉jackson要使用的属性名称

Simply tells jackson what property name to use

然后在JsonSubTypes中我注释可能的选项,在你的情况2:

Then in the JsonSubTypes I annotate the options that are possible, in your case 2:

@Type(value = Param1.class, name = "1") 

这告诉jackson使用Param1.class以防eventType属性为1

this tells jackson to use Param1.class in case the eventType property is "1"

相应的对于PAram2.class也是一样的,属性值是2

Accordingly the same for PAram2.class and the property value being "2"

注意
这就是我稍微改变了json的原因。子类型注释不能将整数作为属性。现在您可以使用不同的选项,例如TypeConverters在运行时将您的整数属性转换为字符串,这样您就可以保持json相同。我跳过了这一步,快速谷歌会给你如何做到这一点的说明。

NOTE This is why I changed the json slightly. The subtype annotations can not take an integer as property. Now there are different options you could be using, e.g. TypeConverters that convert your integer property into a string at runtime, and that way you can keep your json the same. I skipped that step, a quick google will give you instructions on how to do that though.

现在你的参数模型看起来像这样:

Now your parameter model looks like that:

public interface Params {

    public static class Param1 implements Params {
        @JsonProperty
        int field1;
    }

    public static class Param2 implements Params {

        @JsonProperty
        String field2;
    }

}

我正在注释杰克逊的房产知道要对那些进行反序列化。

I am annotating the properties so Jackson knows to deserialise those.

注意我有一点问题,因为有两个属性让我懒惰的眼睛看起来一样:

NOTE I had a bit of an issue because there are two properties that to my lazy tired eyes look the same:

JsonTypeInfo.As.EXTERNAL_PROPERTY
JsonTypeInfo.As.EXISTING_PROPERTY

你不能使用现有:D这需要花费十分钟才能搞清楚。有趣的事实,我上面有两行,并且一直在评论一个,没有理解为什么他们中的一个在另一个工作时抛出异常。

You can not use EXISTING :D This literally took ten minutes to figure out. Fun fact, I had both lines above and kept commenting one out, not getting why on earth one of them is throwing an exception while the other works.

无论如何。

最后测试:

artur@pandaadb:~/tmp/test$ curl -XPOST  "localhost:8085/api/v2/test" -d @1.json -H "Accept: application/json" -H "Content-Type: application/json"
io.gomedia.resource.Params$Param1
artur@pandaadb:~/tmp/test$ 
artur@pandaadb:~/tmp/test$ curl -XPOST  "localhost:8085/api/v2/test" -d @2.json -H "Accept: application/json" -H "Content-Type: application/json"
io.gomedia.resource.Params$Param2

请注意,资源正在打印实例化类的名称。正如您所看到的,两个json已被反序列化为正确的实例类。

Note that the resource is printing the name of the instantiated class. As you can see both json have been deserialised into the correct instance class.

我希望有所帮助:)

Artur

(有趣的事实#2:在我的回答中,我也使用了EXISTING而不是EXTERNAL,只是没有看到它。我可能需要让杰克逊为了我的理智而改变他们的名字)

(Fun fact #2: In my answer I also used EXISTING and not EXTERNAL and just didn't see it. I might need to ask jackson to change their names for my sanity's sake)

编辑

我刚尝试过,杰克逊很聪明,可以为你转换你的json。因此,您可以按原样保留json,并将模型中的属性作为String(如图所示)。一切正常。

I just tried it, and Jackson is smart enough to convert your json for you. So, you can leave the json as is, and simply have the property in your model as a String (as demonstrated). Everything works fine.

为了完整性,如果你想要一个转换器(因为你可能需要将你的字符串模型转换回一个整数来进行serailisation),这将是一个整数 - to-string转换器:

For completeness though, in case you want a converter (because you might need that to convert your string model back into an integer for serailisation), this would be a integer-to-string converter:

public class EventTypeConverter implements Converter<Integer, String>{

    @Override
    public String convert(Integer value) {
        return String.valueOf(value);
    }

    @Override
    public JavaType getInputType(TypeFactory typeFactory) {
        return SimpleType.construct(Integer.class);
    }

    @Override
    public JavaType getOutputType(TypeFactory typeFactory) {
        return SimpleType.construct(String.class);
    }

}

您可以通过以下方式使用它:

You can use it by doing:

@JsonProperty
@JsonDeserialize(converter=EventTypeConverter.class)
String eventType;

这篇关于泽西岛解析JSON / Jackson子类型反序列化的规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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