WARNING: This page is a guide for 1.x series. See here instead to learn about the latest

Working with JSON


Easy-to-use json4s wrapper


SkinnyController provides the following useful methods from JSONStringOps trait by default.

json/src/main/scala/skinny/util/JSONStringOps.scala

The following methods come from JSONFeature trait.

framework/src/main/scala/skinny/controller/feature/JSONFeature.scala

import skinny.util.JSONStringOps._

case class UserResponse(user: User)
case class User(name: String, age: Int, email: String, 
  isAlive: Boolean = false, friend: Option[User] = None)

val jsonString =
  """{
    |  "user": {
    |    "name" : "toto",
    |    "age" : 25,
    |    "email" : "toto@jmail.com",
    |    "isAlive" : true,
    |    "friend" : {
    |      "name" : "tata",
    |      "age" : 20,
    |      "email" : "tata@coldmail.com"
    |    }
    |  }
    |}
  """.stripMargin

val userResponse: Option[UserResponse] = fromJSONString[UserResponse](jsonString)

val jsonString2 = userResponse.map(r => toJSONString(r))

See also: json/src/test/scala/skinny/util/JSONStringOpsSpec.scala

If you need more complex operations, use json4s directly.

If you find a typo or mistake in this page, please report or fix it. How?