You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by Lars Beiderbecke <la...@gmail.com> on 2012/11/27 14:19:24 UTC

JSON array serialization workaround not working

Hello,

I'm trying to work around the JSON array serialization bug of Jettison
in conjunction with CXF as described in

   <http://cxf.apache.org/docs/jax-rs-data-bindings.html#JAX-RSDataBindings-DealingwithJSONarrayserializationissues>

I've created a WEB-INF/classes/cxf.xml file that sets the properties mentioned:

  <beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-2.0.xsd">
    <util:list id="jsonKeys">
      <value>myArray1</value>
      <value>myArray2</value>
    </util:list>
  <bean id="jsonProvider"
class="org.apache.cxf.jaxrs.provider.json.JSONProvider">
      <property name="serializeAsArray" value="true"/>
      <property name="arrayKeys" ref="jsonKeys"/>
    </bean>
  </beans>

Alas, this is not working:

   {"MyModel":{"myVal1":"val","myArray1":123,"myArray2":456}}

but I have no idea what is missing here. The log doesn't show anything unusual.

Note that I'm using the non-Spring servlet:

  <servlet>
    <servlet-name>CXFServlet</servlet-name>
    <servlet-class>org.apache.cxf.jaxrs.servlet.CXFNonSpringJaxrsServlet</servlet-class>
    <init-param>
      <param-name>jaxrs.serviceClasses</param-name>
      <param-value>test.RestService</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>

Is the workaround even supposed to function without Spring? Any help
would be highly appreciated.

Regards
Lars

Re: JSON array serialization workaround not working

Posted by Sergey Beryozkin <sb...@gmail.com>.
I've done a minor update to get the list properties supported at 
CXFNonSpringJaxrsServlet level too, see an example at the end of:

http://svn.apache.org/viewvc?rev=1414825&view=rev

Cheers, Sergey
On 28/11/12 10:37, Sergey Beryozkin wrote:
> Hi Lars
> On 28/11/12 07:43, Lars Beiderbecke wrote:
>> Hello,
>>
>> Yes, a workaround for non-Spring scenarios would be most helpful.
>>
>> Also note that having to include every single array in a separate file
>> such as cxf.xml isn't really a workaround, it's more like an ugly
>> kludge (no offense). Have there been thoughts about a proper solution?
>
> I won't pretend and say it is actually a super way to manage this issue
> :-), but I've no idea how to fix it at the Jettison level such that no
> custom configuration is requested.
>
> Actually, spotted
> http://jackson-users.ning.com/forum/topics/support-for-jettison-s-broken-array-serialization
>
>
> Jackson does it right all the time, but can optionally be configured to
> unwrap a single element array...I suspect there might be few clients out
> there who depend on this Jettison specific treatment of a single array
> element.
>
> As far as I understand, Jettison can not auto-determine in principle, by
> the time it starts to serialize, whether a key with a single value
> represents a simple or list property given that it has no access to the
> type info. I wonder if Jackson introspects the bean which is serialized
> - seems like the only way to do it right...
>
> This is why 'arrayKey' property is needed.
>
> Have a look at Jettison code please too. As I said - I'm not sure this
> can be fixed correctly at the custom writer implementation level without
> any type info, but may be some other 'hint' my help, I don't know, say,
> a simple property can never appear where an array/list property is
> supposed to appear, if something like that is possible to assert then
> Jettison can always know when to use an array serilaization for a key
> with a single value only. Whether it is realistic I do not know...
>
> Cheers. Sergey
>
>> I found bug report<https://issues.apache.org/jira/browse/CXF-2820>,
>> which has been closed as WON'T FIX.
>>
>> Lars
>>
>>
>> On Tue, Nov 27, 2012 at 4:36 PM, Sergey
>> Beryozkin<sb...@gmail.com> wrote:
>>> Hi
>>>
>>> On 27/11/12 15:30, Ladislav Lenčucha wrote:
>>>>
>>>> Have you checked
>>>>
>>>> http://svn.apache.org/repos/asf/cxf/trunk/systests/jaxrs/src/test/resources/jaxrs/WEB-INF/beans.xml?
>>>>
>>>>
>>>> Could you attach all relevant parts of your cxf.xml? That includes e.g.
>>>> <jaxr:server>. Also attach the object that contains
>>>> array/collection. Are
>>>> the collection items annotated as myArray1 or myArray2?
>>>>
>>> Unfortunately, given that non-Spring servlet is used, this spring config
>>> won't be used...And it is only possible to list simple parameters, as
>>> opposed to the list parameters, for specific providers...
>>>
>>> I'll check if a list parameter can be supported easily enough...
>>>
>>> Sergey
>
>


