users@jersey.java.net

Re: [Jersey] Json output not always matching xml output

From: Jakub Podlesak <Jakub.Podlesak_at_Sun.COM>
Date: Tue, 17 Nov 2009 16:16:42 +0100

Hi Paul,

thanks for the example. And sorry for not replying earlier.
We are celebrating 20 years anniversary of the Velvet revolution
in my country today [1], and i spent most of the day offline.
Will try to look at the example and provide something for you
by end of this week.

~Jakub


[1]http://en.wikipedia.org/wiki/Velvet_Revolution


On Tue, Nov 17, 2009 at 01:32:48PM +0000, Paul Taylor wrote:
> Hi Jakob heres, a self conttined example
>
> import com.sun.jersey.api.json.JSONConfiguration;
> import com.sun.jersey.api.json.JSONJAXBContext;
> import com.sun.jersey.api.json.JSONMarshaller;
> import org.musicbrainz.mmd2.Metadata;
> import org.musicbrainz.mmd2.ObjectFactory;
> import org.musicbrainz.mmd2.ReleaseGroup;
> import org.musicbrainz.mmd2.ReleaseGroupList;
>
> import javax.xml.bind.JAXBException;
> import javax.xml.namespace.QName;
>
> public class TestJSONOutput {
>
> static final JSONJAXBContext jsoncontext = initJsonContext();
>
> private static JSONJAXBContext initJsonContext() {
> try {
> return new JSONJAXBContext(JSONConfiguration.natural().build(),
> "org.musicbrainz.mmd2");
> }
> catch (JAXBException ex) {
> //Unable to initilize jaxb context, should never happen
> throw new RuntimeException(ex);
> }
> }
>
> public static void main(String [] args) {
>
>
> try
> {
> ObjectFactory of = new ObjectFactory();
>
> Metadata metadata = of.createMetadata();
> ReleaseGroupList releaseGroupList = of.createReleaseGroupList();
> for(int i=0;i<5;i++) {
> ReleaseGroup releaseGroup = of.createReleaseGroup();
> releaseGroup.getOtherAttributes().put(getScore(), "50");
> releaseGroupList.getReleaseGroup().add(releaseGroup);
> }
> metadata.setReleaseGroupList(releaseGroupList);
> JSONMarshaller m = jsoncontext.createJSONMarshaller();
> m.marshallToJSON(metadata, System.out);
> }
> catch(Exception e)
> {
> e.printStackTrace();
> throw new RuntimeException(e);
> }
> }
>
> protected static QName getScore() {
> return new QName("http://musicbrainz.org/ns/ext#-2.0", "score",
> "ext");
> }
>
>
> }
>
> which outputs score in brackets for every match except the first one
>
> {"release-group-list":{"release-group":[{"score":"50"},{"score":["50"]},{"score":["50"]},{"score":["50"]},{"score":["50"]}]}}
>
> when I would expect it to be
>
> {"release-group-list":{"release-group":[{"score":"50"},{"score":"50"},{"score":"50"},{"score":"50"},{"score":"50"}]}}
>
>
> I'll send you a jar directly
>
> Paiul
>
>

-- 
Jakub Podlešák
Software Engineer at SUN Microsystems And CZJUG Co-Leader
http://blogs.sun.com/japod