[Simple-support] How to handle an empty XML element, when using a Converter via "@Convert"
Brought to you by:
niallg
|
From: Timo R. <tim...@di...> - 2013-02-26 17:58:05
|
Hello,
I have the following situation: I'm using the @Convert annotation with my
own converter implementation on a class field named "time".
When I now try to deserialize XML, which contains that "time" element but is
empty like this:
----
<time></time>
----
my converter will be called and the SimpleXML framework throws an exception:
----
[...]
Caused by: java.lang.NullPointerException
at org.simpleframework.xml.convert.Reference.getType(Reference.java:70)
----
This is because my Converter implementation returns "null", when the XML
element is empty. As the javadoc for "Converter.read" tells, it is not
allowed to return "null".
But now my question is, how should I handle the situation, where I use a
Converter which is "confronted" with an empty XML element?
Looking at the Method
"org.simpleframework.xml.convert.AnnotationStrategy.read(Type,
NodeMap<InputNode>, Value)"
We can see the following code snipped:
----
if(converter != null) {
Object data = converter.read(parent);
if(value != null) {
value.setValue(data);
}
return new Reference(value, data);
}
----
This means, that if there is a @Converter defined, it gets called in any
case, even if the value of that XML element is empty/null. But at the same
time, Converter.read(...) should not return "null".
I think the "AnnotationStrategy" class should handle this situation,
checking for an empty XML element, so the converter gets not called in that
case.
So, what is the correct way to handle such a situation, or is it a
"drawback" (maybe even a bug) of the "AnnotationStrategy" class?
Thanks a lot for your help!
Best Regards,
Timo
|