xstream icon indicating copy to clipboard operation
xstream copied to clipboard

XStream and JavaFX Observable Collection Classes

Open MichaelEllis opened this issue 9 years ago • 4 comments

I am trying to use XStream to Internalize/Externalize some classes that rely on JavaFX Properties and JavaFX Observable collections.

I have been using XStreamFX but have run into a problem with the deserialisation for ObservableListWrapper which seems to fail due is not having a no argument constructor.

I have reported the problem as an XStreamFX Issue but am wondering if XStream will add support for these commonly used FX data classes?

MichaelEllis avatar Jun 18 '15 18:06 MichaelEllis

I was not aware of XStreamFX at all and you're actually the first asking for JavaFX support here at XStream. However, there's currently no one here who knows JavaFX for real. So, yes, in principle it wold be possible to create a new xstream artifact with JavaFX support, but the code must be donated with proper license and valid unit tests (that's how xstream-hibernate came into existence).

XStreamFX's ObservableListConverter is flawed, because it assumes it can handle any type implementing the ObservableList interface. A similar situation exists for XStream handling Collection types and XStream's CollectionConverter does not handle on purpose arbitrary Collection types (see #10). You will need an own converter for ObjectListWrapper.

joehni avatar Jun 18 '15 20:06 joehni

@joehni Thanks for the reply. I have experimented with creating my own ObservableListConverter as follows

public class ObservableListConverter extends CollectionConverter implements Converter {

    public ObservableListConverter(Mapper mapper) {
        super(mapper);
    }

    @Override
    public boolean canConvert(Class type) {
        return ObservableList.class.isAssignableFrom(type);
    }

    @Override
    protected Object createCollection(Class type) {
        if (type == ObservableListWrapper.class) {
            return FXCollections.<Person>observableArrayList();
        }
        return super.createCollection(type);
    }
}

And this works so long as the collection is not implicit i.e. annotated with @XStreamImplicit. Can you offer any advice as how I could handle @XStreamImplicit?

MichaelEllis avatar Jun 19 '15 08:06 MichaelEllis

Have you read about it in the FAQ?

joehni avatar Jun 21 '15 16:06 joehni

+1 I would also like this.

LiSongMWO avatar Jan 30 '16 11:01 LiSongMWO