Am Donnerstag, 17. Juni 2021, 22:32:44 CEST schrieb Emilian Bold:
> Hello,
>
> I see the docs don't give much options on the subject:
> > If a field is removed from the class, deserializing an old version that
> > contains the field will cause an exception. Leaving the field in place
> > but declaring it as transient will avoid the exception, but XStream will
> > not try to deserialize it.
> ... but I can't believe it.
>
> So, I have class Parent which contains a list of class Children which
> contain a class Toy.
>
> It *may* seems that I can use ignoreUnknownElements or omitField but
> this basically only helps with serialization.
Unit tests do not lie:
> During de-serialization of old data, both methods will basically skip
> the whole XML subtree!
If you omit a member that contains a list, then yes, the list any stuff it includes will be omitted.
> What this means that if that if I have a reference to a 'Toy' object
> that was serialized as part of a now ignored element/subtree, that
> reference is never re-created. Later on, I will get an 'Invalid
> Reference' error when the object is referenced from an acceptable
> place.
Yes. What else? XStream has no longer any idea what the omitted field originally contained. Was it a String, a list or another complex type? There's no information left.
> So... is there really no way to just remove a field? Must one go back,
> re-add them and make them transient? And keep them forever?
You can write a custom converter and handle the tag yourself. Let XStream unmarshal it into a list and ignore the result.
> Ideally .omitField() would not serialize/deserialize that field. But
> it could (maybe with yet another flag) instantiate all the object
> subtree so that later references are valid.
As said, XStream ignores the member field of a type. It is no longer relevant, what that field originally contained. An enhancement to XStream would be possible though, if you could pass the deserialization type together woth the field to omit as parameter. However, the implementaion would do the same as you could do with that custom converter.
Regards,
Jörg
--
You received this message because you are subscribed to the Google Groups "XStream User" group.
To unsubscribe from this group and stop receiving emails from it, send an email to xstream-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/xstream-user/6348766.Wl00Ti0lHA%40floh.