Skip to content

Commit bb640da

Browse files
committed
Discovered an ability to customize sorted maps/sets by implicit Ordering[K] instances
Thank you, @jwgmeligmeyling for your questions in FasterXML/jackson-databind#2162
1 parent 2b8736e commit bb640da

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ Support of Scala.js and Scala Native is not a goal for the moment.
6060
constructor
6161
- Types that supported as map keys are primitives, boxed primitives, enums, `String`, `BigInt`, `BigDecimal`,
6262
`java.util.UUID`, `java.time.*`, and value classes for any of them
63+
- Codecs for sorted maps and sets can be customized by implicit `Ordering[K]` instances for keys that are available at
64+
the scope of the `make` macro call
6365
- Parsing of escaped characters are not supported for strings which are mapped to numeric and data/time types
6466
- Support of first-order and higher-kinded types
6567
- Support of 2 representations of ADTs with a sealed trait or a Scala class as base type and non-abstract Scala classes

jsoniter-scala-macros/src/test/scala/com/github/plokhotnyuk/jsoniter_scala/macros/JsonCodecMakerSpec.scala

+29-2
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,7 @@ class JsonCodecMakerSpec extends WordSpec with Matchers {
477477
verifySerDeser(codecOfEnums, Enums(LocationType.GPS), """{"lt":1}""")
478478
verifyDeserError(codecOfEnums, Enums(LocationType.GPS), """{"lt":"GPS"}""", "illegal number, offset: 0x00000006")
479479
}
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 {
481481
implicit val codecOfLevel: JsonKeyCodec[Level] = new JsonKeyCodec[Level] {
482482
override def decodeKey(in: JsonReader): Level = in.readKeyAsInt() match {
483483
case 0 => Level.LOW
@@ -491,7 +491,34 @@ class JsonCodecMakerSpec extends WordSpec with Matchers {
491491
case _ => out.encodeError("illegal enum value")
492492
}
493493
}
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]""")
495522
}
496523
"serialize and deserialize case classes with value classes" in {
497524
case class ValueClassTypes(uid: UserId, oid: OrderId)

0 commit comments

Comments
 (0)