Is there a way to create a converter or some operation that is performed after every single conversion? For context, I am trying to populate default values for fields that are not in my XML in order to maintain backwards compatibility if my data model changes. For instance, if I had this object:
class A {
private String b;
private String c;
private String d;
}
and my XML was something like:
<a>
<b>b</b>
<d>d</d>
</a>
I want my import of the XML to know that there is a default value for the field c
that is "c"
and set it on A
as such. This should be a generic operation to which I can add defaults to any field of a very complex graph. If there were some way to trigger a function after every conversion, it could check the current object against a map of objects I'd like to set a default value on.
Also note, that using readResolve/readObject does not seem to be an option since 1. readObject() never seemed to work for me at all and 2. readResolve would overwrite the field with the default value even if it were actually included in the XML. Please let me know if my assumptions here are wrong though.
Edit:: I found this related thread on the user mailing list: http://article.gmane.org/gmane.comp.java.xstream.user/4619/match=default+value
and it seems like the only suggested solution is to use readResolve()
which I already said was not a valid solution.
readResolve()
method to handle this case.if (c==null) {c="c";}
I frequently use code like that and it works well. – Unterwalden