Last active
September 5, 2024 15:41
-
-
Save dacr/e20e174fc8ca19401e37b9d257e54d13 to your computer and use it in GitHub Desktop.
upickle scala json API cookbook as unit test cases. / published by https://github.com/dacr/code-examples-manager #27899ec3-598b-4734-a4ec-98cd27b29276/944d1e1bc7a14e02ba2b2b3ce108768cd0815839
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 : upickle scala json API cookbook as unit test cases. | |
// keywords : scala, scalatest, upickle, 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 : 27899ec3-598b-4734-a4ec-98cd27b29276 | |
// created-on : 2019-09-27T21:04:36Z | |
// managed-by : https://github.com/dacr/code-examples-manager | |
// run-with : scala-cli $file | |
// --------------------- | |
//> using scala "3.4.2" | |
//> using dep "org.scalatest::scalatest:3.2.16" | |
//> using dep "com.lihaoyi::upickle:1.4.3" | |
//> using objectWrapper | |
// --------------------- | |
object OptionPickler extends upickle.AttributeTagged { | |
override implicit def OptionWriter[T: Writer]: Writer[Option[T]] = | |
implicitly[Writer[T]].comap[Option[T]] { | |
case None => null.asInstanceOf[T] | |
case Some(x) => x | |
} | |
override implicit def OptionReader[T: Reader]: Reader[Option[T]] = { | |
new Reader.Delegate[Any, Option[T]](implicitly[Reader[T]].map(Some(_))) { | |
override def visitNull(index: Int) = None | |
} | |
} | |
} | |
import upickle.default.ReadWriter | |
import org.scalatest.*, matchers.* | |
case class Person(age: Int, name: String) | |
case class Person2(age: Int, name: String) derives ReadWriter | |
case class Person3(age: Int, name: String, gender: Option[String]) derives ReadWriter | |
case class Person4(age: Int, name: String, gender: Option[String]) derives OptionPickler.ReadWriter | |
class JsonUpickleCookBook extends flatspec.AnyFlatSpec with should.Matchers { | |
override def suiteName = "JsonUpickleCookBook" | |
"upickle" should "deserialize json array strings" in { | |
val decoded = upickle.default.read[Array[Int]]("""[1,2,3]""") | |
decoded shouldBe Array(1, 2, 3) | |
} | |
it should "deserialize scala case classes ?" in { | |
val decoded = upickle.default.read[Person2]("""{"age":42, "name":"john"}""") | |
decoded shouldBe Person2(42, "john") | |
} | |
it should "deserialize scala case classes with optional field? default require a subarray !!" in { | |
val decoded = upickle.default.read[Person3]("""{"age":42, "name":"john", "gender":["male"]}""") | |
decoded shouldBe Person3(42, "john", Some("male")) | |
} | |
it should "deserialize scala case classes with optional field? " in { | |
val decoded = OptionPickler.read[Person4]("""{"age":42, "name":"john", "gender":"male"}""") | |
decoded shouldBe Person4(42, "john", Some("male")) | |
} | |
it should "deserialize scala list" in { | |
val decoded = upickle.default.read[List[Int]]("""[1,2,3]""") | |
decoded shouldBe List(1, 2, 3) | |
} | |
it should "provide an AST" in { | |
import ujson.* | |
val json = Obj( | |
"age" -> Num(42), | |
"name" -> Str("john"), | |
"gender" -> Arr("truc", "bidule") | |
) | |
upickle.default.write(json) shouldBe """{"age":42,"name":"john","gender":["truc","bidule"]}""" | |
} | |
// it should "be able to jsonify map data structure" in { | |
// val jwtId = java.util.UUID.randomUUID().toString | |
// val nowEpochSeconds = java.time.Instant.now.getEpochSecond() | |
// | |
// val result = | |
// s"""{ | |
// | "jti" : "$jwtId", | |
// | "iss" : "this-app", | |
// | "iat" : $nowEpochSeconds, | |
// | "exp" : ${nowEpochSeconds + 60L}, | |
// | "nbf" : ${nowEpochSeconds + 2L}, | |
// | "sub" : "[email protected]" | |
// | "user" : 1 | |
// |}""".stripMargin | |
// | |
// val claim = Map( | |
// "jti" -> jwtId, // JTW ID | |
// "iss" -> "this-app", // Issuer | |
// "iat" -> nowEpochSeconds, // Issued at | |
// "exp" -> (nowEpochSeconds + 60L), // Expiration time | |
// "nbf" -> (nowEpochSeconds + 2L), // Not before | |
// "sub" -> "[email protected]", // The subject | |
// "user" -> 1 | |
// ) | |
// upickle.default.write(claim) | |
// } | |
} | |
org.scalatest.tools.Runner.main(Array("-oDF", "-s", classOf[JsonUpickleCookBook].getName)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment