Skip to content

Instantly share code, notes, and snippets.

@dacr
Last active May 27, 2023 06:29
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/3af899a4bf71d02a53d03bb68321ac32 to your computer and use it in GitHub Desktop.
Save dacr/3af899a4bf71d02a53d03bb68321ac32 to your computer and use it in GitHub Desktop.
borer scala json API cookbook as unit test cases. / published by https://github.com/dacr/code-examples-manager #b06a42b6-78ce-4cb4-bbc3-ab8679e53eba/a0fb69c931774ee5baff9e077af33b7f176c6d98
// summary : borer scala json API cookbook as unit test cases.
// keywords : scala, scalatest, borer, 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 : b06a42b6-78ce-4cb4-bbc3-ab8679e53eba
// created-on : 2019-09-28T08:35:48Z
// managed-by : https://github.com/dacr/code-examples-manager
// run-with : scala-cli $file
// ---------------------
//> using scala "2.13.10"
//> using dep "org.scalatest::scalatest:3.2.16"
//> using dep "io.bullet::borer-core:1.7.2"
//> using dep "io.bullet::borer-derivation:1.7.2"
//> using objectWrapper
// ---------------------
// info - https://sirthias.github.io/borer/
// It uses scala2 macros !
import org.scalatest._, matchers._
import io.bullet.borer.Json
import io.bullet.borer.derivation.MapBasedCodecs._
case class Person(age:Int, name:String)
implicit val dogCodec = deriveCodec[Person]
class JsonBorerCookBook extends flatspec.AnyFlatSpec with should.Matchers {
override def suiteName = "JsonBorerCookBook"
"genson" should "deserialize array of strings" in {
val decoded = Json.decode("""[1,2,3]""".getBytes).to[Array[Int]].value
decoded shouldBe Array(1,2,3)
}
it should "deserialize scala case class" in {
val decoded = Json.decode("""{"age":42, "name":"john"}""".getBytes).to[Person].value
decoded shouldBe Person(42, "john")
}
it should "deserialize scala map" in {
val decoded = Json.decode("""{"age":42, "weight":4242}""".getBytes).to[Map[String,Int]].value
decoded shouldBe Map("age"->42, "weight"->4242)
}
it should "deserialize scala list" in {
val decoded = Json.decode("""[1,2,3]""".getBytes()).to[List[Int]].value
decoded shouldBe List(1,2,3)
}
/*
it should "deserialize java list?" in {
fail()
// val in = upickle.default.read[java.util.ArrayList[Int]]("""[1,2,3]""")
//in shouldBe List(1,2,3)
}
*/
}
org.scalatest.tools.Runner.main(Array("-oDF", "-s", classOf[JsonBorerCookBook].getName))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment