Skip to content

Instantly share code, notes, and snippets.

@dacr
Last active May 27, 2023 06:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dacr/55968c19e4238e1c20d9d478b3019382 to your computer and use it in GitHub Desktop.
Save dacr/55968c19e4238e1c20d9d478b3019382 to your computer and use it in GitHub Desktop.
typelevel jawn scala json API cookbook as unit test cases. / published by https://github.com/dacr/code-examples-manager #95cb15c4-4bb7-4936-a815-0172b65d6c6e/a538cb61cc8e9eb27919edeb39710cb9854b0e90
// summary : typelevel jawn scala json API cookbook as unit test cases.
// keywords : scala, scalatest, jawn, json, @testable
// publish : gist
// authors : David Crosson
// license : Apache NON-AI License Version 2.0 (https://raw.githubusercontent.com/non-ai-licenses/non-ai-licenses/main/NON-AI-APACHE2)
// id : 95cb15c4-4bb7-4936-a815-0172b65d6c6e
// created-on : 2019-09-28T08:35:48Z
// managed-by : https://github.com/dacr/code-examples-manager
// execution : scala ammonite script (http://ammonite.io/) - run as follow 'amm scriptname.sc'
// run-with : scala-cli $file
// ---------------------
//> using scala "3.3.0"
//> using dep "org.scalatest::scalatest:3.2.16"
//> using dep "org.typelevel::jawn-parser:1.3.0"
//> using dep "org.typelevel::jawn-ast:1.3.0"
//> using objectWrapper
// ---------------------
// info https://sirthias.github.io/borer/
import org.scalatest.*, matchers.*
import org.typelevel.jawn.ast.*
import org.typelevel.jawn.util.*
import scala.util.Success
import org.scalatest.EitherValues.*
case class Person(age:Int, name:String)
class JawnCookBook extends flatspec.AnyFlatSpec with should.Matchers {
override def suiteName = "JsonBorerCookBook"
"jawn" should "parse array of strings" in {
val decoded = JParser.parseFromString("""[1,2,3]""").get
decoded shouldBe JArray.fromSeq(List(1,2,3).map(v => JNum(v.toLong)))
}
it should "parse objects" in {
val decoded = JParser.parseFromString("""{"age":42, "name":"john"}""")
decoded.get.get("age") shouldBe JNum(42)
decoded.get.get("name") shouldBe JString("john")
}
}
org.scalatest.tools.Runner.main(Array("-oDF", "-s", classOf[JawnCookBook].getName))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment