json - Why do we need to explicitly use the implicit method on map value? -
this working example:
import spray.json._ trait jsonsupport { implicit def string2jsvalue(s: string): jsvalue = { jsstring(s) } implicit def map2jsobject(m: map[string, jsvalue]): jsobject = { jsobject(m) } implicit def mapstring2jsobject(m: map[string, string]): jsobject = { jsobject(m.map { case (k,v) => (k,string2jsvalue(v)) }) } }
as stands now, working. if replace
implicit def mapstring2jsobject(m: map[string, string]): jsobject = { jsobject(m.map { case (k,v) => (k,string2jsvalue(v)) }) }
with
implicit def mapstring2jsobject(m: map[string, string]): jsobject = { jsobject(m) }
then following error occurs:
expected: map[string, jsvalue]
, actual: map[string, string]
why implicit method not doing work , need call implicitly?
is possible use implicits in way such there no need write implicit methods every possible nesting of map
? e.g.:
implicit def map1string2jsobject(m: map[string, string]): jsobject implicit def map2string2jsobject(m: map[string, map[string, string]]): jsobject implicit def map3string2jsobject(m: map[string, map[string, map[string, string]]]): jsobject ...
in line case (k,v) => (k,v)
haven't specified result of case should other (string,string). force implicit conversion, write:
case (k,v) => (k,v:jsvalue)
Comments
Post a Comment