amnaredo / test

0 stars 0 forks source link

json to map not working ? #96

Open amnaredo opened 3 years ago

amnaredo commented 3 years ago

Hi , I am trying to convert json string to Map

read[Map[String,Int]]("""{ "i" : 123}""")

it failing with message

Uncaught upickle.Invalid$Data: data: Obj(ArrayBuffer((i,Num(123)))) msg: Array(n) 

ID: 46 Original Author: chandu0101

amnaredo commented 3 years ago

There'd need to be this Reader in upickle implicits (or you might add it yourself to your app) :

  implicit def MapR[V: R] = R[Map[String, V]](
    Internal.validate("Obj(Array(n))"){
      case Js.Obj(arr@_*) => arr.map { case (k,v) => (k, readJs[V](v))}.toMap
    }
  )

I'm not sure what has changed but I have the same problem... This generic MapR :

  implicit def MapR[K: R, V: R] = R[Map[K, V]](
    Internal.validate("Array(n)"){case x: Js.Arr => x.value.map(readJs[(K, V)]).toMap}
  )

fails, because x is Js.Obj instead of Js.Arr

@lihaoyi do you think the Reader has its place in upickle implicits? Or is it more complicated than that ?

Original Author: l15k4

amnaredo commented 3 years ago

thank you ,its working now .

Original Author: chandu0101

amnaredo commented 3 years ago

It's more complicated than that. Let's close this in favor of https://github.com/lihaoyi/upickle/issues/39

Original Author: lihaoyi