XStream 编组返回 xml 和 json 时出现问题

发布于 2024-08-29 17:29:52 字数 1358 浏览 7 评论 0原文

当我使用

new XStream().toXml(someObject);

它时,它返回以下 xml...

<response>
        <status>SUCCESS</status>
        <isOwnershipVerified class="boolean">false</isOwnershipVerified>
</response>

并且,当我使用

new XStream(new JsonHierarchicalStreamDriver()).toXml(someObject);

它时,它返回以下 json...

{"response": {
  "status": "SUCCESS",
  "isOwnershipVerified": {
    "@class": "boolean""false"}
}}

现在,因为我想完全摆脱类属性(读取它不是为了将其与其他任何东西别名,而是将其删除)我使用以下代码。

    XStream xStream = new XStream();
    StringWriter writer = new StringWriter();
    xStream.marshal(this, new PrettyPrintWriter(writer) {
        @Override
        public void addAttribute(final String key, final String value)
        {
            if (!key.equals("class"))
            {
                super.addAttribute(key, value);
            }
        }
    });
    return writer.toString();

它给出了以下 xml...

<response>
        <status>SUCCESS</status>
        <isOwnershipVerified>false</isOwnershipVerified>
</response>

但是,当我在上面创建 xStream 对象时传递 new JsonHierarchicalStreamDriver() 时,它不会返回 json。它返回与上面所示相同的 xml。

这是怎么回事?

提前致谢...

When i use

new XStream().toXml(someObject);

it returns following xml...

<response>
        <status>SUCCESS</status>
        <isOwnershipVerified class="boolean">false</isOwnershipVerified>
</response>

and, when i use

new XStream(new JsonHierarchicalStreamDriver()).toXml(someObject);

it returns following json...

{"response": {
  "status": "SUCCESS",
  "isOwnershipVerified": {
    "@class": "boolean""false"}
}}

Now, since i want to get rid of class attribute altogether (read it not to alias it with anything else, but to remove it) i use following code.

    XStream xStream = new XStream();
    StringWriter writer = new StringWriter();
    xStream.marshal(this, new PrettyPrintWriter(writer) {
        @Override
        public void addAttribute(final String key, final String value)
        {
            if (!key.equals("class"))
            {
                super.addAttribute(key, value);
            }
        }
    });
    return writer.toString();

which gives follwing xml...

<response>
        <status>SUCCESS</status>
        <isOwnershipVerified>false</isOwnershipVerified>
</response>

but, when i pass new JsonHierarchicalStreamDriver() while xStream object creation above, it does NOT return json. it returns the same xml shown above.

What is wrong going on here?

Thanks in advance...

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

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

发布评论

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

评论(1

謸气贵蔟 2024-09-05 17:29:55

我想通了...

而不是使用

new PrettyPrintWriter(writer) ...

use

new JsonHierarchicalStreamWriter(writer)

并且事情将开始按预期工作...:D

i figured it out...

instead of using

new PrettyPrintWriter(writer) ...

use

new JsonHierarchicalStreamWriter(writer)

and the things will start working as expected... :D

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