users@jersey.java.net

Re: [Jersey] Unmarshalled JSON not populating original class is was marshalled from.

From: Paul Taylor <paul_t100_at_fastmail.fm>
Date: Tue, 05 Jan 2010 20:03:39 +0000

Jakub Podlesak wrote:
> On Tue, Jan 05, 2010 at 07:04:58PM +0000, Paul Taylor wrote:
>
>> Jakub Podlesak wrote:
>>
>>> Hi Paul,
>>>
>>> could you please share your JAXB beans? Without that it is hard to tell
>>> what is going wrong in your case.
>>> The best would be to file a new bug report and attach a reproducible
>>> test case as a maven project. If that is not possible, could you please
>>> just send the JAXB bean sources and an example code populating
>>> the marshalled instance?
>>>
>>> Thanks,
>>>
>>> ~Jakub
>>>
>>>
>> Ok, I'll try but it it always take some time to get nicely packaged test
>>
>
> that is why i suggested you send at least the beans source code,
>
>
>> cases, but I take it from your answer that this should normally work
>> then , i.e Bean - JSON - Bean
>>
>
> yes, this should work and it should work even for inheritance structures,
> i.e. ChildBean extends ParentBean scenarios, where you serialize ParentBean
> containing ChildBean instance, but then you
> need to preserve the "root tag" JSON equivalent (rootUnwrapping==false) and sometimes
> you need to support namespaces.
>
> You might want to try at your side with JSON config built with st. like
> JSONConfiguration.natural().rootUnwrapping(false).build()
> and then let me know.
>
> Anyway, sending the JAXB beans source would help,
>
> ~Jakub
>
>
OK, the JAXB classes are

http://bugs.musicbrainz.org/browser/mmd-schema/trunk/brainz-mmd2-jaxb/src/main/java/org/musicbrainz/mmd2/Artist.java
and
http://bugs.musicbrainz.org/browser/mmd-schema/trunk/brainz-mmd2-jaxb/src/main/java/org/musicbrainz/mmd2/LifeSpan.java

I tried changing my JSONContext building as you described, but now when
I tried to unmarshall the String with

JSONUnmarshaller m =
DatabaseIndex.initJsonContext().createJSONUnmarshaller();
Artist artist = m.unmarshalFromJSON(new
StringReader(artistJson),Artist.class);

I get

javax.xml.bind.UnmarshalException
 - with linked exception:
