lift-json ãªãã¡ã¬ã³ã¹
åè: JSON Support | Lift Project | Assembla
Scalaæ¨æºã«ãjsonãã¼ãµã¨ãããã¾ãããlift-jsonã®ã»ãã人æ°ã
åºç¤ç¥è
A central concept in lift-json library is JSON AST (Abstract Syntax Tree) which models the structure of a JSON document as a syntax tree; all features are implemented in terms of AST. It provides a case class for each of the above mentioned JSON primitive and structured type, and provides functions used to transform the AST itself, or to transform the AST between different formats.
å³ã§ã¾ã¨ããã¨ãããªãã¨ã
以ä¸ã²ããã使ãæ¹ & ãµã³ãã«
import
package objectãimportãããã¨ã§JValueã¨ãparse,renderãªã©ã®ã¡ã½ããã使ããããã«ãªãã¾ãã
import net.liftweb.json._
Jsonæåå -> JsonAST
parse ã使ã
val data = """ { "name": "joe", "address": { "street": "Bulevard", "city": "Helsinki" }, "children": [ { "name": "Mary", "age": 5 "birthdate": "2004-09-04T18:06:22Z" }, { "name": "Mazy", "age": 3 } ] } """ val json = parse(data) println(json) /* JObject(List(JField(name,JString(joe)), JField(address,JObject(List(JField(street,JString(Bulevard)), JField(city,JString(Helsinki))))), JField(children, JArray( List(JObject(List(JField(name,JString(Mary)), JField(age,JInt(5)), JField(birthdate,JString(2004-09-04T18:06:22Z)))), JObject(List(JField(name,JString(Mazy)), JField(age,JInt(3))))))))) */
JsonASTããã®å¤ã®åãåºã
for ã§åä¾ã®ååãåãåºãã¦ã¿ã¾ãã
for { JObject(items) <- json item <- items JField("children", JArray(children)) <- item JObject(child) <- children JField("name", JString(name))<- child } { println(name) } /* Mary Mazy */
forå¼ä¾¿å©ãã§ãã¡ãã£ã¨ããã°ã£ã¦ãæããã¾ããã
XPathã®ãããªæ¸ãæ¹ã§åãåºããã¨ãã§ãã¾ãã
println(json \\ "children" \ "name") /* JArray(List(JField(name,JString(Mary)), JField(name,JString(Mazy)))) */
JsonAST -> case class
extract ã使ã㨠case class ã¸ã®ãããã³ã°ãã§ãã¾ãã
case class Person(name: String, address: Address, children: List[Child]) case class Address(street: String, city: String) case class Child(name: String, age: Int, birthdate: Option[java.util.Date]) println(json.extract[Person]) /* Person(joe,Address(Bulevard,Helsinki),List(Child(Mary,5,Some(Sun Sep 05 03:06:22 JST 2004)), Child(Mazy,3,None))) */
2012/01/05 追è¨
âãããããã¨ãã§ããããã
Lift Json の case class への変換がとても便利な件 - scalaとか・・・
JsonAST -> Jsonæåå
render 㧠scala.text.Document ã«ãã¦ãã pretty or compact
println(compact(render(json))) /* {"name":"joe","address":{"street":"Bulevard","city":"Helsinki"},"children":[{"name":"Mary","age":5,"birthdate":"2004-09-04T18:06:22Z"},{"name":"Mazy","age":3}]} */ println(pretty(render(json))) /* { "name":"joe", "address":{ "street":"Bulevard", "city":"Helsinki" }, "children":[{ "name":"Mary", "age":5, "birthdate":"2004-09-04T18:06:22Z" },{ "name":"Mazy", "age":3 }] } */
case class -> Jsonæåå
Serialization.write ã使ãã¾ãã
import org.scala_tools.time.Imports._ implicit val format = DefaultFormats println(Serialization.write( List(Person("joe" ,Address("Bulevard", "Helsinki"), List(Child("Mary", 5 ,Some(DateTime.now - 5.year toDate)), Child("Mazy", 3, None))), Person("john" ,Address("Bulevard", "Helsinki"), List(Child("Mary", 5 ,Some(DateTime.now - 5.year toDate)), Child("Mazy", 3, None)))) )) /* [{"name":"joe","address":{"street":"Bulevard","city":"Helsinki"},"children":[{"name":"Mary","age":5,"birthdate":"2007-01-03T06:55:16Z"},{"name":"Mazy","age":3}]}, {"name":"john","address":{"street":"Bulevard","city":"Helsinki"},"children":[{"name":"Mary","age":5,"birthdate":"2007-01-03T06:55:16Z"},{"name":"Mazy","age":3}]}] */ }
JsonDSLã使ã£ã¦JsonASTãçµã¿ç«ã¦ã
DSLã¨è¨ãã»ã©ã®ãã®ã§ã¯ãªãæ°ãããã
Map -> JValue, Option[A] -> JValue, Int -> JInt ãªã©ã® implicit conversionãããããå®ç¾©ããã¦ããã®ã¨
ãã¨(String, A)ãJsonAssoc[A]ã£ã¦ã®ã«å¤æããã¦ããã®ããã㧠"~" ã¨ããã¡ã½ããã使ããããã«ãªãã¾ãã
import net.liftweb.json.JsonDSL._ val beatles: JValue = ("Guitar" -> List("John", "George")) ~ ("Bass" -> "Paul") ~ ("Ringo" -> "Drums") println(pretty(render(beatles))) /* { "Guitar":["John","George"], "Bass":"Paul", "Ringo":"Drums" } */