users@jersey.java.net

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

From: Jakub Podlesak <Jakub.Podlesak_at_Sun.COM>
Date: Wed, 18 Nov 2009 15:52:19 +0100

Hi Paul,

It turns out, JAXB runtime does not provide correct information
on the score attribute in this (unfortunate xs:anyAttribute) case.
I am not sure this could be fixed easily, but you can try using the following
JSON configuration as a workaround:

            return new JSONJAXBContext(
                    JSONConfiguration
                    .mapped()
                    .attributeAsElement("score")
                    .arrays("release-group")
                    // comment this out, if you want to get string "50" instead of number 50
                    .nonStrings("score")
                    .build(),
            "org.musicbrainz.mmd2");

in the initJsonContext method.

Which gives me:

{"release-group-list":{"release-group":[{"score":50},{"score":50},{"score":50},{"score":50},{"score":50}]}}

HTH,

~Jakub



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
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>

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