upickle read from scalaJS - upickle.Invalid$Data: String (data: 1)

点点圈 提交于 2020-01-07 02:41:18

问题


From ScalaJS.

import upickle.default._
import scala.scalajs.concurrent.JSExecutionContext.Implicits.queue

case class Post(userId: Long, id: Long, title: String, body: String)

@JSExport
def posts() : Future[Seq[Post]] = {
  val txt = """[{
    "userId": 1,
    "id": 1,
    "title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
    "body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"
  }]"""

  val p = read[Seq[Post]](txt)

  println(p)

  p

}

pp onFailure {
  case x => println(x)
}

println(x) prints: upickle.Invalid$Data: String (data: 1)

How to make it work?

Updated / sync up with: https://github.com/lihaoyi/upickle-pprint/issues/154


回答1:


Hm..

"uPickle serializes Longs as "1" in order to avoid corruption/truncation for large numbers in Scala.js. If they're coming down as JSON Numbers, you need to type them as Ints or Doubles"



来源:https://stackoverflow.com/questions/36879109/upickle-read-from-scalajs-upickle-invaliddata-string-data-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!