-- 
Sergey Beryozkin

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

Blog: http://sberyozkin.blogspot.com

Re: JSON array serialization workaround not working

Posted by Sergey Beryozkin <sb...@gmail.com>.
Hi Lars
On 28/11/12 07:43, Lars Beiderbecke wrote:
> Hello,
>
> Yes, a workaround for non-Spring scenarios would be most helpful.
>
> Also note that having to include every single array in a separate file
> such as cxf.xml isn't really a workaround, it's more like an ugly
> kludge (no offense). Have there been thoughts about a proper solution?

I won't pretend and say it is actually a super way to manage this issue 
:-), but I've no idea how to fix it at the Jettison level such that no 
custom configuration is requested.

Actually, spotted
http://jackson-users.ning.com/forum/topics/support-for-jettison-s-broken-array-serialization

Jackson does it right all the time, but can optionally be configured to 
unwrap a single element array...I suspect there might be few clients out 
there who depend on this Jettison specific treatment of a single array 
element.

As far as I understand, Jettison can not auto-determine in principle, by 
the time it starts to serialize, whether a key with a single value 
represents a simple or list property given that it has no access to the 
type info. I wonder if Jackson introspects the bean which is serialized 
- seems like the only way to do it right...

This is why 'arrayKey' property is needed.

Have a look at Jettison code please too. As I said - I'm not sure this 
can be fixed correctly at the custom writer implementation level without 
any type info, but may be some other 'hint' my help, I don't know, say, 
a simple property can never appear where an array/list property is 
supposed to appear, if something like that is possible to assert then 
Jettison can always know when to use an array serilaization for a key 
with a single value only. Whether it is realistic I do not know...

Cheers. Sergey

> I found bug report<https://issues.apache.org/jira/browse/CXF-2820>,
> which has been closed as WON'T FIX.
>
> Lars
>
>
> On Tue, Nov 27, 2012 at 4:36 PM, Sergey Beryozkin<sb...@gmail.com>  wrote:
>> Hi
>>
>> On 27/11/12 15:30, Ladislav Lenčucha wrote:
>>>
>>> Have you checked
>>>
>>> http://svn.apache.org/repos/asf/cxf/trunk/systests/jaxrs/src/test/resources/jaxrs/WEB-INF/beans.xml?
>>>
>>> Could you attach all relevant parts of your cxf.xml? That includes e.g.
>>> <jaxr:server>. Also attach the object that contains array/collection. Are
>>> the collection items annotated as myArray1 or myArray2?
>>>
>> Unfortunately, given that non-Spring servlet is used, this spring config
>> won't be used...And it is only possible to list simple parameters, as
>> opposed to the list parameters, for specific providers...
>>
>> I'll check if a list parameter can be supported easily enough...
>>
>> Sergey


-- 
Sergey Beryozkin

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

Blog: http://sberyozkin.blogspot.com

Re: JSON array serialization workaround not working

Posted by Lars Beiderbecke <la...@gmail.com>.
Hello,

Yes, a workaround for non-Spring scenarios would be most helpful.

Also note that having to include every single array in a separate file
such as cxf.xml isn't really a workaround, it's more like an ugly
kludge (no offense). Have there been thoughts about a proper solution?
I found bug report <https://issues.apache.org/jira/browse/CXF-2820>,
which has been closed as WON'T FIX.

Lars