[javax.xml.bind.UnmarshalException: unexpected element (uri:"",
local:"artist"). Expected elements are
<{http://musicbrainz.org/ns/mmd-2.0#}alias>,<{http://musicbrainz.org/ns/mmd-2.0#}alias-list>,<{http://musicbrainz.org/ns/mmd-2.0#}annotation>,<{http://musicbrainz.org/ns/mmd-2.0#}annotation-list>,<{http://musicbrainz.org/ns/mmd-2.0#}artist>,<{http://musicbrainz.org/ns/mmd-2.0#}artist-credit>,<{http://musicbrainz.org/ns/mmd-2.0#}artist-list>,<{http://musicbrainz.org/ns/mmd-2.0#}asin>,<{http://musicbrainz.org/ns/mmd-2.0#}attributes>,<{http://musicbrainz.org/ns/mmd-2.0#}barcode>,<{http://musicbrainz.org/ns/mmd-2.0#}begin>,<{http://musicbrainz.org/ns/mmd-2.0#}catalog-number>,<{http://musicbrainz.org/ns/mmd-2.0#}category>,<{http://musicbrainz.org/ns/mmd-2.0#}cdstub>,<{http://musicbrainz.org/ns/mmd-2.0#}cdstub-list>,<{http://musicbrainz.org/ns/mmd-2.0#}comment>,<{http://musicbrainz.org/ns/mmd-2.0#}date>,<{http://musicbrainz.org/ns/mmd-2.0#}direction>,<{http://musicbrainz.org/ns/mmd-2.0#}disambiguation>,<{http://musicbrainz.org/ns/mmd-2.0#}disc>,<{http://musicbrainz.org/ns/mmd-2.0#}disc-list>,<{http://musicbrainz.org/ns/mmd-2.0#}end>,<{http://musicbrainz.org/ns/mmd-2.0#}entity>,<{http://musicbrainz.org/ns/mmd-2.0#}format>,<{http://musicbrainz.org/ns/mmd-2.0#}freedb-disc>,<{http://musicbrainz.org/ns/mmd-2.0#}freedb-disc-list>,<{http://musicbrainz.org/ns/mmd-2.0#}gender>,<{http://musicbrainz.org/ns/mmd-2.0#}isrc>,<{http://musicbrainz.org/ns/mmd-2.0#}isrc-list>,<{http://musicbrainz.org/ns/mmd-2.0#}iswc>,<{http://musicbrainz.org/ns/mmd-2.0#}label>,<{http://musicbrainz.org/ns/mmd-2.0#}label-code>,<{http://musicbrainz.org/ns/mmd-2.0#}label-info>,<{http://musicbrainz.org/ns/mmd-2.0#}label-info-list>,<{http://musicbrainz.org/ns/mmd-2.0#}label-list>,<{http://musicbrainz.org/ns/mmd-2.0#}language>,<{http://musicbrainz.org/ns/mmd-2.0#}length>,<{http://musicbrainz.org/ns/mmd-2.0#}life-span>,<{http://musicbrainz.org/ns/mmd-2.0#}medium>,<{http://musicbrainz.org/ns/mmd-2.0#}medium-list>,<{http://musicbrainz.org/ns/mmd-2.0#}metadata>,<{http://musicbrainz.org/ns/mmd-2.0#}name>,<{http://musicbrainz.org/ns/mmd-2.0#}name-credit>,<{http://musicbrainz.org/ns/mmd-2.0#}packaging>,<{http://musicbrainz.org/ns/mmd-2.0#}position>,<{http://musicbrainz.org/ns/mmd-2.0#}puid>,<{http://musicbrainz.org/ns/mmd-2.0#}puid-list>,<{http://musicbrainz.org/ns/mmd-2.0#}rating>,<{http://musicbrainz.org/ns/mmd-2.0#}recording>,<{http://musicbrainz.org/ns/mmd-2.0#}recording-list>,<{http://musicbrainz.org/ns/mmd-2.0#}relation>,<{http://musicbrainz.org/ns/mmd-2.0#}relation-list>,<{http://musicbrainz.org/ns/mmd-2.0#}release>,<{http://musicbrainz.org/ns/mmd-2.0#}release-group>,<{http://musicbrainz.org/ns/mmd-2.0#}release-group-list>,<{http://musicbrainz.org/ns/mmd-2.0#}release-list>,<{http://musicbrainz.org/ns/mmd-2.0#}script>,<{http://musicbrainz.org/ns/mmd-2.0#}sectors>,<{http://musicbrainz.org/ns/mmd-2.0#}sort-name>,<{http://musicbrainz.org/ns/mmd-2.0#}status>,<{http://musicbrainz.org/ns/mmd-2.0#}tag>,<{http://musicbrainz.org/ns/mmd-2.0#}tag-list>,<{http://musicbrainz.org/ns/mmd-2.0#}target>,<{http://musicbrainz.org/ns/mmd-2.0#}text>,<{http://musicbrainz.org/ns/mmd-2.0#}text-representation>,<{http://musicbrainz.org/ns/mmd-2.0#}title>,<{http://musicbrainz.org/ns/mmd-2.0#}user-rating>,<{http://musicbrainz.org/ns/mmd-2.0#}user-tag>,<{http://musicbrainz.org/ns/mmd-2.0#}user-tag-list>,<{http://musicbrainz.org/ns/mmd-2.0#}work>,<{http://musicbrainz.org/ns/mmd-2.0#}work-list>,<{http://musicbrainz.org/ns/mmd-2.0#}year>]
    at
com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.handleStreamException(UnmarshallerImpl.java:425)
    at
com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:362)
    at
com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:332)
    at
com.sun.jersey.json.impl.JSONUnmarshallerImpl.unmarshal(JSONUnmarshallerImpl.java:110)
    at
com.sun.jersey.json.impl.BaseJSONUnmarshaller.unmarshalFromJSON(BaseJSONUnmarshaller.java:94)
    at
