sjsonã®ä½¿ãæ¹
Scalaã§JSONãæ±ãããã®ã©ã¤ãã©ãªãsjsonã«ã¤ãã¦æ¸ãã¾ãã
ããããScalaã§ã¯æ¨æºã©ã¤ãã©ãªã«JSONã®ãã¼ãµã¼ãã¤ãã¦ã¦ãJSONæååããã¼ã¹ãã¦case classã«å ¥ãã¦ããããããã®ãã¨ã¯ãã¦ããã¾ãããã ãã
- è¿ãå¤ã®åã Option[List[Any] ] ã ã£ãã
- ãã¼ã¹çµæã®JSONObjectï¼case classï¼ãtoStringãã¦ãJSONã«ãªããªãã£ãã
ãªãã§æ¨æºã©ã¤ãã©ãªã«åå¨ãã¦ããã®ãä¸æã§ãããJSONãããªããªã¢ããªã±ã¼ã·ã§ã³å ã§ä½¿ãããã®æ©è½ã¯ããã£ã¦ãªãã§ããï¼ã³ããæ¬ã«è¼ã£ã¦ããµã³ãã«ã¾ãã¾ãªæãã§ãï¼
JSON.scala in scala/tags/R_2_8_0_final/src/library/scala/util/parsing/json – Scala
ãã¨ã¯ãJavaã®ã©ã¤ãã©ãªãããããããã®ã§ããã使ã£ã¦ããããã§ãããå½ç¶ã®ãã¨ãªããã¤ã³ã¿ã¼ãã§ã¤ã¹ãJavaã£ã½ãã®ã§ãScalaã®ã³ã¬ã¯ã·ã§ã³ãªãã¸ã§ã¯ããXMLãªãã©ã«ã渡ãã¦ï½³ï¾ï½°ãã¨ãããã¨ãã§ãã¾ããã
implicit defã¨ãã©ããã¼ãæ¸ãã°ãããã§ãããããã¾ã§ããªãã¦ãScalaç¨ã®JSONã©ã¤ãã©ãªã¯ä»ã«ãããã¾ãããã¨ããããã§sjsonã§ãã
sbtã¨ãmavenã®ãªãã¸ããªè¨å®
sjsonã®ããã±ã¼ã¸ã¯scala-tools.orgã®Mavenãªãã¸ããªã«å
¥ã£ã¦ããã®ã§ãsbtã®å ´åã¯ãªãã¸ããªã®è¨å®ãä¸è¦ã§ãã
以ä¸ã®ããã«ããã¸ã§ã¯ããã¡ã¤ã«ã«sjsonã®ã¢ã¼ãã£ãã¡ã¯ãIDçã
ãæ¸ãã°OKã§ãã
val json = "net.debasishg" %% "sjson_2.8.0" % "0.8"
Mavenã使ãå ´åããsjsonã®ã©ã¤ãã©ãªã¯Scalaã©ã³ã¿ã¤ã ã¨åããªãã¸ããªã«ããã®ã§ãScalaã©ã³ã¿ã¤ã ã¸ã®dependencyãæ¸ãã¦ããã°ããã¨ã¯sjsonã¸ã®dependencyã追å ããã ãã§OKã§ãã
<repositories> <repository> <id>scala-tools.org</id> <url>http://scala-tools.org/repo-releases</url> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>scala-tools.org</id> <url>http://scala-tools.org/repo-releases</url> </pluginRepository> </pluginRepositories> <dependencies> <dependency> <groupId>org.scala-lang</groupId> <artifactId>scala-library</artifactId> <version>2.8.0.final</version> </dependency> <dependency> <groupId>net.debasishg</groupId> <artifactId>sjson_2.8.0</artifactId> <version>0.8</version> </dependency> </dependencies>
ãªãã¸ã§ã¯ã=>JSONã¸ã®ã·ãªã¢ã©ã¤ãº
scala> import sjson.json.JsonSerialization import sjson.json.JsonSerialization scala> JsonSerialization.tojson(Map("a" -> "apple", "b" -> "Borland", "c" -> "Citrix")) res0: dispatch.json.JsValue = {"a" : "apple", "b" : "Borland", "c" : "Citrix"}
ãããªæãã§ãMap, List, Tuple2ã22ããããªãããã©ã«ãã§å¯¾å¿ãã¦ãã¾ãã
対å¿ãã¦ããªãåãå
¥ããã¨ãä¾å¤ãåºãã¾ãã
scala> case class Neko(name : String, color : String, age : Int) defined class Neko scala> JsonSerialization.tojson(Neko("tama", "white", 666)) <<console>:34: error: could not find implicit value for parameter tjs: sjson.json.Writes[Neko] JsonSerialization.tojson(Neko("tama", "white", 666)) ^
æ°ããªåã«å¯¾å¿ãããããã«ã¯ãProtocolãæ¸ãå¿ è¦ãããã¾ãã
scala> object NekoProtocol extends DefaultProtocol { | implicit val NekoFormat : Format[Neko] = | asProduct3("name", "color", "age")(Neko)(Neko.unapply(_).get) | } defined module NekoProtocol scala> import NekoProtocol._ import NekoProtocol._ scala> JsonSerialization.tojson(Neko("tama", "white", 666)) res0: dispatch.json.JsValue = {"name" : "tama", "color" : "white", "age" : 666}
ãã®ã¸ãã®è©³ç´°ã«ã¤ãã¦ã¯ä½è ã®ããã°ã¨ã³ããªã訳ããã¦ããã®ã§ããã¡ããåç §ããã¨è¯ãã¨æãã¾ãã
JSON=>ãªãã¸ã§ã¯ã
ãã£ãã®éã§ããã以ä¸ã®ã¨ããã§ãã
scala> import sjson.json.JsonSerialization import sjson.json.JsonSerialization scala> JsonSerialization.tojson(Neko("tama", "white", 666)) res0: dispatch.json.JsValue = {"name" : "tama", "color" : "white", "age" : 666} scala> JsonSerialization.fromjson[Neko](res2) res1: Neko = Neko(tama,white,666)
å®è£
ãã®sjsonã¯ãããããã¨é¢ç½ãå®è£ ã«ãªã£ã¦ãã¦ãä»»æã®åã«å¯¾ãã¦JSONã¸ã·ãªã¢ã©ã¤ãºããããã®ãããã³ã«ãå®ç¾©ã§ãããããã®ãããã³ã«ã®æå®ãimplicit parameterã§ãããã¦ããã§ãã³ã¼ããèªãã¨åå¼·ã«ãªãã¾ãã
ã㨠sjson.json.Generic.scala ãè¦ãã¨ãè¬ã®ãã¯ãçãªãã®ããããã§ããããªãã ããã©ããªã£ã¦ããã ãã¨ããæãã§ãã
<#list 2..9 as i> <#assign typeParams><#list 1..i as j>T${j}<#if i !=j>,</#if></#list></#assign> def asProduct${i}[S, ${typeParams}](<#list 1..i as j>f${j}: String<#if i != j>,</#if></#list>)(apply : (${typeParams}) => S)(unapply : S => Product${i}[${typeParams}])(implicit <#list 1..i as j>bin${j}: Format[T${j}]<#if i != j>,</#if></#list>) = new Format[S]{ def writes(s: S) = { val product = unapply(s) JsObject( List( <#list 1..i as j> (tojson(f${j}).asInstanceOf[JsString], tojson(product._${j}))<#if i != j>,</#if> </#list> )) } def reads(js: JsValue) = js match { case JsObject(m) => // m is the Map apply( <#list 1..i as j> fromjson[T${j}](m(JsString(f${j})))<#if i != j>,</#if> </#list> ) case _ => throw new RuntimeException("object expected") } } </#list>
ã©ããããJavaã®FreeMarkerã¨ãããã³ãã¬ã¼ãã¨ã³ã¸ã³ãã½ã¼ã¹ã³ã¼ãã«é©ç¨ãã¦ããªããã»ããµãã¡ãããããã§ãã
FMPP: Text file preprocessor (HTML preprocessor)
ããã¤ãå¼ã³åºãã³ã¼ããsbtã®ããã¸ã§ã¯ããã¡ã¤ã«ã«æ¸ãããããã¦ã¾ãã
// ã project / build / TemplateProject.scala ããæç² ã // declares fmpp as a managed dependency. By declaring it in the private 'fmpp' configuration, it doesn't get published val fmppDep = "net.sourceforge.fmpp" % "fmpp" % "0.9.13" % "fmpp" val fmppConf = config("fmpp") hide def fmppClasspath = configurationClasspath(fmppConf) // creates a task that invokes fmpp def fmppTask(args: => List[String], output: => Path, srcRoot: => Path, sources: PathFinder) = { runTask(Some("fmpp.tools.CommandLine"), fmppClasspath, "-U" :: "all" :: "-S" :: srcRoot.absolutePath :: "-O" :: output.absolutePath :: args ::: sources.getPaths.toList) }
ä»ã®JSONã©ã¤ãã©ãª
sjson以å¤ã«ãTwitterã®ãã¤ã¨ããliftã®ãã¤ã¨ããAkkaã®ãã¤ããã£ãããã¾ãã