ferk86
ferk86

Reputation: 2345

implicit String to JsString

Instead of creating a JsObject idiomatically like

Json.obj("foo" -> "bar")

it seems easier in my case to build one up with a

JsObject then += (String, JsValue).

The problem is a JsValue is required so something like

json += "foo" -> "bar" 

will not compile, and need to be written as

json += "foo" -> JsString("bar")

How can you still use the implicit conversions that Json.obj(...) has? Thanks.

Upvotes: 0

Views: 626

Answers (1)

Yann Moisan
Yann Moisan

Reputation: 8281

Given an existing JsObject

scala> Json.obj("foo" -> "bar")
res23: play.api.libs.json.JsObject = {"foo":"bar"}

If you want to add to it a name/value pair, you can do :

scala> res23 ++ Json.obj("john" -> "doe")
res24: play.api.libs.json.JsObject = {"foo":"bar","john":"doe"}

Or

scala> res23 + ("john" -> JsString("doe"))
res32: play.api.libs.json.JsObject = {"foo":"bar","john":"doe"}

Upvotes: 1

Related Questions