In JSON output how do you write a number as a number and not as a string?

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

In JSON output how do you write a number as a number and not as a string?

nbaliga
I upgraded to Jackson 1.9.

I am using Jersey 1.5.

I have set up a Provider to provide a Jackson ObjectMapper instance that I have configured with the following property:


@Component
@Provider
public class JacksonObjectMapperProvider
      implements ContextResolver<ObjectMapper>
{
objectMapper.configure( JsonGenerator.Feature.WRITE_NUMBERS_AS_STRINGS, false );
...
}


I have also set up the initParam enabling the POJOMappingFeature in my web.xml

I am now noticing that numbers are being serialized in JSON, with quotes.

I tried a test case using the same ObjectMapper provider and the numbers get serialized without any quotes.

This implies that Jackson is doing what it's supposed to when used on it's own, but somehow the Jersey + Jackson combination is causing numbers to be quoted.

Any clues?
Reply | Threaded
Open this post in threaded view
|

Re: In JSON output how do you write a number as a number and not as a string?

Marek Potociar
I'm confused. What is the setup where you see qoutes?

What is the setup where you don't see the quotes?

Marek

On 02/16/2012 10:26 PM, nbaliga wrote:

> I upgraded to Jackson 1.9.
>
> I am using Jersey 1.5.
>
> I have set up a Provider to provide a Jackson ObjectMapper instance that I
> have configured with the following property:
>
> /
> @Component
> @Provider
> public class JacksonObjectMapperProvider
>       implements ContextResolver<ObjectMapper>
> {
> objectMapper.configure( JsonGenerator.Feature.WRITE_NUMBERS_AS_STRINGS,
> false );
> ...
> }
> /
>
> I have also set up the initParam enabling the POJOMappingFeature in my
> web.xml
>
> I am now noticing that numbers are being serialized in JSON, with quotes.
>
> I tried a test case using the same ObjectMapper provider and the numbers get
> serialized without any quotes.
>
> This implies that Jackson is doing what it's supposed to when used on it's
> own, but somehow the Jersey + Jackson combination is causing numbers to be
> quoted.
>
> Any clues?
>
> --
> View this message in context: http://jersey.576304.n2.nabble.com/In-JSON-output-how-do-you-write-a-number-as-a-number-and-not-as-a-string-tp7292424p7292424.html
> Sent from the Jersey mailing list archive at Nabble.com.
Reply | Threaded
Open this post in threaded view
|

Re: In JSON output how do you write a number as a number and not as a string?

nbaliga
After I upgraded to Jackson 1.9, I started seeing the quotes on numbers despite having explicitly set the WRITE_NUMBERS_AS_STRINGS to false (the default is also false).

Previously, when I was on Jackson 0.9, the numbers appeared without the quotes.

In both cases, the Jersey version is 1.5.

With Jackson 0.9, I was supplying a JaxbContextResolver via

@Component
@Provider
public class JAXBContextResolver
      implements ContextResolver<JAXBContext>
{...



With Jackson 1.9, I am supplying Jackson ObjectMapper via

@Component
@Provider
public class JacksonObjectMapperProvider
      implements ContextResolver<ObjectMapper>
...


But like I mentioned Jackson 1.9 on it's own doesn't quote numbers.
Reply | Threaded
Open this post in threaded view
|

Re: In JSON output how do you write a number as a number and not as a string?

nbaliga
Perhaps I am confusing the issue by providing a before and after reference.

In the end, I am trying to use the POJO Mapping Feature in Jersey 1.5 and am providing a configured Jackson ObjectMapper instance via a ContextResolver<ObjectMapper> implementation.

I am using Jackson 1.9.

In this scenario, I am seeing numbers being serialized into JSON as quoted strings instead of just numbers.

When I use ObjectMapper on its own i.e. no Jersey in the picture, a simple Main class, then the numbers come out without quotes, as they should.

This little test case was my attempt to figure out whether Jackson was the culprit and not obeying it's WRITE_NUMBERS_AS_STRING=false configuration.

So my feeling here is that there is something in the way Jersey 1.5 is using Jackson 1.9's ObjectMapper that's causing the problem.

It could be that I may be missing some configuration setting.
Reply | Threaded
Open this post in threaded view
|

Re: In JSON output how do you write a number as a number and not as a string?

Glen Mazza
Jersey's 1.11's jacksonjsonprovider sample[1], if you modify its
NonJAXBBean class, adding an "anyInt" variable as shown:

public class NonJAXBBean {

     public String name = "non-JAXB-bean";
     public String description = "I am not a JAXB bean, just an
unannotated POJO";
     public int anyInt = 7; <---  added
     public int[] array = {1, 1, 2, 3, 5, 8, 13, 21, 34};
}

And then run the tutorial as discussed in its README and go to
http://localhost:9998/jacksonjsonprovider/nonJAXBResource, from a
browser, it will return:

callback({
   "name" : "non-JAXB-bean",
   "description" : "I am not a JAXB bean, just an unannotated POJO",
   "anyInt" : 7,
   "array" : [ 1, 1, 2, 3, 5, 8, 13, 21, 34 ]
})


with no quotes around the anyInt value of 7, as shown above.

HTH,
Glen

[1]
http://java.net/projects/jersey/sources/svn/show/trunk/jersey/samples/jacksonjsonprovider?rev=5657

On 02/17/2012 09:30 AM, nbaliga wrote:

> Perhaps I am confusing the issue by providing a before and after reference.
>
> In the end, I am trying to use the POJO Mapping Feature in Jersey 1.5 and am
> providing a configured Jackson ObjectMapper instance via a
> ContextResolver<ObjectMapper>  implementation.
>
> I am using Jackson 1.9.
>
> In this scenario, I am seeing numbers being serialized into JSON as quoted
> strings instead of just numbers.
>
> When I use ObjectMapper on its own i.e. no Jersey in the picture, a simple
> Main class, then the numbers come out without quotes, as they should.
>
> This little test case was my attempt to figure out whether Jackson was the
> culprit and not obeying it's WRITE_NUMBERS_AS_STRING=false configuration.
>
> So my feeling here is that there is something in the way Jersey 1.5 is using
> Jackson 1.9's ObjectMapper that's causing the problem.
>
> It could be that I may be missing some configuration setting.
>
> --
> View this message in context: http://jersey.576304.n2.nabble.com/In-JSON-output-how-do-you-write-a-number-as-a-number-and-not-as-a-string-tp7292424p7294441.html
> Sent from the Jersey mailing list archive at Nabble.com.


--
Glen Mazza
Talend Community Coders - coders.talend.com
blog: www.jroller.com/gmazza

Reply | Threaded
Open this post in threaded view
|

Re: In JSON output how do you write a number as a number and not as a string?

nbaliga
User error on my part.

I had an XmlJavaTypeAdapter to convert from String -> Double and vice versa. This was for some JSR303 validation bug I was trying to fix.

When I removed this adapter from my Double attribute, its JSON representation showed up without quotes just fine.

Thanks all for your input.