org.musicbrainz.search.index.ArtistIndexTest.testIndexArtistWithNoAlias(ArtistIndexTest.java:114)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at
com.intellij.junit3.JUnit3IdeaTestRunner.doRun(JUnit3IdeaTestRunner.java:108)
    at
com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:60)
Caused by: javax.xml.bind.UnmarshalException: unexpected element
(uri:"", local:"artist"). Expected elements are
<{http://musicbrainz.org/ns/mmd-2.0#}alias>,<{http://musicbrainz.org/ns/mmd-2.0#}alias-list>,<{http://musicbrainz.org/ns/mmd-2.0#}annotation>,<{http://musicbrainz.org/ns/mmd-2.0#}annotation-list>,<{http://musicbrainz.org/ns/mmd-2.0#}artist>,<{http://musicbrainz.org/ns/mmd-2.0#}artist-credit>,<{http://musicbrainz.org/ns/mmd-2.0#}artist-list>,<{http://musicbrainz.org/ns/mmd-2.0#}asin>,<{http://musicbrainz.org/ns/mmd-2.0#}attributes>,<{http://musicbrainz.org/ns/mmd-2.0#}barcode>,<{http://musicbrainz.org/ns/mmd-2.0#}begin>,<{http://musicbrainz.org/ns/mmd-2.0#}catalog-number>,<{http://musicbrainz.org/ns/mmd-2.0#}category>,<{http://musicbrainz.org/ns/mmd-2.0#}cdstub>,<{http://musicbrainz.org/ns/mmd-2.0#}cdstub-list>,<{http://musicbrainz.org/ns/mmd-2.0#}comment>,<{http://musicbrainz.org/ns/mmd-2.0#}date>,<{http://musicbrainz.org/ns/mmd-2.0#}direction>,<{http://musicbrainz.org/ns/mmd-2.0#}disambiguation>,<{http://musicbrainz.org/ns/mmd-2.0#}disc>,<{http://musicbrainz.org/ns/mmd-2.0#}disc-list>,<{http://musicbrainz.org/ns/mmd-2.0#}end>,<{http://musicbrainz.org/ns/mmd-2.0#}entity>,<{http://musicbrainz.org/ns/mmd-2.0#}format>,<{http://musicbrainz.org/ns/mmd-2.0#}freedb-disc>,<{http://musicbrainz.org/ns/mmd-2.0#}freedb-disc-list>,<{http://musicbrainz.org/ns/mmd-2.0#}gender>,<{http://musicbrainz.org/ns/mmd-2.0#}isrc>,<{http://musicbrainz.org/ns/mmd-2.0#}isrc-list>,<{http://musicbrainz.org/ns/mmd-2.0#}iswc>,<{http://musicbrainz.org/ns/mmd-2.0#}label>,<{http://musicbrainz.org/ns/mmd-2.0#}label-code>,<{http://musicbrainz.org/ns/mmd-2.0#}label-info>,<{http://musicbrainz.org/ns/mmd-2.0#}label-info-list>,<{http://musicbrainz.org/ns/mmd-2.0#}label-list>,<{http://musicbrainz.org/ns/mmd-2.0#}language>,<{http://musicbrainz.org/ns/mmd-2.0#}length>,<{http://musicbrainz.org/ns/mmd-2.0#}life-span>,<{http://musicbrainz.org/ns/mmd-2.0#}medium>,<{http://musicbrainz.org/ns/mmd-2.0#}medium-list>,<{http://musicbrainz.org/ns/mmd-2.0#}metadata>,<{http://musicbrainz.org/ns/mmd-2.0#}name>,<{http://musicbrainz.org/ns/mmd-2.0#}name-credit>,<{http://musicbrainz.org/ns/mmd-2.0#}packaging>,<{http://musicbrainz.org/ns/mmd-2.0#}position>,<{http://musicbrainz.org/ns/mmd-2.0#}puid>,<{http://musicbrainz.org/ns/mmd-2.0#}puid-list>,<{http://musicbrainz.org/ns/mmd-2.0#}rating>,<{http://musicbrainz.org/ns/mmd-2.0#}recording>,<{http://musicbrainz.org/ns/mmd-2.0#}recording-list>,<{http://musicbrainz.org/ns/mmd-2.0#}relation>,<{http://musicbrainz.org/ns/mmd-2.0#}relation-list>,<{http://musicbrainz.org/ns/mmd-2.0#}release>,<{http://musicbrainz.org/ns/mmd-2.0#}release-group>,<{http://musicbrainz.org/ns/mmd-2.0#}release-group-list>,<{http://musicbrainz.org/ns/mmd-2.0#}release-list>,<{http://musicbrainz.org/ns/mmd-2.0#}script>,<{http://musicbrainz.org/ns/mmd-2.0#}sectors>,<{http://musicbrainz.org/ns/mmd-2.0#}sort-name>,<{http://musicbrainz.org/ns/mmd-2.0#}status>,<{http://musicbrainz.org/ns/mmd-2.0#}tag>,<{http://musicbrainz.org/ns/mmd-2.0#}tag-list>,<{http://musicbrainz.org/ns/mmd-2.0#}target>,<{http://musicbrainz.org/ns/mmd-2.0#}text>,<{http://musicbrainz.org/ns/mmd-2.0#}text-representation>,<{http://musicbrainz.org/ns/mmd-2.0#}title>,<{http://musicbrainz.org/ns/mmd-2.0#}user-rating>,<{http://musicbrainz.org/ns/mmd-2.0#}user-tag>,<{http://musicbrainz.org/ns/mmd-2.0#}user-tag-list>,<{http://musicbrainz.org/ns/mmd-2.0#}work>,<{http://musicbrainz.org/ns/mmd-2.0#}work-list>,<{http://musicbrainz.org/ns/mmd-2.0#}year>
    at
com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:642)
    at
