Skip to content

Commit e112213

Browse files
committed
Fix #97 by adding of missing decoding for ADT class names
1 parent 3f763d0 commit e112213

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

macros/src/main/scala/com/github/plokhotnyuk/jsoniter_scala/macros/JsonCodecMaker.scala

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import scala.collection.immutable.{BitSet, IntMap, LongMap}
1212
import scala.collection.mutable.ArrayBuffer
1313
import scala.collection.{breakOut, mutable}
1414
import scala.language.experimental.macros
15+
import scala.reflect.NameTransformer
1516
import scala.reflect.macros.blackbox
1617

1718
@field
@@ -124,7 +125,7 @@ object JsonCodecMaker {
124125

125126
def info(msg: String): Unit = c.info(c.enclosingPosition, msg, force = true)
126127

127-
def decodedName(s: Symbol): String = s.name.decodedName.toString
128+
def decodedName(s: Symbol): String = decodeName(s.name.toString)
128129

129130
def methodType(m: MethodSymbol): Type = m.returnType.dealias
130131

@@ -382,7 +383,7 @@ object JsonCodecMaker {
382383
cq"${JsonReader.toHashCode(cs, cs.length)} => in.skip()"
383384
}
384385

385-
def discriminatorValue(tpe: Type): String = codecConfig.adtLeafClassNameMapper(tpe.typeSymbol.fullName)
386+
def discriminatorValue(tpe: Type): String = codecConfig.adtLeafClassNameMapper(decodeName(tpe.typeSymbol.fullName))
386387

387388
def getStringified(annotations: Map[String, FieldAnnotations], name: String): Boolean =
388389
annotations.get(name).fold(false)(_.stringified)
@@ -898,6 +899,10 @@ object JsonCodecMaker {
898899
}
899900
}
900901

902+
private[this] def decodeName(s: String): String =
903+
if (s.indexOf('$') >= 0) NameTransformer.decode(s)
904+
else s
905+
901906
private[this] def isEncodingRequired(s: String): Boolean = {
902907
val len = s.length
903908
var i = 0

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,12 @@ class JsonCodecMakerSpec extends WordSpec with Matchers {
163163

164164
case object D extends AdtBase
165165

166+
sealed trait TimeZone
167+
168+
case object `US/Alaska` extends TimeZone
169+
170+
case object `Europe/Paris` extends TimeZone
171+
166172
sealed abstract class База
167173

168174
case class А(б: Б) extends База
@@ -889,6 +895,11 @@ class JsonCodecMakerSpec extends WordSpec with Matchers {
889895
List(А(Б("x")), Б("x")),
890896
"""[{"тип":"А","б":{"с":"x"}},{"тип":"Б","с":"x"}]""".getBytes("UTF-8"))
891897
}
898+
"serialize and deserialize ADTs with Scala operators in names" in {
899+
verifySerDeser(make[List[TimeZone]](CodecMakerConfig(discriminatorFieldName = "zoneId")),
900+
List(`US/Alaska`, `Europe/Paris`),
901+
"""[{"zoneId":"US/Alaska"},{"zoneId":"Europe/Paris"}]""".getBytes("UTF-8"))
902+
}
892903
"throw parse exception in case of missing discriminator field or illegal value of discriminator field" in {
893904
assert(intercept[JsonParseException] {
894905
verifyDeser(codecOfADTList, List(A(1)), """[{"a":1}]""".getBytes("UTF-8"))

0 commit comments

Comments
 (0)