简体   繁体   English

Jersey / MOXy任意JSON属性

[英]Jersey/MOXy arbitrary JSON property

I have a bean with an arbitrary JsonValue property which I need to marshal/unmarshal from JSON. 我有一个带有任意JsonValue属性的bean,需要从JSON封送/取消封送。

public class MyBean {
    public String name;
    public JsonValue data;
}

Since JsonValue is the standard javax.json.JsonValue , I was expecting MOXy to marshal/unmarshal it out of the box, instead I got a plain string: 由于JsonValue是标准的javax.json.JsonValue ,因此我期望MOXy可以将其封送/解组,相反,我得到了一个纯字符串:

{
  "name": "foo",
  "data": "{\"some\":\"json\"}"
}

where I was expecting this: 我期待的地方:

{
  "name": "foo",
  "data": {
    "some": "json"
  }
}

When unmarshalling, data becomes null . 解组时, data变为null How can I use JsonValue s and get MOXy manage them like expected? 如何使用JsonValue并使MOXy像预期的那样对其进行管理?

(I need to stick with default Jersey/MOXy, so no use of other libraries.) (我需要使用默认的Jersey / MOXy,所以不要使用其他库。)

Note: this is not an answer (solution for the OP). 注意:这不是答案(OP解决方案)。 I typed it out before I read "I need to stick with default Jersey/MOXy" . 在输入“我需要坚持使用默认的Jersey / MOXy”之前,我已将其打出 Was going to change to a comment, but there's too much stuff. 本来打算更改为评论,但内容太多了。 I'll just leave it up for future readers. 我将其留给以后的读者使用。

There is a different provider for the javax.json classes. javax.json类有一个不同的提供程序 MOXy doesn't know how to handle them the way you expect. MOXy不知道如何以您期望的方式处理它们。 Without looking at any source code I would guess that what you are seeing is the value of toString() from the JsonValue instance. 如果不查看任何源代码,我会猜测您所看到的是JsonValue实例中toString()的值。 This is what will happen if the type can't be handled. 如果无法处理类型,则会发生这种情况。 You'll just get a toString() call. 您只会得到toString()调用。

Even if you add the dependency I linked to, you still have the problem that different providers don't interact with each other. 即使您添加了我链接到的依赖项,您仍然会遇到不同的提供程序无法相互交互的问题。 What would need to happen is that in the middle of MOXy serializing the MyBean instance, if it sees a javax.json object, it tries to look for a different provider to handle it. 将会发生的事情是,在MOXy序列化MyBean实例的中间,如果它看到一个javax.json对象,它将尝试寻找其他提供者来处理它。 It just doesn't work like that. 就是那样行不通。 Only one provider will be used. 将仅使用一个提供程序。

The only solution I can think of is to instead of using MOXy, use Jackson, which has a module that supports javax.json . 我能想到的唯一解决方案是使用Jackson支持的模块,而不是使用MOXy,该模块具有支持javax.json的模块。 If you register that module with Jackson, it will know how to handle the javax.json types, "mid-serialization". 如果您在Jackson上注册了该模块,它将知道如何处理javax.json类型的“ mid-serialization”。 You can have a look at this answer , which describes the step you should take to make it work with Jackson. 您可以看一下这个答案 ,它描述了与Jackson配合使用时应该采取的步骤。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM