0

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}
Rumid
  • 1,627
  • 2
  • 21
  • 39
lprakashv
  • 1,121
  • 10
  • 19

1 Answers1

0

In Play JSON library you've got following JSON types:

  • JsString
  • JsNumber
  • JsBoolean
  • JsObject
  • JsArray
  • JsNull

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.

Rumid
  • 1,627
  • 2
  • 21
  • 39