users@jersey.java.net

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

From: Glen Mazza <gmazza_at_talend.com>
Date: Fri, 17 Feb 2012 15:53:41 -0500

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