You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomee.apache.org by bibhas <bi...@gmail.com> on 2015/03/24 02:29:15 UTC

JSONProvider single length array

JSONProvider is serializing a single length array as a scalar. For example,
instead of serializing an array as:

["Hello"]

It is serializing as:

"Hello"

What can I enter in my resource.xml to fix this problem? Currently it looks
like this:

<resources>
    <Service id="jsonProvider"
class-name="org.apache.cxf.jaxrs.provider.json.JSONProvider">
        dropRootElement=true
        supportUnwrapped = true
        dropCollectionWrapperElement=true
        serializeAsArray = true
    </Service>
</resources>

Thank you.





--
View this message in context: http://tomee-openejb.979440.n4.nabble.com/JSONProvider-single-length-array-tp4674152.html
Sent from the TomEE Users mailing list archive at Nabble.com.

Re: JSONProvider single length array

Posted by Jean-Louis Monteiro <jl...@tomitribe.com>.
Ok then it's fine you have the configuration setup.
By default CXF uses Jettison which is some kind of JAXB adapter to
translate from XML to JSON. And obviously as the model are very different,
it does not work very well.

So basically, no one uses it AFAIK.
Most people just use Jackson provider which is the most used and the most
well known.
We have also Apache Johnzon lighter (a lot) and works fine.

JLouis

--
Jean-Louis Monteiro
http://twitter.com/jlouismonteiro
http://www.tomitribe.com

On Tue, Mar 24, 2015 at 2:11 PM, bibhas <bi...@gmail.com> wrote:

> Jean-Louis MONTEIRO wrote
> > I guess you use this definition as a reference in this openejb-jar.xml  ?
>
> Hi jean, I am not sure I understand your question. I am using
> openejb-jar.xml and resource.xml already to fix many other strange
> behaviors
> of JSONProvider. Now I am trying to fix this particular problem where a
> single element array is not serialized as array.
>
>
>
> --
> View this message in context:
> http://tomee-openejb.979440.n4.nabble.com/JSONProvider-single-length-array-tp4674152p4674159.html
> Sent from the TomEE Users mailing list archive at Nabble.com.
>

Re: JSONProvider single length array

Posted by Jonathan Gallimore <jo...@gmail.com>.
That's great, I'm glad you're getting the output you need. Thanks for
posting back with the link! Its been a while since I last used GSon, but I
remember it working well and being pretty easy to use.

Jon

On Tue, Mar 24, 2015 at 4:58 PM, bibhas <bi...@gmail.com> wrote:

> Jonathan Gallimore-2 wrote
> > One quick suggestion I have is that you might want to see if a different
> > provider solves your problem.
>
> I eventually chose to do the same. First I tried the Jackson provider. This
> has its own problem (it serializes null values as JSON null which causes
> unnecessary problems in iOS Objective-C). So, finally I went with a Gson
> provider (https://gist.github.com/nicksen/7335295). This is working great
> so
> far and I like Gson in general.
>
>
>
> --
> View this message in context:
> http://tomee-openejb.979440.n4.nabble.com/JSONProvider-single-length-array-tp4674152p4674162.html
> Sent from the TomEE Users mailing list archive at Nabble.com.
>

Re: JSONProvider single length array

Posted by Romain Manni-Bucau <rm...@gmail.com>.
it is configurable in jackson (like in johnzon actually):
http://wiki.fasterxml.com/JacksonAnnotationSerializeNulls



Romain Manni-Bucau
@rmannibucau <https://twitter.com/rmannibucau> |  Blog
<http://rmannibucau.wordpress.com> | Github <https://github.com/rmannibucau> |
LinkedIn <https://www.linkedin.com/in/rmannibucau> | Tomitriber
<http://www.tomitribe.com>

2015-03-24 17:58 GMT+01:00 bibhas <bi...@gmail.com>:

> Jonathan Gallimore-2 wrote
> > One quick suggestion I have is that you might want to see if a different
> > provider solves your problem.
>
> I eventually chose to do the same. First I tried the Jackson provider. This
> has its own problem (it serializes null values as JSON null which causes
> unnecessary problems in iOS Objective-C). So, finally I went with a Gson
> provider (https://gist.github.com/nicksen/7335295). This is working great
> so
> far and I like Gson in general.
>
>
>
> --
> View this message in context:
> http://tomee-openejb.979440.n4.nabble.com/JSONProvider-single-length-array-tp4674152p4674162.html
> Sent from the TomEE Users mailing list archive at Nabble.com.
>

Re: JSONProvider single length array

Posted by bibhas <bi...@gmail.com>.
Jonathan Gallimore-2 wrote
> One quick suggestion I have is that you might want to see if a different
> provider solves your problem. 

I eventually chose to do the same. First I tried the Jackson provider. This
has its own problem (it serializes null values as JSON null which causes
unnecessary problems in iOS Objective-C). So, finally I went with a Gson
provider (https://gist.github.com/nicksen/7335295). This is working great so
far and I like Gson in general. 



--
View this message in context: http://tomee-openejb.979440.n4.nabble.com/JSONProvider-single-length-array-tp4674152p4674162.html
Sent from the TomEE Users mailing list archive at Nabble.com.

Re: JSONProvider single length array

Posted by Jonathan Gallimore <jo...@gmail.com>.
One quick suggestion I have is that you might want to see if a different
provider solves your problem. Generally, this means adding the right
.jar(s) to your application and specifying the provider to use in your
openejb-jar.xml file:

Jackson:

<openejb-jar>
  <pojo-deployment
class-name="org.superbiz.moviefun.rest.ApplicationConfig">
    <properties>
      cxf.jaxrs.providers =
org.codehaus.jackson.jaxrs.JacksonJaxbJsonProvider
    </properties>
  </pojo-deployment>
</openejb-jar>

    <!-- Dependencies for Jackson CXF provider -->
    <dependency>
      <groupId>org.codehaus.jackson</groupId>
      <artifactId>jackson-core-asl</artifactId>
      <version>1.9.13</version>
    </dependency>
    <dependency>
      <groupId>org.codehaus.jackson</groupId>
      <artifactId>jackson-jaxrs</artifactId>
      <version>1.9.13</version>
    </dependency>
    <dependency>
      <groupId>org.codehaus.jackson</groupId>
      <artifactId>jackson-mapper-asl</artifactId>
      <version>1.9.13</version>
    </dependency>
    <dependency>
      <groupId>org.codehaus.jackson</groupId>
      <artifactId>jackson-xc</artifactId>
      <version>1.9.13</version>
    </dependency>

Or Johnzon:

<openejb-jar>
  <pojo-deployment
class-name="org.superbiz.moviefun.rest.ApplicationConfig">
    <properties>
      cxf.jaxrs.skip-provider-scanning = true
      cxf.jaxrs.providers =
org.apache.johnzon.jaxrs.JohnzonProvider,org.apache.openejb.server.cxf.rs.EJBAccessExceptionMapper
    </properties>
  </pojo-deployment>
</openejb-jar>

    <dependency>
        <groupId>org.apache.johnzon</groupId>
        <artifactId>johnzon-core</artifactId>
        <version>0.5-incubating</version>
      </dependency>
      <dependency>
        <groupId>org.apache.johnzon</groupId>
        <artifactId>johnzon-mapper</artifactId>
        <version>0.5-incubating</version>
      </dependency>
      <dependency>
        <groupId>org.apache.johnzon</groupId>
        <artifactId>johnzon-jaxrs</artifactId>
        <version>0.5-incubating</version>
      </dependency>

I would imagine you will still need to do some fiddling to get the exact
output you require, but one of these providers may help if you're not
getting the exact output you want from the CXF provider.

Hope that helps.

Jon

On Tue, Mar 24, 2015 at 1:11 PM, bibhas <bi...@gmail.com> wrote:

> Jean-Louis MONTEIRO wrote
> > I guess you use this definition as a reference in this openejb-jar.xml  ?
>
> Hi jean, I am not sure I understand your question. I am using
> openejb-jar.xml and resource.xml already to fix many other strange
> behaviors
> of JSONProvider. Now I am trying to fix this particular problem where a
> single element array is not serialized as array.
>
>
>
> --
> View this message in context:
> http://tomee-openejb.979440.n4.nabble.com/JSONProvider-single-length-array-tp4674152p4674159.html
> Sent from the TomEE Users mailing list archive at Nabble.com.
>

Re: JSONProvider single length array

Posted by bibhas <bi...@gmail.com>.
Jean-Louis MONTEIRO wrote
> I guess you use this definition as a reference in this openejb-jar.xml  ?

Hi jean, I am not sure I understand your question. I am using
openejb-jar.xml and resource.xml already to fix many other strange behaviors
of JSONProvider. Now I am trying to fix this particular problem where a
single element array is not serialized as array.



--
View this message in context: http://tomee-openejb.979440.n4.nabble.com/JSONProvider-single-length-array-tp4674152p4674159.html
Sent from the TomEE Users mailing list archive at Nabble.com.

Re: JSONProvider single length array

Posted by Jean-Louis Monteiro <jl...@tomitribe.com>.
I guess you use this definition as a reference in this openejb-jar.xml  ?
Le 24 mars 2015 02:43, "bibhas" <bi...@gmail.com> a écrit :

> JSONProvider is serializing a single length array as a scalar. For example,
> instead of serializing an array as:
>
> ["Hello"]
>
> It is serializing as:
>
> "Hello"
>
> What can I enter in my resource.xml to fix this problem? Currently it looks
> like this:
>
> <resources>
>     <Service id="jsonProvider"
> class-name="org.apache.cxf.jaxrs.provider.json.JSONProvider">
>         dropRootElement=true
>         supportUnwrapped = true
>         dropCollectionWrapperElement=true
>         serializeAsArray = true
>     </Service>
> </resources>
>
> Thank you.
>
>
>
>
>
> --
> View this message in context:
> http://tomee-openejb.979440.n4.nabble.com/JSONProvider-single-length-array-tp4674152.html
> Sent from the TomEE Users mailing list archive at Nabble.com.
>