com.sun.xml.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:254)
    at
com.sun.xml.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:249)
    at
com.sun.xml.bind.v2.runtime.unmarshaller.Loader.reportUnexpectedChildElement(Loader.java:116)
    at
com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext$DefaultRootLoader.childElement(UnmarshallingContext.java:1049)
    at
com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext._startElement(UnmarshallingContext.java:478)
    at
com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.startElement(UnmarshallingContext.java:459)
    at
com.sun.xml.bind.v2.runtime.unmarshaller.InterningXmlVisitor.startElement(InterningXmlVisitor.java:71)
    at
com.sun.xml.bind.v2.runtime.unmarshaller.StAXStreamConnector.handleStartElement(StAXStreamConnector.java:242)
    at
com.sun.xml.bind.v2.runtime.unmarshaller.StAXStreamConnector.bridge(StAXStreamConnector.java:176)
    at
com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:360)
    ... 20 more
Caused by: javax.xml.bind.UnmarshalException: unexpected element
(uri:"", local:"artist"). Expected elements are
<{http://musicbrainz.org/ns/mmd-2.0#}alias>,<{http://musicbrainz.org/ns/mmd-2.0#}alias-list>,<{http://musicbrainz.org/ns/mmd-2.0#}annotation>,<{http://musicbrainz.org/ns/mmd-2.0#}annotation-list>,<{http://musicbrainz.org/ns/mmd-2.0#}artist>,<{http://musicbrainz.org/ns/mmd-2.0#}artist-credit>,<{http://musicbrainz.org/ns/mmd-2.0#}artist-list>,<{http://musicbrainz.org/ns/mmd-2.0#}asin>,<{http://musicbrainz.org/ns/mmd-2.0#}attributes>,<{http://musicbrainz.org/ns/mmd-2.0#}barcode>,<{http://musicbrainz.org/ns/mmd-2.0#}begin>,<{http://musicbrainz.org/ns/mmd-2.0#}catalog-number>,<{http://musicbrainz.org/ns/mmd-2.0#}category>,<{http://musicbrainz.org/ns/mmd-2.0#}cdstub>,<{http://musicbrainz.org/ns/mmd-2.0#}cdstub-list>,<{http://musicbrainz.org/ns/mmd-2.0#}comment>,<{http://musicbrainz.org/ns/mmd-2.0#}date>,<{http://musicbrainz.org/ns/mmd-2.0#}direction>,<{http://musicbrainz.org/ns/mmd-2.0#}disambiguation>,<{http://musicbrainz.org/ns/mmd-2.0#}disc>,<{http://musicbrainz.org/ns/mmd-2.0#}disc-list>,<{http://musicbrainz.org/ns/mmd-2.0#}end>,<{http://musicbrainz.org/ns/mmd-2.0#}entity>,<{http://musicbrainz.org/ns/mmd-2.0#}format>,<{http://musicbrainz.org/ns/mmd-2.0#}freedb-disc>,<{http://musicbrainz.org/ns/mmd-2.0#}freedb-disc-list>,<{http://musicbrainz.org/ns/mmd-2.0#}gender>,<{http://musicbrainz.org/ns/mmd-2.0#}isrc>,<{http://musicbrainz.org/ns/mmd-2.0#}isrc-list>,<{http://musicbrainz.org/ns/mmd-2.0#}iswc>,<{http://musicbrainz.org/ns/mmd-2.0#}label>,<{http://musicbrainz.org/ns/mmd-2.0#}label-code>,<{http://musicbrainz.org/ns/mmd-2.0#}label-info>,<{http://musicbrainz.org/ns/mmd-2.0#}label-info-list>,<{http://musicbrainz.org/ns/mmd-2.0#}label-list>,<{http://musicbrainz.org/ns/mmd-2.0#}language>,<{http://musicbrainz.org/ns/mmd-2.0#}length>,<{http://musicbrainz.org/ns/mmd-2.0#}life-span>,<{http://musicbrainz.org/ns/mmd-2.0#}medium>,<{http://musicbrainz.org/ns/mmd-2.0#}medium-list>,<{http://musicbrainz.org/ns/mmd-2.0#}metadata>,<{http://musicbrainz.org/ns/mmd-2.0#}name>,<{http://musicbrainz.org/ns/mmd-2.0#}name-credit>,<{http://musicbrainz.org/ns/mmd-2.0#}packaging>,<{http://musicbrainz.org/ns/mmd-2.0#}position>,<{http://musicbrainz.org/ns/mmd-2.0#}puid>,<{http://musicbrainz.org/ns/mmd-2.0#}puid-list>,<{http://musicbrainz.org/ns/mmd-2.0#}rating>,<{http://musicbrainz.org/ns/mmd-2.0#}recording>,<{http://musicbrainz.org/ns/mmd-2.0#}recording-list>,<{http://musicbrainz.org/ns/mmd-2.0#}relation>,<{http://musicbrainz.org/ns/mmd-2.0#}relation-list>,<{http://musicbrainz.org/ns/mmd-2.0#}release>,<{http://musicbrainz.org/ns/mmd-2.0#}release-group>,<{http://musicbrainz.org/ns/mmd-2.0#}release-group-list>,<{http://musicbrainz.org/ns/mmd-2.0#}release-list>,<{http://musicbrainz.org/ns/mmd-2.0#}script>,<{http://musicbrainz.org/ns/mmd-2.0#}sectors>,<{http://musicbrainz.org/ns/mmd-2.0#}sort-name>,<{http://musicbrainz.org/ns/mmd-2.0#}status>,<{http://musicbrainz.org/ns/mmd-2.0#}tag>,<{http://musicbrainz.org/ns/mmd-2.0#}tag-list>,<{http://musicbrainz.org/ns/mmd-2.0#}target>,<{http://musicbrainz.org/ns/mmd-2.0#}text>,<{http://musicbrainz.org/ns/mmd-2.0#}text-representation>,<{http://musicbrainz.org/ns/mmd-2.0#}title>,<{http://musicbrainz.org/ns/mmd-2.0#}user-rating>,<{http://musicbrainz.org/ns/mmd-2.0#}user-tag>,<{http://musicbrainz.org/ns/mmd-2.0#}user-tag-list>,<{http://musicbrainz.org/ns/mmd-2.0#}work>,<{http://musicbrainz.org/ns/mmd-2.0#}work-list>,<{http://musicbrainz.org/ns/mmd-2.0#}year>
    ... 31 more

which sounds like your 'support namespaces' point, but dont know ehere
to go with it

Paul