Skip to content

Instantly share code, notes, and snippets.

@chuwy
Last active August 29, 2015 14:21
Show Gist options
  • Save chuwy/5cddcdc089dcd1e3f0ca to your computer and use it in GitHub Desktop.
Save chuwy/5cddcdc089dcd1e3f0ca to your computer and use it in GitHub Desktop.
import java.io.{File, PrintWriter}
import org.json4s.jackson.JsonMethods._
def printToFile(f: File)(op: PrintWriter => Unit) {
val p = new java.io.PrintWriter(f)
try { op(p) } finally { p.close() }
}
object jsonGen extends JsonGen
val innerShape = Map(
"inner_text" -> jsonGen.arbitraryJString
)
def generateShape = {
val innerJson = jsonGen.generateJsonWithKeys(innerShape)
Map(
"id" -> jsonGen.arbitraryJBool,
"created_at" -> jsonGen.arbitraryJIsoDate,
"is_new" -> jsonGen.arbitraryJBool,
"in" -> innerJson
)
}
for (i <- 1 to 5) {
val json = jsonGen.generateJsonWithKeys(generateShape)
val fileNameTemple = s"int_test_json_${i}.json"
json.sample match {
case Some(newJson) => printToFile(new File(fileNameTemple)) { f =>
val rendered = pretty(render(newJson))
f.print(rendered)
}
case _ =>
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment