0

I have a generic (POJO-)container to share statistical data with clients:

@XmlRootElement
public class StatsSeries implements Serializable {

    private TreeMap<Date, Number> timeSeries;

    /* accessor methods */
}

Depending on the data, the server stores Long, Integer or Double in it, which is why I'm using abstract java.lang.Number.

Marshalling works fine, and hints indicating the concrete class are included in the data:

            "timeSeries": {
                "entry": [
                    {
                        "key": "2012-08-20T00:00:00Z", 
                        "value": {
                            "$": "24", 
                            "@type": "xs:long"
                        }
                    }, 
                    ....
                 ]
              }

Or in XML representation:

    <timeSeries>
      <entry>
        <key>2012-08-20T00:00:00Z</key>
        <value xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" xsi:type="xs:long">24</value>
      </entry>
      ...
    </timeSeries>

When attempting to unmarshall this, I get an javax.xml.bind.UnmarshalException: Unable to create an instance of java.lang.Number.

I saw this question, but it doesn't help me. How can I annotate java.lang.Number? Any other suggestions?

Update: Looking at JAXB-890, I understand that it should be fixed either on JDK 1.7 or using com.sun.xml.bind:jaxb-impl:2.2.6 -- neither works for me.

Community
  • 1
  • 1
Hank
  • 4,597
  • 5
  • 42
  • 84

1 Answers1

0

Not sure this will work given type-erasure, but you might try annotating with XmlElementRef

    @XmlElementRef
    private TreeMap<Date, Number> timeSeries;

"when this annotation is used, the XML element name is derived from the instance of the type of the JavaBean property at runtime".

Failing that, see if this link helps.

Community
  • 1
  • 1
Richard Sitze
  • 8,262
  • 3
  • 36
  • 48