XML : EclipseLink JAXB (MOXy) unmarshalling map with Serializable value fails to convert

Having a Map inside an object that gets marshalled and unmarshalled using EclipseLink JAXB (MOXy), I see the following behavior: If the Map-values are of type Object, everything is fine. If the Map-values are of type Serializable they do not get converted correctly (i.e. Boolean gets converted to String).

Here is a short self-contained example that demonstrates the problem (the test fails using EclipseLink 2.4.2):

  @XmlRootElement  @XmlAccessorType(XmlAccessType.FIELD)  public static class MyMapping {        @XmlElement      private final TreeMap<String, Serializable> map = new TreeMap<String, Serializable>();        public void put(String key, Serializable value) {          map.put(key, value);      }        public Object getValue(String key) {          return map.get(key);      }  }    @Test  public void boolean_criteria_should_become_boolean_after_reload() throws Exception {      String key = "key";      MyMapping origMapping = new MyMapping();      origMapping.put(key, Boolean.TRUE);      String xml = toXML(origMapping);      System.out.println(xml);      MyMapping readMapping = fromXML(xml);      Assert.assertEquals(origMapping.getValue(key), readMapping.getValue(key));  }    public String toXML(Object obj) throws Exception {      ByteArrayOutputStream baos = new ByteArrayOutputStream();      PrintStream out = new PrintStream(baos);      JAXBContext jc = createJAXBContext();      Marshaller marshaller = jc.createMarshaller();      marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);      marshaller.marshal(obj, out);      return baos.toString();  }    @SuppressWarnings("unchecked")  public <T> T fromXML(String xml) throws Exception {      JAXBContext jc = createJAXBContext();      Unmarshaller unmarshaller = jc.createUnmarshaller();      return (T) unmarshaller.unmarshal(new ByteArrayInputStream(xml.getBytes()));  }    private JAXBContext createJAXBContext() throws JAXBException {      // EclipseLink JAXB (MOXy)      return JAXBContextFactory.createContext(new Class<?>[] { MyMapping.class }, Collections.emptyMap());      // return JAXBContext.newInstance(MyMapping.class);  }    

To make this test pass, simply change declaration from TreeMap<String, Serializable> to TreeMap<String, Object>.

Anyone any ideas on this? Why doesn't it work with Serializable? How can I make it work? Is this a bug?

No comments:

Post a Comment