How can I construct and parse a JSON string in Scala / Lift

后端 未结 2 1745
滥情空心
滥情空心 2020-12-04 06:53

I am attempting to use JSON to send data between the browser and my app.

I am attempting to use Lift 1.0 to create and parse JSON strings, but for some reason I am u

2条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-04 07:13

    Take a look at Circe. It's really nice to use and it leverages some of the new tools from Shapeless and Cats. Plus, you can use it from Scala compiled to Javascript.

    Taken from the Circe readme:

    scala> import io.circe., io.circe.generic.auto., io.circe.parser., io.circe.syntax. import io.circe._ import io.circe.generic.auto._ import io.circe.parser._ import io.circe.syntax._

    scala> sealed trait Foo defined trait Foo

    scala> case class Bar(xs: List[String]) extends Foo defined class Bar

    scala> case class Qux(i: Int, d: Option[Double]) extends Foo defined class Qux

    scala> val foo: Foo = Qux(13, Some(14.0)) foo: Foo = Qux(13,Some(14.0))

    scala> foo.asJson.noSpaces res0: String = {"Qux":{"d":14.0,"i":13}}

    scala> decodeFoo res1: cats.data.Xor[io.circe.Error,Foo] = Right(Qux(13,Some(14.0)))

提交回复
热议问题