Scala object to Json with Play API -
i need handle case hasn't been answered question: convert scala object json
basically, don't know how handle list , map types. i've tried following doesn't work:
implicit val anyvalwriter = writes[any] (a => match { case v: double => json.tojson(v) case v: float => json.tojson(v) case v: long => json.tojson(v) case v: int => json.tojson(v) case v: string => json.tojson(v) case v: iterable[any] => json.tojson(v.map(t => json.tojson(t)).tolist) case v: map[string, any] => jsobject(v.map { case (k, v) => (k, json.tojson(v)) }.tolist) // or, if don't care value case _ => throw new runtimeexception("type not serializable.") })
the resulting error is: no json serializer found type any. try implement implicit writes or format type.
trying add anyvalwriter
json.tojson
results in: recursive value anyvalwriter needs type
any idea?
i sorted problem out through workaround. i've chosen not handle iterable
& map
types in writes[any]
, used wrapper handle both types in case any
won't occurrence of either list[...]
or map[string, any]
.
here's code interested:
implicit val anyvalwriter = writes[any] (a => match { case v: double => json.tojson(v) case v: float => json.tojson(v) case v: long => json.tojson(v) case v: int => json.tojson(v) case v: string => json.tojson(v) case v: jsarray => v // or, if don't care value case _ => throw new runtimeexception("type not serializable.") })
and wrapper:
def converttojsonvalue(v : iterable[map[string, any]])(implicit write: writes[any]) = { jsarray(v.map(t => jsobject(t.map { case (k, value) => (k, json.tojson(value)(write)) }.tolist)).tolist) }
i'll find out later if can sort out ugly jsarray
case.
ps: know it's wrong i'm using sql api returns map[string, any] don't know columns returned (and have reasons , not use orm feature).
Comments
Post a Comment