On Tue, Nov 27, 2012 at 4:36 PM, Sergey Beryozkin <sb...@gmail.com> wrote:
> Hi
>
> On 27/11/12 15:30, Ladislav Lenčucha wrote:
>>
>> Have you checked
>>
>> http://svn.apache.org/repos/asf/cxf/trunk/systests/jaxrs/src/test/resources/jaxrs/WEB-INF/beans.xml?
>>
>> Could you attach all relevant parts of your cxf.xml? That includes e.g.
>> <jaxr:server>. Also attach the object that contains array/collection. Are
>> the collection items annotated as myArray1 or myArray2?
>>
> Unfortunately, given that non-Spring servlet is used, this spring config
> won't be used...And it is only possible to list simple parameters, as
> opposed to the list parameters, for specific providers...
>
> I'll check if a list parameter can be supported easily enough...
>
> Sergey

Re: JSON array serialization workaround not working

Posted by Sergey Beryozkin <sb...@gmail.com>.
Hi
On 27/11/12 15:30, Ladislav Lenčucha wrote:
> Have you checked
> http://svn.apache.org/repos/asf/cxf/trunk/systests/jaxrs/src/test/resources/jaxrs/WEB-INF/beans.xml?
>
> Could you attach all relevant parts of your cxf.xml? That includes e.g.
> <jaxr:server>. Also attach the object that contains array/collection. Are
> the collection items annotated as myArray1 or myArray2?
>
Unfortunately, given that non-Spring servlet is used, this spring config 
won't be used...And it is only possible to list simple parameters, as 
opposed to the list parameters, for specific providers...

I'll check if a list parameter can be supported easily enough...

Sergey

Re: JSON array serialization workaround not working

Posted by Ladislav Lenčucha <la...@gmail.com>.
Have you checked
http://svn.apache.org/repos/asf/cxf/trunk/systests/jaxrs/src/test/resources/jaxrs/WEB-INF/beans.xml?

Could you attach all relevant parts of your cxf.xml? That includes e.g.
<jaxr:server>. Also attach the object that contains array/collection. Are
the collection items annotated as myArray1 or myArray2?

-- 
S pozdravom
Ladislav Lenčucha
ladislav.lencucha@gmail.com



On Tue, Nov 27, 2012 at 2:19 PM, Lars Beiderbecke <
lars.beiderbecke@gmail.com> wrote:

> Hello,
>
> I'm trying to work around the JSON array serialization bug of Jettison
> in conjunction with CXF as described in
>
>    <
> http://cxf.apache.org/docs/jax-rs-data-bindings.html#JAX-RSDataBindings-DealingwithJSONarrayserializationissues
> >
>
> I've created a WEB-INF/classes/cxf.xml file that sets the properties
> mentioned:
>
>   <beans xmlns="http://www.springframework.org/schema/beans"
>        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>        xmlns:util="http://www.springframework.org/schema/util"
>        xsi:schemaLocation="http://www.springframework.org/schema/beans
> http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
> http://www.springframework.org/schema/util
> http://www.springframework.org/schema/util/spring-util-2.0.xsd">
>     <util:list id="jsonKeys">
>       <value>myArray1</value>
>       <value>myArray2</value>
>     </util:list>
>   <bean id="jsonProvider"
> class="org.apache.cxf.jaxrs.provider.json.JSONProvider">
>       <property name="serializeAsArray" value="true"/>
>       <property name="arrayKeys" ref="jsonKeys"/>
>     </bean>
>   </beans>
>
> Alas, this is not working:
>
>    {"MyModel":{"myVal1":"val","myArray1":123,"myArray2":456}}
>
> but I have no idea what is missing here. The log doesn't show anything
> unusual.
>
> Note that I'm using the non-Spring servlet:
>
>   <servlet>
>     <servlet-name>CXFServlet</servlet-name>
>
> <servlet-class>org.apache.cxf.jaxrs.servlet.CXFNonSpringJaxrsServlet</servlet-class>
>     <init-param>
>       <param-name>jaxrs.serviceClasses</param-name>
>       <param-value>test.RestService</param-value>
>     </init-param>
>     <load-on-startup>1</load-on-startup>
>   </servlet>
>
> Is the workaround even supposed to function without Spring? Any help
> would be highly appreciated.
>
> Regards
> Lars
>