Skip to content

Commit

Permalink
Add tests with workarounds for #1130
Browse files Browse the repository at this point in the history
  • Loading branch information
plokhotnyuk committed Apr 15, 2024
1 parent 457d40d commit 86d563b
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.plokhotnyuk.jsoniter_scala.macros

import com.github.plokhotnyuk.jsoniter_scala.core._
import com.github.plokhotnyuk.jsoniter_scala.macros.JsonCodecMaker._
import org.scalatest.exceptions.TestFailedException

class JsonCodecMakerNewKeywordSpec extends VerifyingSpec {
Expand All @@ -22,8 +23,8 @@ class JsonCodecMakerNewKeywordSpec extends VerifyingSpec {

case class RootPathFiles(files: List[String])

given codecOfDeResult1: JsonValueCodec[DeResult[Option[String]]] = JsonCodecMaker.make
given codecOfDeResult2: JsonValueCodec[DeResult[RootPathFiles]] = JsonCodecMaker.make
given codecOfDeResult1: JsonValueCodec[DeResult[Option[String]]] = make
given codecOfDeResult2: JsonValueCodec[DeResult[RootPathFiles]] = make
verifySerDeser(summon[JsonValueCodec[DeResult[RootPathFiles]]],
DeResult[RootPathFiles](true, RootPathFiles(List("VVV")), "WWW"),
"""{"isSucceed":true,"data":{"files":["VVV"]},"message":"WWW"}""")
Expand All @@ -35,12 +36,11 @@ class JsonCodecMakerNewKeywordSpec extends VerifyingSpec {
case class GenDoc[A, B, C](a: A, opt: Option[B], list: List[C])

object GenDoc:
given [A : JsonValueCodec, B : JsonValueCodec, C : JsonValueCodec]: JsonValueCodec[GenDoc[A, B, C]] =
JsonCodecMaker.make
given [A : JsonValueCodec, B : JsonValueCodec, C : JsonValueCodec]: JsonValueCodec[GenDoc[A, B, C]] = make

given aCodec: JsonValueCodec[Boolean] = JsonCodecMaker.make
given bCodec: JsonValueCodec[String] = JsonCodecMaker.make
given cCodec: JsonValueCodec[Int] = JsonCodecMaker.make
given aCodec: JsonValueCodec[Boolean] = make
given bCodec: JsonValueCodec[String] = make
given cCodec: JsonValueCodec[Int] = make
verifySerDeser(summon[JsonValueCodec[GenDoc[Boolean, String, Int]]],
GenDoc(true, Some("VVV"), List(1, 2, 3)), """{"a":true,"opt":"VVV","list":[1,2,3]}""")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,37 @@ class JsonCodecMakerSpec extends VerifyingSpec {
"serialize and deserialize Java enumerations as key in maps" in {
verifySerDeser(make[Map[Level, Int]], Map(Level.HIGH -> 0), """{"HIGH":0}""")
}
"serialize and deserialize option types using a custom codec to handle missing fields and 'null' values differently" in {
case class Ex(opt: Option[String] = _root_.scala.Some("hiya"), level: Option[Int] = _root_.scala.Some(10))

object CustomOptionCodecs {
implicit val intCodec: JsonValueCodec[Int] = make[Int]
implicit val stringCodec: JsonValueCodec[String] = make[String]

implicit def optionCodec[A](implicit aCodec: JsonValueCodec[A]): JsonValueCodec[Option[A]] =
new JsonValueCodec[Option[A]] {
override def decodeValue(in: JsonReader, default: Option[A]): Option[A] =
if (in.isNextToken('n')) in.readNullOrError(_root_.scala.None, "expected 'null' or JSON value")
else {
in.rollbackToken()
_root_.scala.Some(aCodec.decodeValue(in, aCodec.nullValue))
}

override def encodeValue(x: Option[A], out: JsonWriter): _root_.scala.Unit =
if (x eq _root_.scala.None) out.writeNull()
else aCodec.encodeValue(x.get, out)

override def nullValue: Option[A] = _root_.scala.None
}
}

import CustomOptionCodecs._

val codecOfEx = make[Ex](CodecMakerConfig.withTransientNone(false))
verifySerDeser(codecOfEx, Ex(_root_.scala.None, _root_.scala.None), """{"opt":null,"level":null}""")
verifySerDeser(codecOfEx, Ex(_root_.scala.Some("hiya"), _root_.scala.Some(10)), """{}""")
verifySerDeser(codecOfEx, Ex(_root_.scala.Some("pakikisama"), _root_.scala.Some(5)), """{"opt":"pakikisama","level":5}""")
}
"serialize and deserialize types using a custom key codec and a custom ordering for map keys" in {
implicit val codecOfLevel: JsonKeyCodec[Level] = new JsonKeyCodec[Level] {
override def decodeKey(in: JsonReader): Level = in.readKeyAsInt() match {
Expand Down

0 comments on commit 86d563b

Please sign in to comment.