Skip to content
This repository was archived by the owner on Aug 14, 2023. It is now read-only.

Commit cf66f37

Browse files
author
Pulasthi Bandara
committed
Revert "make json parsing more robust"
This reverts commit c9443fe.
1 parent 1c356da commit cf66f37

File tree

2 files changed

+10
-67
lines changed

2 files changed

+10
-67
lines changed

src/main/scala/com/github/vitalsoftware/macros/JsonFormatAnnotation.scala

Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -48,53 +48,31 @@ class jsonMacro(useDefaults: Boolean) {
4848
}
4949
}
5050

51-
/**
52-
* Generates implicit json.Formats that fallback to the defaults is the initial parsing fails.
53-
*/
5451
def jsonFormatter(className: TypeName, fields: List[ValDef]) = {
5552
fields.length match {
5653
case 0 => c.abort(c.enclosingPosition, "Cannot create json formatter for case class with no fields")
5754
case _ =>
58-
val lowPriorityFormats = if (useDefaults) {
59-
q"private val lowPriorityFormats = play.api.libs.json.Json.using[play.api.libs.json.Json.WithDefaultValues].format[$className]"
55+
if (useDefaults) {
56+
q"implicit val jsonAnnotationFormat = play.api.libs.json.Json.using[play.api.libs.json.Json.WithDefaultValues].format[$className]"
6057
} else {
61-
q"private val lowPriorityFormats = play.api.libs.json.Json.format[$className]"
58+
q"implicit val jsonAnnotationFormat = play.api.libs.json.Json.format[$className]"
6259
}
63-
64-
Seq(
65-
lowPriorityFormats,
66-
q"""
67-
private val robustReads = new play.api.libs.json.Reads[$className] {
68-
override def reads(json: play.api.libs.json.JsValue) = lowPriorityFormats.reads(json) match {
69-
case s: play.api.libs.json.JsSuccess[_] => s
70-
case e: play.api.libs.json.JsError =>
71-
val transforms = e.errors.map(_._1)
72-
.filter(_.path.size == 1)
73-
.map(_.json.prune)
74-
.reduce(_ andThen _)
75-
76-
json.transform(transforms).flatMap(lowPriorityFormats.reads _).orElse(e)
77-
}
78-
}
79-
""",
80-
q"implicit val jsonAnnotationFormat = play.api.libs.json.Format(robustReads, lowPriorityFormats)"
81-
)
8260
}
8361
}
8462

85-
def modifiedCompanion(compDeclOpt: Option[ModuleDef], format: Seq[Tree], className: TypeName) = {
63+
def modifiedCompanion(compDeclOpt: Option[ModuleDef], format: ValDef, className: TypeName) = {
8664
compDeclOpt map { compDecl =>
8765
// Add the formatter to the existing companion object
8866
val q"object $obj extends ..$bases { ..$body }" = compDecl
8967
q"""
9068
object $obj extends ..$bases {
9169
..$body
92-
..$format
70+
$format
9371
}
9472
"""
9573
} getOrElse {
9674
// Create a companion object with the formatter
97-
q"object ${className.toTermName} { ..$format }"
75+
q"object ${className.toTermName} { $format }"
9876
}
9977
}
10078

src/test/scala/com/github/vitalsoftware/macros/JsonFormatAnnotationTest.scala

Lines changed: 4 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,20 @@ package com.github.vitalsoftware.macros
33
import org.specs2.mutable.Specification
44
import play.api.libs.json._
55

6-
@json case class Person(name: String, age: Int, gender: Option[String])
7-
@jsonDefaults case class Person2(name: String, age: Int = 7, gender: Option[String] = None)
6+
@json case class Person(name: String, age: Int)
7+
@jsonDefaults case class Person2(name: String, age: Int = 7)
88

99
class JsonFormatAnnotationTest extends Specification {
1010

1111
"@json annotation" should {
1212

1313
"create correct formatter for case class with >= 2 fields" in {
1414

15-
val person = Person("Victor Hugo", 46, Some("Male"))
15+
val person = Person("Victor Hugo", 46)
1616
val json = Json.toJson(person)
1717
json === Json.obj(
1818
"name" -> "Victor Hugo",
19-
"age" -> 46,
20-
"gender" -> "Male"
19+
"age" -> 46
2120
)
2221
Json.fromJson[Person](json).asOpt must beSome(person)
2322
}
@@ -36,38 +35,4 @@ class JsonFormatAnnotationTest extends Specification {
3635
Json.fromJson[Person2](Json.obj("name" -> "Victor Hugo")).asOpt must beSome(person)
3736
}
3837
}
39-
40-
"robustParsing" should {
41-
"make invalid option values None" in {
42-
val json = Json.obj(
43-
"name" -> "Victor Hugo",
44-
"age" -> 46,
45-
"gender" -> true
46-
)
47-
48-
Json.fromJson[Person](json).asOpt must beSome(Person("Victor Hugo", 46, None))
49-
Json.fromJson[Person2](json).asOpt must beSome(Person2("Victor Hugo", 46))
50-
}
51-
52-
"make invalid values with defaults fallback to the default" in {
53-
val json = Json.obj(
54-
"name" -> "Victor Hugo",
55-
"age" -> "non age"
56-
)
57-
58-
Json.fromJson[Person2](json).asOpt must beSome(Person2("Victor Hugo", 7))
59-
}
60-
61-
"throw on invalid values which are not optional or default" in {
62-
val json = Json.obj(
63-
"name" -> "Victor Hugo",
64-
"age" -> "non age"
65-
)
66-
67-
val result = Json.fromJson[Person](json)
68-
result must beAnInstanceOf[JsError]
69-
result.asInstanceOf[JsError].errors.head._1.path.head.asInstanceOf[KeyPathNode].key mustEqual "age"
70-
71-
}
72-
}
7338
}

0 commit comments

Comments
 (0)