11

I try to serialize embedded collection using 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?

1 Answer 1

11

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for answer! I thought about it too:) I managed to serialize such object using my own converter which implements org.simpleframework.xml.convert.Converter interface.but I'm not happy on both solutions. I expect there is simpler way to do it out of the box. We are talking just about serializing java collections, it's not a rocket science:)
Just tried my own converter on your suggestion, I actually think it's the better (even if not ideal) option of the two. At least with the converter I'm not polluting my domain objects with structures that are only there to make the serializer happy :p

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.