Last active
May 25, 2024 10:19
-
-
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/c32036c055824d8d384b0b54e22b783015e2764a
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 : 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.14" | |
//> 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