显示对象的 null -JSON- JAXB

发布于 2024-09-27 09:21:48 字数 719 浏览 0 评论 0原文

我想将 null 对象封送为 JSON 表示形式中的 null。 但是,现在,如果对象为 null,我就看不到 JSON 中的元素。

Example:
@XmlAccessType(FIELD)
@XmlType(name="foo" propOrder={"foo"}
class foo{
@XmlElement
      private Integer foo;
      private Integer another_foo;
..

getter()
setter()

}

在我的代码中,将 foo 元素设置为 null。

但 JSON 表示形式不会显示响应中的元素。

响应看起来像这样

"foo" :{
 "another_foo":something
}

我尝试将 xml 元素属性 nillable 设置为 true。 (@XmlElement(nillable=true)

这使得响应看起来像,

"foo" :{
 "another_foo" : something
 "foo":{nil :true}
}

我希望它是这样的,

    "foo" :{
     "another_foo":something
     "foo" : null
    }

这里做错了什么?

I want to marshal null objects as null in the JSON representation.
But, right now, Am not seeing the element in the JSON if the object is null.

Example:
@XmlAccessType(FIELD)
@XmlType(name="foo" propOrder={"foo"}
class foo{
@XmlElement
      private Integer foo;
      private Integer another_foo;
..

getter()
setter()

}

In my code am setting foo element to null.

But the JSON representation does not show the element in the response.

The response looks like this

"foo" :{
 "another_foo":something
}

I tried setting xml element attribute nillable true. (@XmlElement(nillable=true)

Which makes the response look like,

"foo" :{
 "another_foo" : something
 "foo":{nil :true}
}

I want it to be like,

    "foo" :{
     "another_foo":something
     "foo" : null
    }

What am doing wrong here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

千纸鹤 2024-10-04 09:21:48

首先,JAXB 不支持 JSON。它是一个 XML API。所以你可能正在使用一个框架(我的猜测:JAX-RS 实现,比如 Jersey)?有必要知道您正在使用哪一种来提供更多帮助。

假设这样,问题是,包如何使用 JAXB 注释来指导 JSON 序列化。有些基本上将对象转换为 XML(逻辑结构或完整 xml),然后使用约定转换为 JSON。由于 XML 和 JSON 之间的数据模型不同,这可能会导致数据丢失。

现在:大多数 JAX-RS 实现的简单解决方案是根本不使用基于 JAXB 注释的方法,而是使用特定于 JSON 的序列化程序,例如 Jackson 的 JacksonJsonProvider(Jackson 实际上也可以使用 JAXB 注释)。默认情况下,它将包含属性的空值,尽管这是可配置的,以防您想要抑制空值。

这是 JavaDoc展示了如何将 Jackson 提供程序(指定 FEATURE_POJO_MAPPING 进行配置)与 Jersey 一起使用,如果这可能有帮助的话。

First things first: JAXB does NOT do JSON. It is an XML API. So you are probably using a framework (my guess: JAX-RS implementation like maybe Jersey)? It is necessary to know which one you are using to give more help.

Assuming this, question is, how is the package using JAXB annotations to guide JSON serialization. Some basically convert objects to XML (either logical structure, or full xml), and then convert to JSON using a convention. This may cause data loss because of differences in data model between XML and JSON.

Now: simple solution for most JAX-RS implementations is to not use JAXB annotation based approach at all, but a JSON-specific serializer, such as Jackson's JacksonJsonProvider (Jackson can actually use JAXB annotations, too). It will by default include null values for properties, although this is configurable in case you want to suppress nulls.

Here is JavaDoc that shows how to use Jackson provider (specify FEATURE_POJO_MAPPING for configuration) with Jersey, if that might help.

独自唱情﹋歌 2024-10-04 09:21:48

我认为 json 的功能就是这样,它不会显示 Json 字符串中的空值。假设一个例子,您有一个具有两个属性的对象,即年龄和姓名。现在,如果年龄为 7 并且 name 为 null,那么如果根据您的说法,name 也应该包含在 JSON 对象中,那么您必须纠正代码以显式处理相同代码的反序列化。那么你将必须处理 null 值,因为 JSON 会将“null”视为字符串并将其分配给对象,这将使你的对象具有 name="null",这是错误的。

如果您想实现相同的目标,那么您必须扩展类 JSON 对象并编写自己的实现。

i think the functionality of json is like that only, it will not show you null values in the Json string. Suppose an example, you have an object with two attributes say age and name. Now if the age is 7 and name is null, then if according to you, name should also be included in the JSON object then, u must right the code to explicit handle the deserialization of the same code. then you will have to handle the null value thing as JSON will treat "null" as a string and will assign it to the object which will make your object with name="null", which is wrong.

If you want to achieve the same, then you must extend the class JSON object and write your own implementation.

海夕 2024-10-04 09:21:48

注意:我是EclipseLink JAXB (MOXy) JAXB (JSR-222)< 的领导者和成员/strong>专家组。

EclipseLink JAXB (MOXy) 提供对 JSON 的本机支持。下面是如何利用 @XmlElement 注释来映射您的用例的示例:

Foo

默认情况下,JAXB 实现不会封送 null 属性。您可以通过将 @XmlElement 上的 nillable 属性设置为 true 来更改此行为。

package forum3938279;

import javax.xml.bind.annotation.*;

@XmlAccessorType(XmlAccessType.FIELD)
class Foo {
      @XmlElement(nillable=true)
      private Integer foo;
      private Integer another_foo;

}

jaxb.properties

要将 MOXy 指定为您的 JAXB 提供程序,您必须在与域模型相同的包中包含一个名为 jaxb.properties 的文件,其中包含以下条目:

javax.xml.bind.context.factory = org.eclipse.persistence.jaxb.JAXBContextFactory

Demo

package forum3938279;

import javax.xml.bind.*;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Foo.class);

        Foo foo = new Foo();

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.setProperty("eclipselink.media-type", "application/json");
        marshaller.marshal(foo, System.out);
    }

}

输出

下面是运行演示代码的输出。两个字段都有空值。不带 @XmlElement 注释的字段未编组,带 @XmlElement(nillable=true) 的字段按 JSON 中的预期进行编组。

{
   "foo" : null
}

了解更多信息

Note: I'm the EclipseLink JAXB (MOXy) lead and member of the JAXB (JSR-222) expert group.

EclipseLink JAXB (MOXy) offers native support for JSON. Below is an example of how the @XmlElement annotation can be leveraged to map your use case:

Foo

By default a JAXB implementation will not marshal a null property. You can change this behaviour by setting the nillable property on @XmlElement to true.

package forum3938279;

import javax.xml.bind.annotation.*;

@XmlAccessorType(XmlAccessType.FIELD)
class Foo {
      @XmlElement(nillable=true)
      private Integer foo;
      private Integer another_foo;

}

jaxb.properties

To specify MOXy as your JAXB provider you must include a file called jaxb.properties in the same package as your domain model with the following entry:

javax.xml.bind.context.factory = org.eclipse.persistence.jaxb.JAXBContextFactory

Demo

package forum3938279;

import javax.xml.bind.*;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Foo.class);

        Foo foo = new Foo();

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.setProperty("eclipselink.media-type", "application/json");
        marshaller.marshal(foo, System.out);
    }

}

Output

Below is the output from running the demo code. Both fields had null values. The field without the @XmlElement annotation was not marshalled, and the one with @XmlElement(nillable=true) was marshalled as expected in JSON.

{
   "foo" : null
}

For More Information

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文