Bellow is the snippet from Scala REPL, how do I make a json like {"a":2.0}
?
scala> val f = 2.0
f: Double = 2.0
scala> f
res2: Double = 2.0
scala> val x = play.api.libs.json.Json.obj("a" -> f)
x: play.api.libs.json.JsObject = {"a":2}
Bellow is the snippet from Scala REPL, how do I make a json like {"a":2.0}
?
scala> val f = 2.0
f: Double = 2.0
scala> f
res2: Double = 2.0
scala> val x = play.api.libs.json.Json.obj("a" -> f)
x: play.api.libs.json.JsObject = {"a":2}
In Play JSON library you've got following JSON types:
Which corresponds to JSON types.
It wraps your value into type JsNumber
.
If you would save the same way val f = 2.1
you would receive
x: play.api.libs.json.JsObject = {"a":2.1}
.
JsNumbers behaves like JavaScript numbers, so you don't have to worry about missing zeros at the end. In JavaScript 2
and 2.0
are the same numbers.