users@jaxb.java.net

Re: JAXB Bug [?]: Unknown enum values are unmarhalled as NULLs

From: Saloucious <saloucious_at_gmail.com>
Date: Thu, 25 Sep 2008 13:14:05 -0700 (PDT)

I have annotating my Classes and I encoutered the same problem.

Actually I've a MyRootObject with a List of MyObject.

MyObject has an attribute : MyEnum (with ENUM1, ENUM2)...

When I run unmarshall method, everything works.

Unfortunately suppose with have an XML file like :

<MyRootObject>
   <MyObject>
      <MyEnum>ENUM1</MyEnum>
   </MyObject>
   <MyObject>
      <MyEnum>ENUM3</MyEnum>
   </MyObject>
</MyRootObject >

Unmarshall method will return a MyRootObject with a MyObject List composed
by 2 instances
0 ==> MyObject [ENUM1]
1 ==> MyObject [null]


JAXB could not find valueOf ENUM3 in MyEnum and simply ignore it.

Is it possible to had a listener to log this problem and prevent
MyObject[null] to be inserted in List.


I've added @XmlJavaTypeAdapter and i'm able to log unmarshalling problem in
my Adapter but can find how to prevent build object to be added in List.

Any ideas ?

Thanks.



Andy Malakov wrote:
>
> It seems that JAXB silently ignores incorrect values of enum types. The
> following short program demonstrates this issue. Instead of producing a
> parsing mistake like "Cannot parse 'WRONG' as a value of enum type
> 'Color'" JAXB unmarshaller simply sets Color to null.
>
>
> public class JAXBTest {
>
> @XmlRootElement
> public enum Color {
> Red, Green
> }
>
> public static void main(String[] args) throws Exception {
> JAXBContext context = JAXBContext.newInstance(Color.class);
>
> printColor(context, "<color>Red</color>");
> printColor(context, "<color>Green</color>");
> printColor(context, "<color>WRONG</color>");
> }
>
> private static void printColor(JAXBContext context, String xml)
> throws JAXBException {
> Unmarshaller um = context.createUnmarshaller();
> Color color = (Color) um.unmarshal(new StringReader(xml));
> System.out.println("Color:" + color);
> }
> }
>
> Best Regards,
> Andy Malakov
>

-- 
View this message in context: http://www.nabble.com/JAXB-Bug----%3A-Unknown-enum-values-are-unmarhalled-as-NULLs-tp15344297p19677405.html
Sent from the java.net - jaxb users mailing list archive at Nabble.com.