You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by Chris Geer <ch...@cxtsoftware.com> on 2012/01/18 20:07:09 UTC

JSON Serialization

I have a web service which supports both SOAP and RESTful calls which works
great except for one small issue with JSON serialization on List results.

I have two methods, one that returns a single entity and one that returns a
List of entities.

    @GET
    @Path("/parcel/{id}")
    @Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
    @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
    public Parcel getParcel(@PathParam("id") String id) {...}

and

    @GET
    @Path("/parcels")
    @Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
    @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
    public List<Parcel> getParcels() {...}

When these methods are called via SOAP or Rest with XML return it all works
perfectly. When I call with JSON return it all looks decent but breaks
down. With no JSONProvider configuration the results look like this:

getParcel
    {"ns1.Parcel": {
       "ns1.ParcelID": 26,
       "ns1.Fragile": false,
       "ns1.Weight": 34
    }}

getParcels
    {"ns1.Parcel": [
      {
        "ns1.ParcelID": 1,
        "ns1.Fragile": true,
        "ns1.Weight": 100},
      {
        "ns1.ParcelID": 2,
        "ns1.Fragile": false,
        "ns1.Weight": 1000}
    ]}

Since the ns1 prefix is "random" (it's changed on me several times) I
wanted to define my own static prefix so I added a namespace map to the
JSONProvider and get these results

getParcel
    {"p.Parcel": {
       "p.ParcelID": 26,
       "p.Fragile": false,
       "p.Weight": 34
    }}

getParcels
    {"ns1.Parcel": [
      {
        "p.ParcelID": 1,
        "p.Fragile": true,
        "p.Weight": 100},
      {
        "p.ParcelID": 2,
        "p.Fragile": false,
        "p.Weight": 1000}
    ]}

The return of the single item came across as I expected (everything with a
single prefix p) but the List return still had the collection node with the
random ns1 prefix. Since that didn't work the way I wanted, I removed the
namespace map and added <property name="ignoreNamespaces" value="true"/>
and got these results

getParcel
    {"Parcel": {
       "ParcelID": 26,
       "Fragile": false,
       "Weight": 34
    }}

getParcels
    {"ns1.Parcel": [
      {"Parcel":    {
        "ParcelID": 1,
        "Fragile": true,
        "Weight": 100}},
      {"Parcel":    {
        "ParcelID": 2,
        "Fragile": false,
        "Weight": 1000}}
    ]}

Again, the single item came across fine but the List return wasn't what I
expected at all. I also tried to fool with some of the other options but
nothing I could try seemed to work. I want to get rid of the ns1 prefix on
the List wrapper element but can't figure it out.

Thanks,
Chris

Re: JSON Serialization

Posted by hdave <da...@gmail.com>.
I got around this by replacing Jettison with Jackson for JSON encoding. 
Jackson doesn't rely on the babelfish convention and thus the JSON produced
is more easily consumed by modern Javascript client libraries.

--
View this message in context: http://cxf.547215.n5.nabble.com/JSON-Serialization-tp5155615p5157913.html
Sent from the cxf-user mailing list archive at Nabble.com.

Re: JSON Serialization

Posted by Chris Geer <ch...@cxtsoftware.com>.
Thanks Sergey.

On Wed, Jan 18, 2012 at 4:31 PM, Sergey Beryozkin <sb...@gmail.com>wrote:

> Hi
>
> On 18/01/12 19:07, Chris Geer wrote:
>
>> I have a web service which supports both SOAP and RESTful calls which
>> works
>> great except for one small issue with JSON serialization on List results.
>>
>> I have two methods, one that returns a single entity and one that returns
>> a
>> List of entities.
>>
>>     @GET
>>     @Path("/parcel/{id}")
>>     @Consumes({MediaType.**APPLICATION_JSON, MediaType.APPLICATION_XML})
>>     @Produces({MediaType.**APPLICATION_JSON, MediaType.APPLICATION_XML})
>>     public Parcel getParcel(@PathParam("id") String id) {...}
>>
>> and
>>
>>     @GET
>>     @Path("/parcels")
>>     @Consumes({MediaType.**APPLICATION_JSON, MediaType.APPLICATION_XML})
>>     @Produces({MediaType.**APPLICATION_JSON, MediaType.APPLICATION_XML})
>>     public List<Parcel>  getParcels() {...}
>>
>> When these methods are called via SOAP or Rest with XML return it all
>> works
>> perfectly. When I call with JSON return it all looks decent but breaks
>> down. With no JSONProvider configuration the results look like this:
>>
>> getParcel
>>     {"ns1.Parcel": {
>>        "ns1.ParcelID": 26,
>>        "ns1.Fragile": false,
>>        "ns1.Weight": 34
>>     }}
>>
>> getParcels
>>     {"ns1.Parcel": [
>>       {
>>         "ns1.ParcelID": 1,
>>         "ns1.Fragile": true,
>>         "ns1.Weight": 100},
>>       {
>>         "ns1.ParcelID": 2,
>>         "ns1.Fragile": false,
>>         "ns1.Weight": 1000}
>>     ]}
>>
>> Since the ns1 prefix is "random" (it's changed on me several times) I
>> wanted to define my own static prefix so I added a namespace map to the
>> JSONProvider and get these results
>>
>> getParcel
>>     {"p.Parcel": {
>>        "p.ParcelID": 26,
>>        "p.Fragile": false,
>>        "p.Weight": 34
>>     }}
>>
>> getParcels
>>     {"ns1.Parcel": [
>>       {
>>         "p.ParcelID": 1,
>>         "p.Fragile": true,
>>         "p.Weight": 100},
>>       {
>>         "p.ParcelID": 2,
>>         "p.Fragile": false,
>>         "p.Weight": 1000}
>>     ]}
>>
>> The return of the single item came across as I expected (everything with a
>> single prefix p) but the List return still had the collection node with
>> the
>> random ns1 prefix. Since that didn't work the way I wanted, I removed the
>> namespace map and added<property name="ignoreNamespaces" value="true"/>
>> and got these results
>>
>> getParcel
>>     {"Parcel": {
>>        "ParcelID": 26,
>>        "Fragile": false,
>>        "Weight": 34
>>     }}
>>
>> getParcels
>>     {"ns1.Parcel": [
>>       {"Parcel":    {
>>         "ParcelID": 1,
>>         "Fragile": true,
>>         "Weight": 100}},
>>       {"Parcel":    {
>>         "ParcelID": 2,
>>         "Fragile": false,
>>         "Weight": 1000}}
>>     ]}
>>
>> Again, the single item came across fine but the List return wasn't what I
>> expected at all. I also tried to fool with some of the other options but
>> nothing I could try seemed to work. I want to get rid of the ns1 prefix on
>> the List wrapper element but can't figure it out.
>>
>
> I've got it fixed, please consider introducing a Parcels wrapper bean is a
> workaround, but either  ignoreNamespaces or setting a prefix in the
> namespaceMap will do it for the wrapper element in the next versions
>
> Cheers, Sergey
>
>
>> Thanks,
>> Chris
>>
>>
>
> --
> Sergey Beryozkin
>
> Talend Community Coders
> http://coders.talend.com/
>
> Blog: http://sberyozkin.blogspot.com
>

Re: JSON Serialization

Posted by Sergey Beryozkin <sb...@gmail.com>.
Hi
On 18/01/12 19:07, Chris Geer wrote:
> I have a web service which supports both SOAP and RESTful calls which works
> great except for one small issue with JSON serialization on List results.
>
> I have two methods, one that returns a single entity and one that returns a
> List of entities.
>
>      @GET
>      @Path("/parcel/{id}")
>      @Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
>      @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
>      public Parcel getParcel(@PathParam("id") String id) {...}
>
> and
>
>      @GET
>      @Path("/parcels")
>      @Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
>      @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
>      public List<Parcel>  getParcels() {...}
>
> When these methods are called via SOAP or Rest with XML return it all works
> perfectly. When I call with JSON return it all looks decent but breaks
> down. With no JSONProvider configuration the results look like this:
>
> getParcel
>      {"ns1.Parcel": {
>         "ns1.ParcelID": 26,
>         "ns1.Fragile": false,
>         "ns1.Weight": 34
>      }}
>
> getParcels
>      {"ns1.Parcel": [
>        {
>          "ns1.ParcelID": 1,
>          "ns1.Fragile": true,
>          "ns1.Weight": 100},
>        {
>          "ns1.ParcelID": 2,
>          "ns1.Fragile": false,
>          "ns1.Weight": 1000}
>      ]}
>
> Since the ns1 prefix is "random" (it's changed on me several times) I
> wanted to define my own static prefix so I added a namespace map to the
> JSONProvider and get these results
>
> getParcel
>      {"p.Parcel": {
>         "p.ParcelID": 26,
>         "p.Fragile": false,
>         "p.Weight": 34
>      }}
>
> getParcels
>      {"ns1.Parcel": [
>        {
>          "p.ParcelID": 1,
>          "p.Fragile": true,
>          "p.Weight": 100},
>        {
>          "p.ParcelID": 2,
>          "p.Fragile": false,
>          "p.Weight": 1000}
>      ]}
>
> The return of the single item came across as I expected (everything with a
> single prefix p) but the List return still had the collection node with the
> random ns1 prefix. Since that didn't work the way I wanted, I removed the
> namespace map and added<property name="ignoreNamespaces" value="true"/>
> and got these results
>
> getParcel
>      {"Parcel": {
>         "ParcelID": 26,
>         "Fragile": false,
>         "Weight": 34
>      }}
>
> getParcels
>      {"ns1.Parcel": [
>        {"Parcel":    {
>          "ParcelID": 1,
>          "Fragile": true,
>          "Weight": 100}},
>        {"Parcel":    {
>          "ParcelID": 2,
>          "Fragile": false,
>          "Weight": 1000}}
>      ]}
>
> Again, the single item came across fine but the List return wasn't what I
> expected at all. I also tried to fool with some of the other options but
> nothing I could try seemed to work. I want to get rid of the ns1 prefix on
> the List wrapper element but can't figure it out.

I've got it fixed, please consider introducing a Parcels wrapper bean is 
a workaround, but either  ignoreNamespaces or setting a prefix in the 
namespaceMap will do it for the wrapper element in the next versions

Cheers, Sergey

>
> Thanks,
> Chris
>


-- 
Sergey Beryozkin

Talend Community Coders
http://coders.talend.com/

Blog: http://sberyozkin.blogspot.com