Last active
May 25, 2024 10:18
-
-
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/3ad735ee3c480d4bf5d965e9121a0951ec28ae6f
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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.4.2" | |
//> 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