From 37ad3c66baa2df36594277022dcf68fa8d4fff26 Mon Sep 17 00:00:00 2001 From: Jakob Odersky Date: Thu, 2 Aug 2018 15:42:22 -0700 Subject: Rename default type field to '@type' --- README.md | 2 +- shared/src/main/scala/DerivedFormats.scala | 2 +- shared/src/main/scala/annotations.scala | 8 ++++---- shared/src/test/scala/CoproductTypeFormatTests.scala | 16 ++++++++-------- 4 files changed, 14 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 4148926..6e72b7f 100644 --- a/README.md +++ b/README.md @@ -70,7 +70,7 @@ object Main extends App { println(Seq[X](Z(Y(42), "foo"), Y(2)).toJson.prettyPrint) // [{ - // "type": "Z", + // "@type": "Z", // "y": { // "x": 42 // }, diff --git a/shared/src/main/scala/DerivedFormats.scala b/shared/src/main/scala/DerivedFormats.scala index ed39f46..7c13314 100644 --- a/shared/src/main/scala/DerivedFormats.scala +++ b/shared/src/main/scala/DerivedFormats.scala @@ -68,7 +68,7 @@ trait DerivedFormats { self: BasicFormats => .collectFirst { case g: adt => g.typeFieldName } - .getOrElse("type") + .getOrElse("@type") new JsonFormat[T] { override def write(value: T): JsValue = ctx.dispatch(value) { sub => diff --git a/shared/src/main/scala/annotations.scala b/shared/src/main/scala/annotations.scala index 4437aeb..ad2a37f 100644 --- a/shared/src/main/scala/annotations.scala +++ b/shared/src/main/scala/annotations.scala @@ -7,16 +7,16 @@ import scala.annotation.StaticAnnotation * added to the final JSON objects. * * Note that by default all sealed traits are treated as ADTs, with a type - * field called `type`. This annotation enables overriding the name of that + * field called `@type`. This annotation enables overriding the name of that * field and is really only useful if a child itself has a field called `type` * that would otherwise result in a conflict. * * Example: * {{{ * // the JSON field "kind" will contain the actual type of the serialized child - * @adt("kind") sealed abstract class Keyword(`type`: String) - * case class If(`type`: String) extends Keyword(`type`) + * @adt("kind") sealed abstract class Keyword(`@type`: String) + * case class If(`@type`: String) extends Keyword(`@type`) * }}} * @param typeFieldName the name of the field to inject into a serialized JSON * object */ -final class adt(val typeFieldName: String = "type") extends StaticAnnotation +final class adt(val typeFieldName: String = "@type") extends StaticAnnotation diff --git a/shared/src/test/scala/CoproductTypeFormatTests.scala b/shared/src/test/scala/CoproductTypeFormatTests.scala index 7da2041..5e88856 100644 --- a/shared/src/test/scala/CoproductTypeFormatTests.scala +++ b/shared/src/test/scala/CoproductTypeFormatTests.scala @@ -18,33 +18,33 @@ class CoproductTypeFormatTests "No-parameter case class child" should behave like checkRoundtrip[Expr]( Zero(), - """{"type":"Zero"}""" + """{"@type":"Zero"}""" ) "Simple parameter case class child" should behave like checkRoundtrip[Expr]( Value(42), - """{"type":"Value","x":42}""" + """{"@type":"Value","x":42}""" ) "Nested parameter case class child" should behave like checkRoundtrip[Expr]( Plus(Value(42), One), - """{"type":"Plus","lhs":{"type":"Value","x":42},"rhs":{"type":"One"}}""" + """{"@type":"Plus","lhs":{"@type":"Value","x":42},"rhs":{"@type":"One"}}""" ) "Case object child" should behave like checkRoundtrip[Expr]( One, - """{"type": "One"}""" + """{"@type": "One"}""" ) @adt("kind") - sealed abstract class Keyword(`type`: String) - case class If(`type`: String) extends Keyword(`type`) + sealed abstract class Keyword(`@type`: String) + case class If(`@type`: String) extends Keyword(`@type`) implicit val keywordFormat: RootJsonFormat[Keyword] = jsonFormat[Keyword] "ADT with type field alias" should behave like checkRoundtrip[Keyword]( If("class"), - """{"kind":"If","type":"class"}""" + """{"kind":"If","@type":"class"}""" ) @adt("""_`crazy type!`"""") @@ -68,7 +68,7 @@ class CoproductTypeFormatTests "Enum" should behave like checkRoundtrip[List[Enum]]( A :: B :: Nil, - """[{"type":"A"}, {"type":"B"}]""" + """[{"@type":"A"}, {"@type":"B"}]""" ) "Serializing as sealed trait and deserializing as child" should "work" in { -- cgit v1.2.3