You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by Richard Snowden <ri...@gmail.com> on 2014/11/26 20:14:48 UTC
Marshalling of Strings in JSON
I defined a very simple JAX-RS service with JSON. It works well for complex
data types, but when I use simple types, like boolean or String, I get
malformed JSON data.
...
@POST
@Path("/checkString")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public boolean validate(String myString);
...
In order to get, lets say "someString" in the method validate(), I have to
POST exactly this:
someString
If I post
"someString"
or
{"string" : "someString"}
it won't work (won't de-marshall correctly), because the quotes and
brackets are not removed!
Furthermore the reply of the validate() method is no valid JSON either.
It's simply
true (or false)
But not
"true"
or
{"booleanValue" : "true"}
Does CXF have issues with marshalling/demarshalling simple types correctly
to/from JSON?
Re: Marshalling of Strings in JSON
Posted by Sergey Beryozkin <sb...@gmail.com>.
May be CXF can ship a basic delegating JSON provider just to make it
really trivial; I'll try to do it early next week
Cheers, Sergey
On 28/11/14 15:18, Aaron Titus wrote:
> curly braces are object syntax. If you are using JAXB deserializer, it
> needs an object to create, and therefore you'll have curly braces in the
> json. The object can have properties that are primitives like boolean or
> plain String objects and it works just fine for me but as I have stated
> before I'm using JAXB annotations so this may have something to do with it.
> I have not tried it with a pure Jackson mapper and JSON-only annotations.
>
>
> On Fri, Nov 28, 2014 at 9:49 AM, Benson Margulies <be...@basistech.com>
> wrote:
>
>> Of course it won't work with brackets. That is not the json syntax for a
>> plain string.
>> On Nov 28, 2014 9:47 AM, "Aaron Titus" <at...@fwdco.com> wrote:
>>
>>> I think the key is that I am using the JAXB annotations, and not the
>> JAX-RS
>>> ones. I am using jackson 2.2.3 now, but it was working also before with
>>> 1.9. You include the databind jar, and then the JAXB serializer can use
>>> the Jackson Provider. Specifically I'm using this one:
>>>
>>> com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider
>>>
>>>
>>>
>>
>
Re: Marshalling of Strings in JSON
Posted by Sergey Beryozkin <sb...@gmail.com>.
I've prototyped the following:
http://git-wip-us.apache.org/repos/asf/cxf/blob/25b66032/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/provider/json/SimpleTypeJsonProvider.java
Cheers, Sergey
On 28/11/14 21:38, Aaron Titus wrote:
> oops that should have said "and that object class has the properties you
> previously wanted as your parameters"
>
> that makes more sense!
>
>
>
> On Fri, Nov 28, 2014 at 4:36 PM, Aaron Titus <at...@fwdco.com> wrote:
>
>> Yes, this is correct. Incoming REST parameters when simple strings should
>> be specified with the URL for get requestss and with a post they can be
>> form encoded. If you don't want to form encode but instead send a json
>> object then change the method so it takes a single object as its parameter,
>> and that object class has the parameters you want. This is the simplest
>> way to do it and will save Sergey some work ;-)
>>
>>
>> On Fri, Nov 28, 2014 at 10:34 AM, Benson Margulies <be...@basistech.com>
>> wrote:
>>
>>> Then you'd need your parameter to be, say, Map<String, Object>, or
>>> some class, not a plain string.
>>>
>>> On Fri, Nov 28, 2014 at 10:18 AM, Aaron Titus <at...@fwdco.com> wrote:
>>>> curly braces are object syntax. If you are using JAXB deserializer, it
>>>> needs an object to create, and therefore you'll have curly braces in the
>>>> json. The object can have properties that are primitives like boolean or
>>>> plain String objects and it works just fine for me but as I have stated
>>>> before I'm using JAXB annotations so this may have something to do with
>>> it.
>>>> I have not tried it with a pure Jackson mapper and JSON-only
>>> annotations.
>>>>
>>>>
>>>> On Fri, Nov 28, 2014 at 9:49 AM, Benson Margulies <benson@basistech.com
>>>>
>>>> wrote:
>>>>
>>>>> Of course it won't work with brackets. That is not the json syntax for
>>> a
>>>>> plain string.
>>>>> On Nov 28, 2014 9:47 AM, "Aaron Titus" <at...@fwdco.com> wrote:
>>>>>
>>>>>> I think the key is that I am using the JAXB annotations, and not the
>>>>> JAX-RS
>>>>>> ones. I am using jackson 2.2.3 now, but it was working also before
>>> with
>>>>>> 1.9. You include the databind jar, and then the JAXB serializer
>>> can use
>>>>>> the Jackson Provider. Specifically I'm using this one:
>>>>>>
>>>>>> com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider
>>>>>>
>>>>>>
>>>>>>
>>>>>
>>>
>>
>>
>
Re: Marshalling of Strings in JSON
Posted by Aaron Titus <at...@fwdco.com>.
oops that should have said "and that object class has the properties you
previously wanted as your parameters"
that makes more sense!
On Fri, Nov 28, 2014 at 4:36 PM, Aaron Titus <at...@fwdco.com> wrote:
> Yes, this is correct. Incoming REST parameters when simple strings should
> be specified with the URL for get requestss and with a post they can be
> form encoded. If you don't want to form encode but instead send a json
> object then change the method so it takes a single object as its parameter,
> and that object class has the parameters you want. This is the simplest
> way to do it and will save Sergey some work ;-)
>
>
> On Fri, Nov 28, 2014 at 10:34 AM, Benson Margulies <be...@basistech.com>
> wrote:
>
>> Then you'd need your parameter to be, say, Map<String, Object>, or
>> some class, not a plain string.
>>
>> On Fri, Nov 28, 2014 at 10:18 AM, Aaron Titus <at...@fwdco.com> wrote:
>> > curly braces are object syntax. If you are using JAXB deserializer, it
>> > needs an object to create, and therefore you'll have curly braces in the
>> > json. The object can have properties that are primitives like boolean or
>> > plain String objects and it works just fine for me but as I have stated
>> > before I'm using JAXB annotations so this may have something to do with
>> it.
>> > I have not tried it with a pure Jackson mapper and JSON-only
>> annotations.
>> >
>> >
>> > On Fri, Nov 28, 2014 at 9:49 AM, Benson Margulies <benson@basistech.com
>> >
>> > wrote:
>> >
>> >> Of course it won't work with brackets. That is not the json syntax for
>> a
>> >> plain string.
>> >> On Nov 28, 2014 9:47 AM, "Aaron Titus" <at...@fwdco.com> wrote:
>> >>
>> >> > I think the key is that I am using the JAXB annotations, and not the
>> >> JAX-RS
>> >> > ones. I am using jackson 2.2.3 now, but it was working also before
>> with
>> >> > 1.9. You include the databind jar, and then the JAXB serializer
>> can use
>> >> > the Jackson Provider. Specifically I'm using this one:
>> >> >
>> >> > com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider
>> >> >
>> >> >
>> >> >
>> >>
>>
>
>
Re: Marshalling of Strings in JSON
Posted by Aaron Titus <at...@fwdco.com>.
Yes, this is correct. Incoming REST parameters when simple strings should
be specified with the URL for get requestss and with a post they can be
form encoded. If you don't want to form encode but instead send a json
object then change the method so it takes a single object as its parameter,
and that object class has the parameters you want. This is the simplest
way to do it and will save Sergey some work ;-)
On Fri, Nov 28, 2014 at 10:34 AM, Benson Margulies <be...@basistech.com>
wrote:
> Then you'd need your parameter to be, say, Map<String, Object>, or
> some class, not a plain string.
>
> On Fri, Nov 28, 2014 at 10:18 AM, Aaron Titus <at...@fwdco.com> wrote:
> > curly braces are object syntax. If you are using JAXB deserializer, it
> > needs an object to create, and therefore you'll have curly braces in the
> > json. The object can have properties that are primitives like boolean or
> > plain String objects and it works just fine for me but as I have stated
> > before I'm using JAXB annotations so this may have something to do with
> it.
> > I have not tried it with a pure Jackson mapper and JSON-only annotations.
> >
> >
> > On Fri, Nov 28, 2014 at 9:49 AM, Benson Margulies <be...@basistech.com>
> > wrote:
> >
> >> Of course it won't work with brackets. That is not the json syntax for a
> >> plain string.
> >> On Nov 28, 2014 9:47 AM, "Aaron Titus" <at...@fwdco.com> wrote:
> >>
> >> > I think the key is that I am using the JAXB annotations, and not the
> >> JAX-RS
> >> > ones. I am using jackson 2.2.3 now, but it was working also before
> with
> >> > 1.9. You include the databind jar, and then the JAXB serializer can
> use
> >> > the Jackson Provider. Specifically I'm using this one:
> >> >
> >> > com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider
> >> >
> >> >
> >> >
> >>
>
Re: Marshalling of Strings in JSON
Posted by Benson Margulies <be...@basistech.com>.
Then you'd need your parameter to be, say, Map<String, Object>, or
some class, not a plain string.
On Fri, Nov 28, 2014 at 10:18 AM, Aaron Titus <at...@fwdco.com> wrote:
> curly braces are object syntax. If you are using JAXB deserializer, it
> needs an object to create, and therefore you'll have curly braces in the
> json. The object can have properties that are primitives like boolean or
> plain String objects and it works just fine for me but as I have stated
> before I'm using JAXB annotations so this may have something to do with it.
> I have not tried it with a pure Jackson mapper and JSON-only annotations.
>
>
> On Fri, Nov 28, 2014 at 9:49 AM, Benson Margulies <be...@basistech.com>
> wrote:
>
>> Of course it won't work with brackets. That is not the json syntax for a
>> plain string.
>> On Nov 28, 2014 9:47 AM, "Aaron Titus" <at...@fwdco.com> wrote:
>>
>> > I think the key is that I am using the JAXB annotations, and not the
>> JAX-RS
>> > ones. I am using jackson 2.2.3 now, but it was working also before with
>> > 1.9. You include the databind jar, and then the JAXB serializer can use
>> > the Jackson Provider. Specifically I'm using this one:
>> >
>> > com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider
>> >
>> >
>> >
>>
Re: Marshalling of Strings in JSON
Posted by Aaron Titus <at...@fwdco.com>.
curly braces are object syntax. If you are using JAXB deserializer, it
needs an object to create, and therefore you'll have curly braces in the
json. The object can have properties that are primitives like boolean or
plain String objects and it works just fine for me but as I have stated
before I'm using JAXB annotations so this may have something to do with it.
I have not tried it with a pure Jackson mapper and JSON-only annotations.
On Fri, Nov 28, 2014 at 9:49 AM, Benson Margulies <be...@basistech.com>
wrote:
> Of course it won't work with brackets. That is not the json syntax for a
> plain string.
> On Nov 28, 2014 9:47 AM, "Aaron Titus" <at...@fwdco.com> wrote:
>
> > I think the key is that I am using the JAXB annotations, and not the
> JAX-RS
> > ones. I am using jackson 2.2.3 now, but it was working also before with
> > 1.9. You include the databind jar, and then the JAXB serializer can use
> > the Jackson Provider. Specifically I'm using this one:
> >
> > com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider
> >
> >
> >
>
Re: Marshalling of Strings in JSON
Posted by Benson Margulies <be...@basistech.com>.
Of course it won't work with brackets. That is not the json syntax for a
plain string.
On Nov 28, 2014 9:47 AM, "Aaron Titus" <at...@fwdco.com> wrote:
> I think the key is that I am using the JAXB annotations, and not the JAX-RS
> ones. I am using jackson 2.2.3 now, but it was working also before with
> 1.9. You include the databind jar, and then the JAXB serializer can use
> the Jackson Provider. Specifically I'm using this one:
>
> com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider
>
>
>
>
> *Aaron Titus*
> Senior Software Engineer
> F.W. Davison & Company, Inc.
> 508-747-7261 x245
> atitus@fwdco.com
>
>
> On Thu, Nov 27, 2014 at 11:34 AM, Richard Snowden <
> richard.t.snowden@gmail.com> wrote:
>
> > Which version of Jackson do you use?
> >
> > On Thu, Nov 27, 2014 at 5:24 PM, Aaron Titus <at...@fwdco.com> wrote:
> >
> > > I'm not sure if this will help you or not but I am using Jackson as the
> > > provider with CXF, and my object classes are annotated with JAXB
> > > annotations. I'm able to create both JSON and XML without issue, even
> > using
> > > simple types like String and boolean. This may be more overhead than
> you
> > > want? Maybe just create a simple class to use as a wrapper for the
> > return
> > > boolean.
> > >
> >
>
Re: Marshalling of Strings in JSON
Posted by Aaron Titus <at...@fwdco.com>.
I think the key is that I am using the JAXB annotations, and not the JAX-RS
ones. I am using jackson 2.2.3 now, but it was working also before with
1.9. You include the databind jar, and then the JAXB serializer can use
the Jackson Provider. Specifically I'm using this one:
com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider
*Aaron Titus*
Senior Software Engineer
F.W. Davison & Company, Inc.
508-747-7261 x245
atitus@fwdco.com
On Thu, Nov 27, 2014 at 11:34 AM, Richard Snowden <
richard.t.snowden@gmail.com> wrote:
> Which version of Jackson do you use?
>
> On Thu, Nov 27, 2014 at 5:24 PM, Aaron Titus <at...@fwdco.com> wrote:
>
> > I'm not sure if this will help you or not but I am using Jackson as the
> > provider with CXF, and my object classes are annotated with JAXB
> > annotations. I'm able to create both JSON and XML without issue, even
> using
> > simple types like String and boolean. This may be more overhead than you
> > want? Maybe just create a simple class to use as a wrapper for the
> return
> > boolean.
> >
>
Re: Marshalling of Strings in JSON
Posted by Richard Snowden <ri...@gmail.com>.
Which version of Jackson do you use?
On Thu, Nov 27, 2014 at 5:24 PM, Aaron Titus <at...@fwdco.com> wrote:
> I'm not sure if this will help you or not but I am using Jackson as the
> provider with CXF, and my object classes are annotated with JAXB
> annotations. I'm able to create both JSON and XML without issue, even using
> simple types like String and boolean. This may be more overhead than you
> want? Maybe just create a simple class to use as a wrapper for the return
> boolean.
>
Re: Marshalling of Strings in JSON
Posted by Aaron Titus <at...@fwdco.com>.
I'm not sure if this will help you or not but I am using Jackson as the
provider with CXF, and my object classes are annotated with JAXB
annotations. I'm able to create both JSON and XML without issue, even using
simple types like String and boolean. This may be more overhead than you
want? Maybe just create a simple class to use as a wrapper for the return
boolean.
Re: Marshalling of Strings in JSON
Posted by Richard Snowden <ri...@gmail.com>.
On Wed, Nov 26, 2014 at 9:58 PM, Sergey Beryozkin <sb...@gmail.com>
wrote:
> Hi
>
> The default JSON provider used by the JAXRS implementation is JAXB based,
> it does not deal with auto-wrapping of primitive types.
> You can register a Jackson JAXRS provider - it should take care of
> serializing such types.
>
Just tried it with Jackson - unfortunately the same effect.
Re: Marshalling of Strings in JSON
Posted by Sergey Beryozkin <sb...@gmail.com>.
Hi
The default JSON provider used by the JAXRS implementation is JAXB
based, it does not deal with auto-wrapping of primitive types.
You can register a Jackson JAXRS provider - it should take care of
serializing such types.
Another option is a bit more involved, but would also work, register a
custom JAXRS MessageBodyWriter which would have support all types. It
would have "@Context Providers p;" injected and in its writeTo method it
would either create a JSON representation of the primitive types or
delegate, via the Providers instance to another registered (possibly
default) JSON provider which will manage complex types
Cheers, Sergey
On 26/11/14 19:14, Richard Snowden wrote:
> I defined a very simple JAX-RS service with JSON. It works well for complex
> data types, but when I use simple types, like boolean or String, I get
> malformed JSON data.
>
> ...
> @POST
> @Path("/checkString")
> @Produces(MediaType.APPLICATION_JSON)
> @Consumes(MediaType.APPLICATION_JSON)
> public boolean validate(String myString);
> ...
>
>
> In order to get, lets say "someString" in the method validate(), I have to
> POST exactly this:
> someString
>
> If I post
> "someString"
> or
> {"string" : "someString"}
> it won't work (won't de-marshall correctly), because the quotes and
> brackets are not removed!
>
>
> Furthermore the reply of the validate() method is no valid JSON either.
> It's simply
> true (or false)
>
> But not
> "true"
> or
> {"booleanValue" : "true"}
>
>
> Does CXF have issues with marshalling/demarshalling simple types correctly
> to/from JSON?
>
--
Sergey Beryozkin
Talend Community Coders
http://coders.talend.com/
Blog: http://sberyozkin.blogspot.com