users@jersey.java.net

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

From: Paul Taylor <paul_t100_at_fastmail.fm>
Date: Tue, 17 Nov 2009 13:32:48 +0000

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