diff options
-rw-r--r-- | src/main/scala/spray/json/ProductFormats.scala | 4 | ||||
-rw-r--r-- | src/test/scala/spray/json/ProductFormatsSpec.scala | 16 |
2 files changed, 19 insertions, 1 deletions
diff --git a/src/main/scala/spray/json/ProductFormats.scala b/src/main/scala/spray/json/ProductFormats.scala index 5504fbd..fecc7de 100644 --- a/src/main/scala/spray/json/ProductFormats.scala +++ b/src/main/scala/spray/json/ProductFormats.scala @@ -16,6 +16,8 @@ package spray.json +import java.lang.reflect.Modifier + /** * Provides the helpers for constructing custom JsonFormat implementations for types implementing the Product trait * (especially case classes) @@ -499,7 +501,7 @@ trait ProductFormats { // 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(_.getName.startsWith("$")) + val fields = clazz.getDeclaredFields.filterNot(f => f.getName.startsWith("$") || Modifier.isTransient(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 ec9d65f..5a07b4f 100644 --- a/src/test/scala/spray/json/ProductFormatsSpec.scala +++ b/src/test/scala/spray/json/ProductFormatsSpec.scala @@ -22,11 +22,15 @@ class ProductFormatsSpec extends Specification { case class Test2(a: Int, b: Option[Double]) case class Test3[A, B](as: List[A], bs: List[B]) + case class TestTransient(a: Int, b: Option[Double]) { + @transient var c = false + } 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) } object TestProtocol1 extends DefaultJsonProtocol with TestProtocol object TestProtocol2 extends DefaultJsonProtocol with TestProtocol with NullOptions @@ -95,4 +99,16 @@ class ProductFormatsSpec extends Specification { } } + "A JsonFormat for a case class with transient fields and created with `jsonFormat`" should { + import TestProtocol1._ + val obj = TestTransient(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[TestTransient] mustEqual obj + } + } + } |