12

A JSON string:

{
    "name": "Mike",
    "age": 111
}

A case class:

case class User(name:String, age:Int)

If I use scala, there are many libraries can let me convert the JSON to the case class and vice versa, but they can't be used in scala.js.

How to do this work in scala.js?

Freewind
  • 193,756
  • 157
  • 432
  • 708

3 Answers3

9

Use one of the pickling libraries that work with Scala.js. The following two produce well-behaved and predictable JSON:

sjrd
  • 21,805
  • 2
  • 61
  • 91
  • Thanks for recommending this, they are just what I'm looking for. And just found the author of `Prickle` is my friend ... – Freewind Feb 18 '15 at 15:23
  • 2
    Deprecated repository for uPickle/PPrint. PPrint now lives at https://github.com/lihaoyi/pprint, and uPickle is deprecated in favor of https://github.com/playframework/play-json – Waldemar Wosiński Sep 09 '17 at 08:13
1

Circe has more than 1000 stars: https://github.com/circe/circe

Tvaroh
  • 6,645
  • 4
  • 51
  • 55
Waldemar Wosiński
  • 1,490
  • 17
  • 28
  • circe, can we used with scalajs as some other inherent java libs may required when import jar packages in scvalajs client side during final bundle js generataed with fatOptJS/fullOptJS. It may cause issue? – donald Sep 12 '18 at 03:58
  • uPickle is faster than Play-Json, Circe, or Argonaut by a large margin. – donald Oct 16 '18 at 07:14
1

More safe and efficient options are:

Bellow are results of benchmarks for parsing and serialization of a message sample from the Twitter API:

Twitter API Reading Twitter API Writing

Andriy Plokhotnyuk
  • 7,883
  • 2
  • 44
  • 68