Skip to content
Browse files

practical jackson

  • Loading branch information...
0 parents commit cd370d5498d6d777c532f722991272019b7c9c25 @softprops committed Jan 21, 2012
Showing with 106 additions and 0 deletions.
  1. +1 −0 .gitignore
  2. +7 −0 app/build.sbt
  3. +18 −0 app/src/main/scala/app.scala
  4. +9 −0 pj/build.sbt
  5. +53 −0 pj/src/main/scala/pj.scala
  6. +6 −0 project/build.scala
  7. +1 −0 project/plugins.sbt
  8. +11 −0 src/main/conscript/pj/launchconfig
1 .gitignore
@@ -0,0 +1 @@
+target/
7 app/build.sbt
@@ -0,0 +1,7 @@
+organization := "me.lessis"
+
+name := "app"
+
+version := "0.1.0-SNAPSHOT"
+
+seq(conscript.Harness.conscriptSettings: _*)
18 app/src/main/scala/app.scala
@@ -0,0 +1,18 @@
+package pj
+
+object App {
+ def main(args: Array[String]) {
+ val exit = run(args)
+ System.exit(exit)
+ }
+ def run(args: Array[String]): Int = {
+ println("parse it!")
+ 0
+ }
+}
+
+class App extends xsbti.AppMain {
+ def run(config: xsbti.AppConfiguration) =
+ new Exit(App.run(config.arguments))
+}
+class Exit(val code: Int) extends xsbti.Exit
9 pj/build.sbt
@@ -0,0 +1,9 @@
+organization := "me.lessis"
+
+name := "pj"
+
+version := "0.1.0-SNAPSHOT"
+
+scalacOptions += "-deprecation"
+
+libraryDependencies += "org.codehaus.jackson" % "jackson-core-asl" % "1.9.0"
53 pj/src/main/scala/pj.scala
@@ -0,0 +1,53 @@
+package pj
+
+object PJ {
+ import java.io.{ InputStream => IN, OutputStream => OUT,
+ Reader, Writer, StringWriter }
+ import org.codehaus.jackson.{
+ JsonFactory, JsonGenerator, JsonParser,
+ JsonParseException }
+ import org.codehaus.jackson.util.DefaultPrettyPrinter
+
+ def apply(in: String): Either[String, String] =
+ factory { f =>
+ val writer = new StringWriter
+ pretty(f.createJsonParser(in), f.createJsonGenerator(writer)) {
+ writer.toString
+ }
+ }
+
+ def apply(in: Reader, out: Writer): Either[String, Unit] =
+ factory { f =>
+ pretty(f.createJsonParser(in), f.createJsonGenerator(out)) {
+ ()
+ }
+ }
+
+ def apply(in: IN, out: OUT): Either[String, Unit] =
+ factory { f =>
+ pretty(f.createJsonParser(in), f.createJsonGenerator(out)) {
+ ()
+ }
+ }
+
+ private def pretty[T](
+ parser: JsonParser, gen: JsonGenerator)(
+ f: => T): Either[String, T] =
+ try {
+ parser.nextToken
+ gen.setPrettyPrinter(new DefaultPrettyPrinter)
+ gen.copyCurrentStructure(parser)
+ gen.flush
+ Right(f)
+ } catch {
+ case jpe: JsonParseException =>
+ Left(jpe.getMessage)
+ } finally {
+ gen.close
+ parser.close
+ }
+
+ private def factory[T](f: JsonFactory => T): T =
+ f(new JsonFactory)
+
+}
6 project/build.scala
@@ -0,0 +1,6 @@
+object Build extends sbt.Build {
+ import sbt._
+ lazy val root = Project("root", file(".")) aggregate(pj, app)
+ lazy val pj = Project("pj", file("pj"))
+ lazy val app = Project("app", file("app"))
+}
1 project/plugins.sbt
@@ -0,0 +1 @@
+addSbtPlugin("net.databinder" % "conscript-plugin" % "0.3.3")
11 src/main/conscript/pj/launchconfig
@@ -0,0 +1,11 @@
+[app]
+ version: 0.1.0-SNAPSHOT
+ org: me.lessis
+ name: pj-app
+ class: pj.App
+[scala]
+ version: 2.9.1
+[repositories]
+ local
+ scala-tools-releases
+ maven-central

0 comments on commit cd370d5

Please sign in to comment.
Something went wrong with that request. Please try again.