Re: [Simple-support] Element/Attribute Default Values
Brought to you by:
niallg
|
From: Niall G. <gal...@ya...> - 2007-06-12 09:37:07
|
Hi,
In future I hope to add an "empty" attribute to all of the annotations such that empty values can be inserted without having to bend over backwards to cope with null values. Ill be implementing something like
@Root
public class MyClass {
@Attribute(empty="NULL")
private String example;
}
Such that if the field "example" had a null value it would be written as follows:
<myClass example="NULL"/>
The deserialization process will know to substitute the specified value with null when reconstituting the object. For the @Element, @ElementList, and @ElementArray annotations the field will be a boolean like so:
@Root
public class MyOtherClass {
@ElementList(empty=true)
private Collection<String> list;
}
The above annotation tells the persister to serialize the empty value such that the result of having a null list would be:
<myOtherClass>
<list/>
</myOtherClass>
Where as if the annotation was set not to write an empty value the resulting XML would be:
<myOtherClass/>
This requires only very minor changes, so will probably make it in soon enough.
Niall
----- Original Message ----
From: Niall Gallagher <gal...@ya...>
To: Keith Byrne <ka...@gm...>; sim...@li...
Sent: Monday, June 11, 2007 10:38:52 PM
Subject: Re: [Simple-support] Element/Attribute Default Values
Hi,
Yes there is a way to intercept, for example:
public class MyExample {
private static final DEFAULT_VALUE = "[[NULL]]";
@Element
private String webAddress
@Commit
private void resolveMyDefault() {
if(webAddress.equals(DEFAULT_VALUE)) {
webAddress = null;
}
}
@Persist
private void fixMyObject() {
if(webAddress == null){
webAddress = DEFAULT_VALUE:
}
}
@Complete
private void backToNormal() {
resolveMyDefault();
}
}
This should do the trick, as @Persist gets called just before the object is persisted, and @Complete gets called when its been serialized to the XML document. The @Commit gets called after deserialization so the object values will be reverted to a proper working runtime value.
Hope this helps.
Niall
----- Original Message ----
From: Keith Byrne <ka...@gm...>
To: sim...@li...
Sent: Monday, June 11, 2007 10:33:16 AM
Subject: [Simple-support] Element/Attribute Default Values
I have a situation where a field isn't required, but when it's null and is serialized, the tag still needs to show up empty. Specifically, there is a String field (webAddress) that has an Attribute annotation, that went it's null
should be serialized to <webAddress/>, instead of not showing up. Is there a way to intercept the serializing of an Element/Attribute, and based upon it's type, serialize a default value?
-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
_______________________________________________
Simple-support mailing list
Sim...@li...
https://lists.sourceforge.net/lists/listinfo/simple-support
Shape Yahoo! in your own image.
Join our Network Research Panel today!
-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
_______________________________________________
Simple-support mailing list
Sim...@li...
https://lists.sourceforge.net/lists/listinfo/simple-support
____________________________________________________________________________________
Fussy? Opinionated? Impossible to please? Perfect. Join Yahoo!'s user panel and lay it on us. http://surveylink.yahoo.com/gmrs/yahoo_panel_invite.asp?a=7
|