[2.0] json JsNumber integer or decimal problem

285 views
Skip to first unread message

Simon Chan

unread,
Jan 16, 2012, 9:44:22 AM1/16/12
to play-fr...@googlegroups.com
This line of code:
Ok(toJson(JsObject(List( "status"->JsNumber(1) ))))
generates a json response like this:
{"status":1.0}

However, I expect to get this instead:
{"status":1}

Would there be a solution? Thanks!

Pascal Voitot Dev

unread,
Jan 18, 2012, 5:45:21 PM1/18/12
to play-fr...@googlegroups.com
In current code, a Int/Long/Float/Double is mapped to a JsNumber which is serialized as a double.
You can't do much more for the time being.

Pascal

--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To view this discussion on the web visit https://groups.google.com/d/msg/play-framework/-/LLah9HvOQxkJ.
To post to this group, send email to play-fr...@googlegroups.com.
To unsubscribe from this group, send email to play-framewor...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/play-framework?hl=en.

Ike

unread,
Jan 20, 2012, 10:31:04 AM1/20/12
to play-fr...@googlegroups.com
I've requested this in the past and even submitted a Pull request with changes to support Integers and Floats. But I think this part of the code is still very much in flux. Hopefully the team will consider this request when the JSON library is stabilized.

Erwan Loisant

unread,
Jan 20, 2012, 11:40:43 AM1/20/12
to play-fr...@googlegroups.com
Yes, we'll be fixing that soon.

> --
> You received this message because you are subscribed to the Google Groups
> "play-framework" group.
> To view this discussion on the web visit

> https://groups.google.com/d/msg/play-framework/-/0dSGgcPCSAkJ.


>
> To post to this group, send email to play-fr...@googlegroups.com.
> To unsubscribe from this group, send email to
> play-framewor...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/play-framework?hl=en.

--
Erwan Loisant

Pascal Voitot Dev

unread,
Jan 20, 2012, 11:55:42 AM1/20/12
to play-fr...@googlegroups.com
+1 ;)

Simon Chan

unread,
Jan 21, 2012, 6:12:51 PM1/21/12
to play-fr...@googlegroups.com
+1 also

Thanks.

Marc Siegel

unread,
Sep 13, 2012, 3:42:15 PM9/13/12
to play-fr...@googlegroups.com
+1, and seriously, what's with the delay?

It's still not working in 2.0.3!

Sadache Aldrobi

unread,
Sep 13, 2012, 3:58:05 PM9/13/12
to play-fr...@googlegroups.com
Actually this is fixed on master:

scala> JsObject(List( "status"->JsNumber(1) ))
res1: play.api.libs.json.JsObject = {"status":1}

scala> JsObject(List( "status"->JsNumber(1.0) ))
res2: play.api.libs.json.JsObject = {"status":1.0}

On Thu, Sep 13, 2012 at 9:42 PM, Marc Siegel <marc....@timgroup.com> wrote:
+1, and seriously, what's with the delay?

It's still not working in 2.0.3!

--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To view this discussion on the web visit https://groups.google.com/d/msg/play-framework/-/e0WlFXgJGgcJ.

To post to this group, send email to play-fr...@googlegroups.com.
To unsubscribe from this group, send email to play-framewor...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/play-framework?hl=en.



--
www.sadekdrobi.com
ʎdoɹʇuǝ

Marc Siegel

unread,
Sep 13, 2012, 4:08:37 PM9/13/12
to play-fr...@googlegroups.com
That also works in Play 2.0.3. But try using org.specs2.JsonMatchers in order to write tests on the resulting json.

For example:

val s = JsObject(List( "status"->JsNumber(1234L) ))
// s: String = {"status":1234}
s must /("status" -> 1234L)
// [error]  [{"status" : 1234.0}] doesn't contain: status -> 1234

Does this make sense?
-Marc

Marc Siegel

unread,
Sep 13, 2012, 4:14:46 PM9/13/12
to play-fr...@googlegroups.com
Ah ha, I've got it.

The actual problem was in a JsArray, like this:
    var s = toJson(List(Map( "status" -> 1234L))).toString
    // s: String = [{"status":1234}]

The following error is misleading:

    s must /("status" -> 1234L)
    // [error]  [{"status" : 1234.0}] doesn't contain: status -> 1234

The actual problem is using the JsonDeepPairMatcher (notice the star in the next line) to get down through the JsArray:
    s must */("status" -> 1234L)

The error text, which shows the JsNumber with a decimal point, was the red herring. In fact, as you can see above, the serialization of the json did not actually contain the decimal point.

Thanks for your help,
-Marc
Reply all
Reply to author
Forward
0 new messages