Re: [Simple-support] Default converter & default values
Brought to you by:
niallg
|
From: Niall G. - Y. <Nia...@yi...> - 2014-02-12 22:15:42
|
There is no DefaultConverter nor is there ever likely to be, one thing you can do with a converter which may help is you can give it a reference to the Serializer and push through objects again with the outputnode or inputnode. There are examples in the test cases of this. There is also an empty attribute on @Text and @Attribute that can set defaults.
/**
* This is used to provide a default value for the attribute if
* the annotated field or method is null. This ensures the the
* serialization process writes the attribute with a value even
* if the value is null, and allows deserialization to determine
* whether the value within the object was null or not.
*
* @return this returns the default attribute value to use
*/
String empty() default "";
From: Paul Johnston [mailto:pa...@pa...]
Sent: 13 February 2014 07:52
To: sim...@li...
Subject: [Simple-support] Default converter & default values
Hi,
Thanks for the tips on JSON. I haven't looked into them just yet; will do shortly. In the meantime, I have a couple more questions:
1) Is there a DefaultConverter I can inherit from?
When writing a Converter I often find I only want to explicitly set an attribute or two, and would otherwise like to use the default conversion behaviour. It would be helpful if I could subclass something like DefaultConverter and call super.write() when I need.
I'm aware that I can kind of get this behaviour using a Strategy - in fact, that's what I'm doing now. But I'd prefer to use a Converter because then I can keep serialisation logic with the class that is being serialised, rather than having a central serialisation class.
2) Is there a way to have default values?
I have a few classes with a number of boolean attributes. 95% of the time these fields are at their default value and it would make the XML much more concise to omit them. The semantics I'd like are that when writing, the node is only generated if the value does not match the default. And when reading, if the node is not present, then the default is used.
Any hints appreciated,
Paul
|