On Wed, Aug 8, 2012 at 8:29 AM, James Green <james.mk.green_at_gmail.com> wrote:
> See below.
>
> On 8 August 2012 16:10, Tatu Saloranta <tsaloranta_at_gmail.com> wrote:
>>
>> On Wed, Aug 8, 2012 at 8:01 AM, Tatu Saloranta <tsaloranta_at_gmail.com>
>> wrote:
>> > On Tue, Aug 7, 2012 at 11:33 PM, James Green <james.mk.green_at_gmail.com>
>> > wrote:
>> >>
>> >>
>> >> On 7 August 2012 23:38, Tatu Saloranta <tsaloranta_at_gmail.com> wrote:
>> >>>
>> >>> On Tue, Aug 7, 2012 at 2:54 PM, James Green <james.mk.green_at_gmail.com>
>> >>> wrote:
>> >>> > OK, I've hooked up a real simple project with a Customer bean having
>> >>> > @XmlRootElement. It works out the of box with an Application
>> >>> > activator
>> >>> > returning a JacksonJaxbJsonProvider.
>> >>> >
>> >>> > I've switched on SerializationFeature.WRAP_ROOT_VALUE to mirror our
>> >>> > XML
>> >>> > api
>> >>> > documentation a little closer. Two things are otherwise immediately
>> >>> > apparent:
>> >>> >
>> >>> > 1. The JSON has an empty root name:
>> >>> >
>> >>> > {
>> >>> > "": {
>> >>> > "id": 2
>> >>> > }
>> >>> > }
>> >>> >
>> >>> > Unless I expressly set the name of the @XmlRootElement. Can't find
>> >>> > reference
>> >>> > to this on Google though. Any ideas?
>> >>>
>> >>> This does seem odd: the default behavior, if annotation is not used
>> >>> (that, or Jackson's @JsonRootName), would be to use simple class name
>> >>> of the value class.
>> >>
>> >>
>> >> Mind taking a look?
>> >>
>> >> Customer model in full: http://pastebin.com/RjdTvagY
>> >> JAX-RS Activator: http://pastebin.com/9v2CkbLz
>> >
>> > I'll have a look, see if anything jumps out.
>>
>> Ah. This is due to interaction between JAXB annotation introspector
>> (Jackson component), and Jackson's root-wrapper. Empty String is not
>> converted properly it seems, and all tests use either explicit name,
>> or omit @XmlRootElement.
>> That is, this looks like a bug in Jackson.
>>
>> I will add a Jira issue and get this fixed. As a work-around, you can
>> either leave out the annotation (unless you have to use JAXB for XML);
>> or add the name argument.
>
>
> Would this be the same for @XmlElement(name="mappedName") ?
No, that should work as expected (different code path). And from what
I can see, also does work. :)
> Does Jackson happen to have a factory to create a JSON configuration to
> closely match XML representations?
I don't think there is a single XML representation to match, but maybe
you are thinking of the default JAXB XML output? There are no
convenience method to try to match output settings.
> I ask as I'm using rest-assured to perform testing of our JAX-RS interface,
> querying entity.field for values. By default, entity does not exist (it's
> not wrapped) and dates are represented as longs. Not sure yet what else will
> prove a difference...
Right. Jackson tries to produce as clean JSON as possible, which is
why root name wrapping is a late addition -- it is something XML must
have, but is mostly unnecessary for JSON.
In general the goal is to be able to handle same POJOs, but not to try
to couple JSON and XML structure together: formats have high
impedance, their own idioms, so I prefer both clean, rather than
similar looking.
However, some form of "compatibility settings" might well make sense.
I don't know what the right way to expose those would be, but I am
open to good ideas.
-+ Tatu +-