@@ -477,7 +477,7 @@ class JsonCodecMakerSpec extends WordSpec with Matchers {
477
477
verifySerDeser(codecOfEnums, Enums (LocationType .GPS ), """ {"lt":1}""" )
478
478
verifyDeserError(codecOfEnums, Enums (LocationType .GPS ), """ {"lt":"GPS"}""" , " illegal number, offset: 0x00000006" )
479
479
}
480
- " serialize and deserialize outer types using custom key codecs for map keys" in {
480
+ " serialize and deserialize types using a custom key codec and a custom ordering for map keys" in {
481
481
implicit val codecOfLevel : JsonKeyCodec [Level ] = new JsonKeyCodec [Level ] {
482
482
override def decodeKey (in : JsonReader ): Level = in.readKeyAsInt() match {
483
483
case 0 => Level .LOW
@@ -491,7 +491,34 @@ class JsonCodecMakerSpec extends WordSpec with Matchers {
491
491
case _ => out.encodeError(" illegal enum value" )
492
492
}
493
493
}
494
- verifySerDeser(make[Map [Level , Int ]](CodecMakerConfig ()), Map (Level .HIGH -> 0 ), """ {"1":0}""" )
494
+ verifySerDeser(make[Map [Level , Int ]](CodecMakerConfig ()), Map (Level .HIGH -> 100 ), """ {"1":100}""" )
495
+ implicit val levelOrdering : Ordering [Level ] = new Ordering [Level ] {
496
+ override def compare (x : Level , y : Level ): Int = y.ordinal - x.ordinal
497
+ }
498
+ verifySerDeser(make[collection.immutable.TreeMap [Level , Int ]](CodecMakerConfig ()),
499
+ collection.immutable.TreeMap [Level , Int ](Level .HIGH -> 100 , Level .LOW -> 10 ), """ {"0":10,"1":100}""" )
500
+ }
501
+ " serialize and deserialize types using a custom value codec and a custom ordering for set values" in {
502
+ implicit val codecOfLevel : JsonValueCodec [Level ] = new JsonValueCodec [Level ] {
503
+ override def decodeValue (in : JsonReader , default : Level ): Level = in.readInt() match {
504
+ case 0 => Level .LOW
505
+ case 1 => Level .HIGH
506
+ case x => in.enumValueError(x.toString)
507
+ }
508
+
509
+ override def encodeValue (x : Level , out : JsonWriter ): Unit = x match {
510
+ case Level .LOW => out.writeVal(0 )
511
+ case Level .HIGH => out.writeVal(1 )
512
+ case _ => out.encodeError(" illegal enum value" )
513
+ }
514
+
515
+ override def nullValue : Level = null .asInstanceOf [Level ]
516
+ }
517
+ implicit val levelOrdering : Ordering [Level ] = new Ordering [Level ] {
518
+ override def compare (x : Level , y : Level ): Int = y.ordinal - x.ordinal
519
+ }
520
+ verifySerDeser(make[collection.immutable.TreeSet [Level ]](CodecMakerConfig ()),
521
+ collection.immutable.TreeSet [Level ](Level .HIGH , Level .LOW ), """ [0,1]""" )
495
522
}
496
523
" serialize and deserialize case classes with value classes" in {
497
524
case class ValueClassTypes (uid : UserId , oid : OrderId )
0 commit comments