开发者

SimpleXml framework - embedded collections

开发者 https://www.devze.com 2023-02-10 20:21 出处:网络
I try to serialize embedded collection using开发者_StackOverflow社区 simple. For example : Map<String, List<MyClass>>

I try to serialize embedded collection using开发者_StackOverflow社区 simple. For example :

Map<String, List<MyClass>>

I already added necessary annotations in MyClass, i tried with @ElementMap but it doesn't work: Exception in thread "main" org.simpleframework.xml.transform.TransformException: Transform of class java.util.ArrayList not supported

If its just

@ElementMap Map<String, MyClass>

it works fine. I don't know ho to deal with embedded collection. I know about @ElementList annotation but don't know how to use it in this case. Any hints?


I'm coming across the same issue. The only way I have managed to get it working has been a really cheesy hack - wrapping List in another class.

public class MyWrapper {

    @ElementList(name="data")
    private List<MyClass> data = new ArrayList<MyClass>();

    public MyWrapper(List<MyClass> data) {
        this.data = data;
    }

    public List<MyClass> getData() {
        return this.data;
    }

    public void setData(List<MyClass> data) {
        this.data = data;
    }

}

And then, instead of

@ElementMap Map<String,List<MyClass>>

...you'd have:

@ElementMap Map<String,MyWrapper>

In my case, the Map is entirely private to my class (i.e. other classes never get to talk directly to the Map), so the fact that I have this extra layer in here doesn't make much of a difference. The XML that is produced of course, is gross, but again, in my case, it's bearable because there is nothing outside of my class that is consuming it. Wish I had a better solution than this, but at the moment, I'm stumped.

0

精彩评论

暂无评论...
验证码 换一张
取 消