users@jersey.java.net

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

From: Paul Taylor <paul_t100_at_fastmail.fm>
Date: Sun, 22 Nov 2009 08:51:02 +0000

Jakub Podlesak wrote:
> On Thu, Nov 19, 2009 at 07:33:19PM +0000, Paul Taylor wrote:
>
>> Jakub Podlesak wrote:
>>
>>> Hi Paul,
>>>
>>> please see inline...
>>>
>>> On Wed, Nov 18, 2009 at 08:12:35PM +0000, Paul Taylor wrote:
>>>
>>>
>>>> Jakub Podlesak wrote:
>>>>
>>>>
>>>>> 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
>>>>>
>>>>>
>>>>>
>>>> Hmm, Unfortunately I need to use mapping natural() so that the json
>>>> matches the form of json returned by another (non-java) api , and this
>>>> doesn't have the attributesAsElement option so I can't use this
>>>> workaround.
>>>>
>>>>
>>> I do not understand. What matters is the JSON format, and the only thing
>>> you get from using attributeAsElement option in Jersey is you get rid of
>>> the '@' prefixes. The other (non-java) api should be able to process
>>> such JSON data. What am i missing?
>>>
>>>
>> I think you mean 'and the only thing you get from using *natural* option
>> in Jersey is you get rid of the '@' prefixes'
>>
>> yes okay, but the code (written by someone else) that processeses my
>> output doesn't expect to see these @, they already have code for
>> processing json that comes back from a database that doesnt
>> contain '@' for attributes so they don't want them. Perhaps I could use
>> your workaround then remove the @'s myself but that would be using
>>
>
> After applying the workaround, the '@'s will get removed by Jersey,
> no need to apply any other filter. To be clear, you need to name all
> possible attributes in your configuration to get rid of all '@'s,
> which is why you probably would like to use the natural option.
>
>
>> rexexp , expensive and I might make a mess of it. It would be nice if
>> json api allowed
>> removal of @ AND use of attributeAsElemnt() method.
>>
>>>
>>>
>>>> I could modify the xsd though, could you show me how to change the xsd
>>>> so that there is a score attribute on releasegroup which is (and this is
>>>> the bit i don't get) is a part of the
>>>> "http://musicbrainz.org/ns/ext#-2.0"
>>>> namespace rather than the main namespace.
>>>>
>>>>
>>> O.K. i could provide you st later tonight my time.
>>>
>
> It's later than promised, but please see attached project including needed schema files
> for details. (i had to update the original schema file and added a new one)
> It shows how to use the natural notation (i.e. you do not need to configure
> anything manually), just unzip it and launch mvn clean compile exec:java
> then check out http://localhost:9998/myresource which should give you:
>
> {"release-group-list":{"release-group":[{"score":0},{"score":1},{"score":2},{"score":3},{"score":4}]}}
>
> corresponding XML is then:
>
> <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
> <metadata xmlns="http://musicbrainz.org/ns/mmd-2.0#" xmlns:ns2="http://musicbrainz.org/ns/ext#-2.0">
> <release-group-list>
> <release-group ns2:score="0"/>
> <release-group ns2:score="1"/>
> <release-group ns2:score="2"/>
> <release-group ns2:score="3"/>
> <release-group ns2:score="4"/>
> </release-group-list>
> </metadata>
>
>
> Does it work for you?
>
> ~Jakub
>
>
Hi Jakub

It does sort of work, the json is now fine thankyou, but why has the
namespace prefix gone to ns2, previously it was ext, i.e

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<metadata xmlns="http://musicbrainz.org/ns/mmd-2.0#"><release-group-list
offset="0" count="1">
<release-group type="compilation"
id="765675cc-070b-36e0-829c-69c7634e92ff" ext:score="100"
xmlns:ext="http://musicbrainz.org/ns/ext#-2.0">
<title>The History of John Fred and the
Playboys</title><artist-credit><name-credit>
<artist id="05cfb5c7-0152-41f4-a9c9-622e8f710dfa">
<name>John Fred &amp; His Playboy Band</name>
<sort-name>Fred, John &amp; His Playboy Band</sort-name>
</artist></name-credit></artist-credit><release-list count="1">
<release id="9cddae0c-1764-41d2-a1c4-0af2a8404e57"><title>The History of
John Fred and the
Playboys</title></release></release-list></release-group></release-group-list>
</metadata>



How can I keep it as ext, or is the original xml wrong in some way
(forgive me but despite some effort I havent got my head round the
namespace syntax)

Paul