After some further reading last night I wondered if the solution would be to using something other than the default ReflectionProvider but I'll have to take a look at the Converters tutorial to see what I can do. I hadn't considered writing a custom converter for the Hours type but that seems like that might be the best option.
I don't think that the use of the static placeholder would work for me without impacting too many other pieces of the code. -----Original Message----- From: Jörg Schaible [mailto:[email protected]] Sent: Tuesday, February 07, 2012 4:04 AM To: [email protected] Subject: [xstream-user] Re: Using a Converter that will handle null values Hi John, John Fereira wrote: > I have been using XStream for quite a while without a hitch but > recently was presented with a use case that I'm not sure how to resolve. > Essentially I have an Hours object that represents the opening and > closing hours of a location. The actual open/close fields are "Time" objects > that uses a Calendar instance on the back end. Up until recently the > open/closed Time objects were always instantiated to a non-null value. > However, for reasons that I don't necessarily agree with I've been > asked to set the open/closed Time objects to a null if a flag (another > field in the Hours object) has been set to "closed all day" or "open all day". > The issue that I'm having is that I want to serialize the Hours > object and marshall a "openTime" element (with the time set to null) even > when > the Time object is null. I've got a "TimeConverter" class that returns > true for the canConvert method (it is recognizing that a Time.class is > being handled) but it doesn't appear that the marshal method is being > called if the Time object is a null. As you already found out, this does not work. Most XStream converters will omit any null objects at all. The reason is that XStream has to separate between objects that may be represented by an empty string and null values itself. In your case I simply assume that Hour is handled by the ReflectionConverter that will not do anything for fields with null values. Therefore your TimeConverter is not called, because no XML node has been generated. The simplest solution is to write a custom converter for your Hour type. Then your free to write whatever you like into the XML representation for your null values. Have a look at the converter tutorial to see how you may forward the conversion for the individual fields of the Hour type to XStream again. Another approach is to use a constant static placeholder representing the NULL Time. You may identify that object in your TimeConverter and getter/setter of your Hour type must translate null into this placeholder. Regards, Jörg --------------------------------------------------------------------- To unsubscribe from this list, please visit: http://xircles.codehaus.org/manage_email
