XStream:如何在自定义转换器中编组/解组列表?

发布于 2024-09-25 13:21:48 字数 548 浏览 0 评论 0原文

我有以下使用自定义转换器序列化的类(旧版;不可注释):

class Test {

    // some other variables

    List<SomeType> someTypeList;

}

已经可以使用 SomeType 正常工作的转换器。但是我希望序列化列表,就像用 @XStreamAlias("someTypes") 注释一样。

最后,我期望 someTypeList 具有以下格式:

<someTypes class="list-type">
    <someType>
        ....
    </someType>
    ...
</someTypes>

如何实现 marshal/unmarshal 方法才能获得所需的输出?调用 context.convertAnother(someTypeList) 不会产生预期结果,因为周围的 标记丢失。

I have the following class (legacy; not annotatable) that is serialized with a custom converter:

class Test {

    // some other variables

    List<SomeType> someTypeList;

}

A properly working converter for SomeType is already available. However I want the list to be serialized as if it was annotated with @XStreamAlias("someTypes").

In the end I expect the following format for someTypeList:

<someTypes class="list-type">
    <someType>
        ....
    </someType>
    ...
</someTypes>

How do I have to implement the marshal/unmarshal method to get the desired output? Calling context.convertAnother(someTypeList) didn't yield the expected result as the surrounding <someTypes> tag was missing.

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

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

发布评论

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

评论(2

阳光的暖冬 2024-10-02 13:21:48

明智的做法是获取结构:

<someTypes class="list-type">  
<someType>  
....  
</someType>  
...  
</someTypes>

看下面的代码。对于您的列表,您需要标记:

@XStreamImplicit(itemFieldName="someType")  
List<someType>List;

现在,根据您内部的内容,您可能需要创建一个自定义转换器。要引用它,您可以像这样进行更改:

@XStreamImplicit(itemFieldName="someType")  @XStreamConverter(YourOwnConverter.class)  
List<SomeType> someTypeList;

然后创建一个转换器类(YourOwnConverter),该类将知道如何取消/封送:

public boolean canConvert(Class type) 
{
    return type.equals(SomeType.class);
}

public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) 
{
    SomeType mytype = (SomeType) source;
    writer.addAttribute("position", mytype.getPosition());
    writer.setValue(mytype.getId());
}
public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) 
{
    SomeType mytype = new SomeType();
    String position =  reader.getAttribute("position");
    ......  
        return mytype ;
}

使用此示例:
http://x-stream.github.io/converter-tutorial.html

You wise to get the structure:

<someTypes class="list-type">  
<someType>  
....  
</someType>  
...  
</someTypes>

Look at the following code. For your list you need to tag:

@XStreamImplicit(itemFieldName="someType")  
List<someType>List;

Now, depending on what you got inside, you might need to create a custom converter. To refer to that you change a bit like this:

@XStreamImplicit(itemFieldName="someType")  @XStreamConverter(YourOwnConverter.class)  
List<SomeType> someTypeList;

Then create a converter class (YourOwnConverter) that would know how to un/marshal:

public boolean canConvert(Class type) 
{
    return type.equals(SomeType.class);
}

public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) 
{
    SomeType mytype = (SomeType) source;
    writer.addAttribute("position", mytype.getPosition());
    writer.setValue(mytype.getId());
}
public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) 
{
    SomeType mytype = new SomeType();
    String position =  reader.getAttribute("position");
    ......  
        return mytype ;
}

Use this as example:
http://x-stream.github.io/converter-tutorial.html

七度光 2024-10-02 13:21:48

在配置期间是否有在 xstream 对象上调用的 addImplicitCollection 导致跳过 someTypes 标记?

Is there a addImplicitCollection called on the xstream object during configuration somewhere which causes the someTypes tag to be skipped ?

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