diff options
author | Mark van der Tol <github@markvandertol.nl> | 2013-10-13 18:02:49 +0200 |
---|---|---|
committer | Mark van der Tol <github@markvandertol.nl> | 2013-10-13 18:02:49 +0200 |
commit | b1bd64e5ec75665aff9fe8f3915dac23e737498f (patch) | |
tree | 062a7334f11480aadf4d01bdb73840866080708d | |
parent | 4be4d6ff8e466b52fbb349a823549424e2e259eb (diff) | |
download | spray-json-b1bd64e5ec75665aff9fe8f3915dac23e737498f.tar.gz spray-json-b1bd64e5ec75665aff9fe8f3915dac23e737498f.tar.bz2 spray-json-b1bd64e5ec75665aff9fe8f3915dac23e737498f.zip |
Ignore static fields in jsonFormat
-rw-r--r-- | src/main/scala/spray/json/ProductFormats.scala | 4 | ||||
-rw-r--r-- | src/test/scala/spray/json/ProductFormatsSpec.scala | 15 |
2 files changed, 18 insertions, 1 deletions
diff --git a/src/main/scala/spray/json/ProductFormats.scala b/src/main/scala/spray/json/ProductFormats.scala index 8b5843f..ad57221 100644 --- a/src/main/scala/spray/json/ProductFormats.scala +++ b/src/main/scala/spray/json/ProductFormats.scala @@ -61,7 +61,9 @@ trait ProductFormats extends ProductFormatsInstances { // that lexical sorting of ...8(), ...9(), ...10() is not correct, so we extract N and sort by N.toInt val copyDefaultMethods = clazz.getMethods.filter(_.getName.startsWith("copy$default$")).sortBy( _.getName.drop("copy$default$".length).takeWhile(_ != '(').toInt) - val fields = clazz.getDeclaredFields.filterNot(f => f.getName.startsWith("$") || Modifier.isTransient(f.getModifiers)) + val fields = clazz.getDeclaredFields.filterNot { f => + f.getName.startsWith("$") || Modifier.isTransient(f.getModifiers) || Modifier.isStatic(f.getModifiers) + } if (copyDefaultMethods.length != fields.length) sys.error("Case class " + clazz.getName + " declares additional fields") if (fields.zip(copyDefaultMethods).exists { case (f, m) => f.getType != m.getReturnType }) diff --git a/src/test/scala/spray/json/ProductFormatsSpec.scala b/src/test/scala/spray/json/ProductFormatsSpec.scala index c969a6f..8e3390d 100644 --- a/src/test/scala/spray/json/ProductFormatsSpec.scala +++ b/src/test/scala/spray/json/ProductFormatsSpec.scala @@ -25,12 +25,15 @@ class ProductFormatsSpec extends Specification { case class TestTransient(a: Int, b: Option[Double]) { @transient var c = false } + @SerialVersionUID(1L) // SerialVersionUID adds a static field to the case class + case class TestStatic(a: Int, b: Option[Double]) trait TestProtocol { this: DefaultJsonProtocol => implicit val test2Format = jsonFormat2(Test2) implicit def test3Format[A: JsonFormat, B: JsonFormat] = jsonFormat2(Test3.apply[A, B]) implicit def testTransientFormat = jsonFormat2(TestTransient) + implicit def testStaticFormat = jsonFormat2(TestStatic) } object TestProtocol1 extends DefaultJsonProtocol with TestProtocol object TestProtocol2 extends DefaultJsonProtocol with TestProtocol with NullOptions @@ -147,4 +150,16 @@ class ProductFormatsSpec extends Specification { } } + "A JsonFormat for a case class with static fields and created with `jsonFormat`" should { + import TestProtocol1._ + val obj = TestStatic(42, Some(4.2)) + val json = JsObject("a" -> JsNumber(42), "b" -> JsNumber(4.2)) + "convert to a respective JsObject" in { + obj.toJson mustEqual json + } + "convert a JsObject to the respective case class instance" in { + json.convertTo[TestStatic] mustEqual obj + } + } + } |