diff options
-rw-r--r-- | build.sbt | 4 | ||||
-rw-r--r-- | project/plugins.sbt | 4 | ||||
-rw-r--r-- | src/main/boilerplate/spray/json/ProductFormatsInstances.scala.template | 43 | ||||
-rw-r--r-- | src/main/scala/spray/json/ProductFormats.scala | 452 | ||||
-rw-r--r-- | src/test/scala/spray/json/ProductFormatsSpec.scala | 36 |
5 files changed, 87 insertions, 452 deletions
@@ -35,6 +35,8 @@ libraryDependencies <++= scalaVersion { sv => scaladocOptions <<= (name, version).map { (n, v) => Seq("-doc-title", n + " " + v) } +// generate boilerplate +Boilerplate.settings /////////////// // publishing @@ -68,4 +70,4 @@ seq(lsSettings:_*) (LsKeys.docsUrl in LsKeys.lsync) := Some(new URL("http://spray.github.com/spray/api/spray-json/")) -(externalResolvers in LsKeys.lsync) := Seq("spray repo" at "http://repo.spray.io")
\ No newline at end of file +(externalResolvers in LsKeys.lsync) := Seq("spray repo" at "http://repo.spray.io") diff --git a/project/plugins.sbt b/project/plugins.sbt index feffd34..0bea931 100644 --- a/project/plugins.sbt +++ b/project/plugins.sbt @@ -1 +1,3 @@ -addSbtPlugin("me.lessis" % "ls-sbt" % "0.1.2")
\ No newline at end of file +addSbtPlugin("me.lessis" % "ls-sbt" % "0.1.2") + +addSbtPlugin("io.spray" % "sbt-boilerplate" % "0.5.0") diff --git a/src/main/boilerplate/spray/json/ProductFormatsInstances.scala.template b/src/main/boilerplate/spray/json/ProductFormatsInstances.scala.template new file mode 100644 index 0000000..fa5cbb2 --- /dev/null +++ b/src/main/boilerplate/spray/json/ProductFormatsInstances.scala.template @@ -0,0 +1,43 @@ +/* + * Copyright (C) 2011,2012 Mathias Doenitz, Johannes Rudolph + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package spray.json + +trait ProductFormatsInstances { self: ProductFormats with StandardFormats => +[# // Case classes with 1 parameters + + def jsonFormat1[[#P1 :JF#], T <: Product :ClassManifest](construct: ([#P1#]) => T): RootJsonFormat[T] = { + val Array([#p1#]) = extractFieldNames(classManifest[T]) + jsonFormat(construct, [#p1#]) + } + def jsonFormat[[#P1 :JF#], T <: Product](construct: ([#P1#]) => T, [#fieldName1: String#]): RootJsonFormat[T] = new RootJsonFormat[T]{ + def write(p: T) = { + val fields = new collection.mutable.ListBuffer[(String, JsValue)] + fields.sizeHint(1 * 2) + [#fields ++= productElement2Field[P1](fieldName1, p, 0)# + ] + JsObject(fields: _*) + } + def read(value: JsValue) = { + [#val p1V = fromField[P1](value, fieldName1)# + ] + construct([#p1V#]) + } + }# + + +] +} diff --git a/src/main/scala/spray/json/ProductFormats.scala b/src/main/scala/spray/json/ProductFormats.scala index 1f5f7cf..d364af0 100644 --- a/src/main/scala/spray/json/ProductFormats.scala +++ b/src/main/scala/spray/json/ProductFormats.scala @@ -22,457 +22,9 @@ import java.lang.reflect.Modifier * Provides the helpers for constructing custom JsonFormat implementations for types implementing the Product trait * (especially case classes) */ -trait ProductFormats { +trait ProductFormats extends ProductFormatsInstances { this: StandardFormats => - def jsonFormat0[T <: Product :ClassManifest](construct: () => T): RootJsonFormat[T] = { - jsonFormat(construct) - } - def jsonFormat[T <: Product](construct: () => T): RootJsonFormat[T] = new RootJsonFormat[T]{ - def write(p: T) = JsObject() - def read(value: JsValue) = construct() - } - - def jsonFormat1[A :JF, T <: Product :ClassManifest](construct: A => T): RootJsonFormat[T] = { - val Array(a) = extractFieldNames(classManifest[T]) - jsonFormat(construct, a) - } - def jsonFormat[A :JF, T <: Product](construct: A => T, a: String): RootJsonFormat[T] = new RootJsonFormat[T]{ - def write(p: T) = JsObject( - productElement2Field[A](a, p, 0) - ) - def read(value: JsValue) = construct( - fromField[A](value, a) - ) - } - - def jsonFormat2[A :JF, B :JF, T <: Product :ClassManifest](construct: (A, B) => T): RootJsonFormat[T] = { - val Array(a, b) = extractFieldNames(classManifest[T]) - jsonFormat(construct, a, b) - } - def jsonFormat[A :JF, B :JF, T <: Product](construct: (A, B) => T, a: String, b: String): RootJsonFormat[T] = new RootJsonFormat[T]{ - def write(p: T) = JsObject( - productElement2Field[A](a, p, 0, - productElement2Field[B](b, p, 1)) - ) - def read(value: JsValue) = construct( - fromField[A](value, a), - fromField[B](value, b) - ) - } - - def jsonFormat3[A :JF, B :JF, C :JF, T <: Product :ClassManifest](construct: (A, B, C) => T): RootJsonFormat[T] = { - val Array(a, b, c) = extractFieldNames(classManifest[T]) - jsonFormat(construct, a, b, c) - } - def jsonFormat[A :JF, B :JF, C :JF, T <: Product](construct: (A, B, C) => T, - a: String, b: String, c: String): RootJsonFormat[T] = new RootJsonFormat[T]{ - def write(p: T) = JsObject( - productElement2Field[A](a, p, 0, - productElement2Field[B](b, p, 1, - productElement2Field[C](c, p, 2))) - ) - def read(value: JsValue) = construct( - fromField[A](value, a), - fromField[B](value, b), - fromField[C](value, c) - ) - } - - def jsonFormat4[A :JF, B :JF, C :JF, D :JF, T <: Product :ClassManifest] - (construct: (A, B, C, D) => T): RootJsonFormat[T] = { - val Array(a, b, c, d) = extractFieldNames(classManifest[T]) - jsonFormat(construct, a, b, c, d) - } - def jsonFormat[A :JF, B :JF, C :JF, D :JF, T <: Product](construct: (A, B, C, D) => T, - a: String, b: String, c: String, d: String): RootJsonFormat[T] = new RootJsonFormat[T]{ - def write(p: T) = JsObject( - productElement2Field[A](a, p, 0, - productElement2Field[B](b, p, 1, - productElement2Field[C](c, p, 2, - productElement2Field[D](d, p, 3)))) - ) - def read(value: JsValue) = construct( - fromField[A](value, a), - fromField[B](value, b), - fromField[C](value, c), - fromField[D](value, d) - ) - } - - def jsonFormat5[A :JF, B :JF, C :JF, D :JF, E :JF, T <: Product :ClassManifest] - (construct: (A, B, C, D, E) => T): RootJsonFormat[T] = { - val Array(a, b, c, d, e) = extractFieldNames(classManifest[T]) - jsonFormat(construct, a, b, c, d, e) - } - def jsonFormat[A :JF, B :JF, C :JF, D :JF, E :JF, T <: Product](construct: (A, B, C, D, E) => T, - a: String, b: String, c: String, d: String, e: String): RootJsonFormat[T] = new RootJsonFormat[T]{ - def write(p: T) = JsObject( - productElement2Field[A](a, p, 0, - productElement2Field[B](b, p, 1, - productElement2Field[C](c, p, 2, - productElement2Field[D](d, p, 3, - productElement2Field[E](e, p, 4))))) - ) - def read(value: JsValue) = construct( - fromField[A](value, a), - fromField[B](value, b), - fromField[C](value, c), - fromField[D](value, d), - fromField[E](value, e) - ) - } - - def jsonFormat6[A :JF, B :JF, C :JF, D :JF, E :JF, F :JF, T <: Product :ClassManifest] - (construct: (A, B, C, D, E, F) => T): RootJsonFormat[T] = { - val Array(a, b, c, d, e, f) = extractFieldNames(classManifest[T]) - jsonFormat(construct, a, b, c, d, e, f) - } - def jsonFormat[A :JF, B :JF, C :JF, D :JF, E :JF, F :JF, T <: Product](construct: (A, B, C, D, E, F) => T, - a: String, b: String, c: String, d: String, e: String, f: String): RootJsonFormat[T] = new RootJsonFormat[T]{ - def write(p: T) = JsObject( - productElement2Field[A](a, p, 0, - productElement2Field[B](b, p, 1, - productElement2Field[C](c, p, 2, - productElement2Field[D](d, p, 3, - productElement2Field[E](e, p, 4, - productElement2Field[F](f, p, 5)))))) - ) - def read(value: JsValue) = construct( - fromField[A](value, a), - fromField[B](value, b), - fromField[C](value, c), - fromField[D](value, d), - fromField[E](value, e), - fromField[F](value, f) - ) - } - - def jsonFormat7[A :JF, B :JF, C :JF, D :JF, E :JF, F :JF, G :JF, T <: Product :ClassManifest] - (construct: (A, B, C, D, E, F, G) => T): RootJsonFormat[T] = { - val Array(a, b, c, d, e, f, g) = extractFieldNames(classManifest[T]) - jsonFormat(construct, a, b, c, d, e, f, g) - } - def jsonFormat[A :JF, B :JF, C :JF, D :JF, E :JF, F :JF, G :JF, T <: Product](construct: (A, B, C, D, E, F, G) => T, - a: String, b: String, c: String, d: String, e: String, f: String, g: String): RootJsonFormat[T] = new RootJsonFormat[T]{ - def write(p: T) = JsObject( - productElement2Field[A](a, p, 0, - productElement2Field[B](b, p, 1, - productElement2Field[C](c, p, 2, - productElement2Field[D](d, p, 3, - productElement2Field[E](e, p, 4, - productElement2Field[F](f, p, 5, - productElement2Field[G](g, p, 6))))))) - ) - def read(value: JsValue) = construct( - fromField[A](value, a), - fromField[B](value, b), - fromField[C](value, c), - fromField[D](value, d), - fromField[E](value, e), - fromField[F](value, f), - fromField[G](value, g) - ) - } - - def jsonFormat8[A :JF, B :JF, C :JF, D :JF, E :JF, F :JF, G :JF, H :JF, T <: Product :ClassManifest] - (construct: (A, B, C, D, E, F, G, H) => T): RootJsonFormat[T] = { - val Array(a, b, c, d, e, f, g, h) = extractFieldNames(classManifest[T]) - jsonFormat(construct, a, b, c, d, e, f, g, h) - } - def jsonFormat[A :JF, B :JF, C :JF, D :JF, E :JF, F :JF, G :JF, H :JF, T <: Product] - (construct: (A, B, C, D, E, F, G, H) => T, - a: String, b: String, c: String, d: String, e: String, f: String, g: String, h: String): RootJsonFormat[T] = new RootJsonFormat[T]{ - def write(p: T) = JsObject( - productElement2Field[A](a, p, 0, - productElement2Field[B](b, p, 1, - productElement2Field[C](c, p, 2, - productElement2Field[D](d, p, 3, - productElement2Field[E](e, p, 4, - productElement2Field[F](f, p, 5, - productElement2Field[G](g, p, 6, - productElement2Field[H](h, p, 7)))))))) - ) - def read(value: JsValue) = construct( - fromField[A](value, a), - fromField[B](value, b), - fromField[C](value, c), - fromField[D](value, d), - fromField[E](value, e), - fromField[F](value, f), - fromField[G](value, g), - fromField[H](value, h) - ) - } - - def jsonFormat9[A :JF, B :JF, C :JF, D :JF, E :JF, F :JF, G :JF, H :JF, I :JF, T <: Product :ClassManifest] - (construct: (A, B, C, D, E, F, G, H, I) => T): RootJsonFormat[T] = { - val Array(a, b, c, d, e, f, g, h, i) = extractFieldNames(classManifest[T]) - jsonFormat(construct, a, b, c, d, e, f, g, h, i) - } - def jsonFormat[A :JF, B :JF, C :JF, D :JF, E :JF, F :JF, G :JF, H :JF, I :JF, T <: Product] - (construct: (A, B, C, D, E, F, G, H, I) => T, a: String, b: String, c: String, d: String, e: String, f: String, - g: String, h: String, i: String): RootJsonFormat[T] = new RootJsonFormat[T]{ - def write(p: T) = JsObject( - productElement2Field[A](a, p, 0, - productElement2Field[B](b, p, 1, - productElement2Field[C](c, p, 2, - productElement2Field[D](d, p, 3, - productElement2Field[E](e, p, 4, - productElement2Field[F](f, p, 5, - productElement2Field[G](g, p, 6, - productElement2Field[H](h, p, 7, - productElement2Field[I](i, p, 8))))))))) - ) - def read(value: JsValue) = construct( - fromField[A](value, a), - fromField[B](value, b), - fromField[C](value, c), - fromField[D](value, d), - fromField[E](value, e), - fromField[F](value, f), - fromField[G](value, g), - fromField[H](value, h), - fromField[I](value, i) - ) - } - - def jsonFormat10[A :JF, B :JF, C :JF, D :JF, E :JF, F :JF, G :JF, H :JF, I :JF, J :JF, T <: Product :ClassManifest] - (construct: (A, B, C, D, E, F, G, H, I, J) => T): RootJsonFormat[T] = { - val Array(a, b, c, d, e, f, g, h, i, j) = extractFieldNames(classManifest[T]) - jsonFormat(construct, a, b, c, d, e, f, g, h, i, j) - } - def jsonFormat[A :JF, B :JF, C :JF, D :JF, E :JF, F :JF, G :JF, H :JF, I :JF, J :JF, T <: Product] - (construct: (A, B, C, D, E, F, G, H, I, J) => T, a: String, b: String, c: String, d: String, e: String, - f: String, g: String, h: String, i: String, j: String): RootJsonFormat[T] = new RootJsonFormat[T]{ - def write(p: T) = JsObject( - productElement2Field[A](a, p, 0, - productElement2Field[B](b, p, 1, - productElement2Field[C](c, p, 2, - productElement2Field[D](d, p, 3, - productElement2Field[E](e, p, 4, - productElement2Field[F](f, p, 5, - productElement2Field[G](g, p, 6, - productElement2Field[H](h, p, 7, - productElement2Field[I](i, p, 8, - productElement2Field[J](j, p, 9)))))))))) - ) - def read(value: JsValue) = construct( - fromField[A](value, a), - fromField[B](value, b), - fromField[C](value, c), - fromField[D](value, d), - fromField[E](value, e), - fromField[F](value, f), - fromField[G](value, g), - fromField[H](value, h), - fromField[I](value, i), - fromField[J](value, j) - ) - } - - def jsonFormat11[A :JF, B :JF, C :JF, D :JF, E :JF, F :JF, G :JF, H :JF, I :JF, J :JF, K :JF, T <: Product :ClassManifest] - (construct: (A, B, C, D, E, F, G, H, I, J, K) => T): RootJsonFormat[T] = { - val Array(a, b, c, d, e, f, g, h, i, j, k) = extractFieldNames(classManifest[T]) - jsonFormat(construct, a, b, c, d, e, f, g, h, i, j, k) - } - def jsonFormat[A :JF, B :JF, C :JF, D :JF, E :JF, F :JF, G :JF, H :JF, I :JF, J :JF, K :JF, T <: Product] - (construct: (A, B, C, D, E, F, G, H, I, J, K) => T, a: String, b: String, c: String, d: String, e: String, - f: String, g: String, h: String, i: String, j: String, k: String): RootJsonFormat[T] = new RootJsonFormat[T]{ - def write(p: T) = JsObject( - productElement2Field[A](a, p, 0, - productElement2Field[B](b, p, 1, - productElement2Field[C](c, p, 2, - productElement2Field[D](d, p, 3, - productElement2Field[E](e, p, 4, - productElement2Field[F](f, p, 5, - productElement2Field[G](g, p, 6, - productElement2Field[H](h, p, 7, - productElement2Field[I](i, p, 8, - productElement2Field[J](j, p, 9, - productElement2Field[K](k, p, 10))))))))))) - ) - def read(value: JsValue) = construct( - fromField[A](value, a), - fromField[B](value, b), - fromField[C](value, c), - fromField[D](value, d), - fromField[E](value, e), - fromField[F](value, f), - fromField[G](value, g), - fromField[H](value, h), - fromField[I](value, i), - fromField[J](value, j), - fromField[K](value, k) - ) - } - - def jsonFormat12[A :JF, B :JF, C :JF, D :JF, E :JF, F :JF, G :JF, H :JF, I :JF, J :JF, K :JF, L: JF, T <: Product :ClassManifest] - (construct: (A, B, C, D, E, F, G, H, I, J, K, L) => T): RootJsonFormat[T] = { - val Array(a, b, c, d, e, f, g, h, i, j, k, l) = extractFieldNames(classManifest[T]) - jsonFormat(construct, a, b, c, d, e, f, g, h, i, j, k, l) - } - def jsonFormat[A :JF, B :JF, C :JF, D :JF, E :JF, F :JF, G :JF, H :JF, I :JF, J :JF, K :JF, L :JF, T <: Product] - (construct: (A, B, C, D, E, F, G, H, I, J, K, L) => T, a: String, b: String, c: String, d: String, e: String, - f: String, g: String, h: String, i: String, j: String, k: String, l: String): RootJsonFormat[T] = new RootJsonFormat[T]{ - def write(p: T) = JsObject( - productElement2Field[A](a, p, 0, - productElement2Field[B](b, p, 1, - productElement2Field[C](c, p, 2, - productElement2Field[D](d, p, 3, - productElement2Field[E](e, p, 4, - productElement2Field[F](f, p, 5, - productElement2Field[G](g, p, 6, - productElement2Field[H](h, p, 7, - productElement2Field[I](i, p, 8, - productElement2Field[J](j, p, 9, - productElement2Field[K](k, p, 10, - productElement2Field[L](l, p, 11)))))))))))) - ) - def read(value: JsValue) = construct( - fromField[A](value, a), - fromField[B](value, b), - fromField[C](value, c), - fromField[D](value, d), - fromField[E](value, e), - fromField[F](value, f), - fromField[G](value, g), - fromField[H](value, h), - fromField[I](value, i), - fromField[J](value, j), - fromField[K](value, k), - fromField[L](value, l) - ) - } - - def jsonFormat13[A :JF, B :JF, C :JF, D :JF, E :JF, F :JF, G :JF, H :JF, I :JF, J :JF, K :JF, L: JF, M :JF, T <: Product :ClassManifest] - (construct: (A, B, C, D, E, F, G, H, I, J, K, L, M) => T): RootJsonFormat[T] = { - val Array(a, b, c, d, e, f, g, h, i, j, k, l, m) = extractFieldNames(classManifest[T]) - jsonFormat(construct, a, b, c, d, e, f, g, h, i, j, k, l, m) - } - def jsonFormat[A :JF, B :JF, C :JF, D :JF, E :JF, F :JF, G :JF, H :JF, I :JF, J :JF, K :JF, L :JF, M :JF, T <: Product] - (construct: (A, B, C, D, E, F, G, H, I, J, K, L, M) => T, a: String, b: String, c: String, d: String, e: String, - f: String, g: String, h: String, i: String, j: String, k: String, l: String, m: String): RootJsonFormat[T] = new RootJsonFormat[T]{ - def write(p: T) = JsObject( - productElement2Field[A](a, p, 0, - productElement2Field[B](b, p, 1, - productElement2Field[C](c, p, 2, - productElement2Field[D](d, p, 3, - productElement2Field[E](e, p, 4, - productElement2Field[F](f, p, 5, - productElement2Field[G](g, p, 6, - productElement2Field[H](h, p, 7, - productElement2Field[I](i, p, 8, - productElement2Field[J](j, p, 9, - productElement2Field[K](k, p, 10, - productElement2Field[L](l, p, 11, - productElement2Field[M](m, p, 12))))))))))))) - ) - def read(value: JsValue) = construct( - fromField[A](value, a), - fromField[B](value, b), - fromField[C](value, c), - fromField[D](value, d), - fromField[E](value, e), - fromField[F](value, f), - fromField[G](value, g), - fromField[H](value, h), - fromField[I](value, i), - fromField[J](value, j), - fromField[K](value, k), - fromField[L](value, l), - fromField[M](value, m) - ) - } - - def jsonFormat14[A :JF, B :JF, C :JF, D :JF, E :JF, F :JF, G :JF, H :JF, I :JF, J :JF, K :JF, L: JF, M :JF, N :JF, T <: Product :ClassManifest] - (construct: (A, B, C, D, E, F, G, H, I, J, K, L, M, N) => T): RootJsonFormat[T] = { - val Array(a, b, c, d, e, f, g, h, i, j, k, l, m, n) = extractFieldNames(classManifest[T]) - jsonFormat(construct, a, b, c, d, e, f, g, h, i, j, k, l, m, n) - } - def jsonFormat[A :JF, B :JF, C :JF, D :JF, E :JF, F :JF, G :JF, H :JF, I :JF, J :JF, K :JF, L :JF, M :JF, N :JF, T <: Product] - (construct: (A, B, C, D, E, F, G, H, I, J, K, L, M, N) => T, a: String, b: String, c: String, d: String, - e: String, f: String, g: String, h: String, i: String, j: String, k: String, l: String, m: String, - n: String): RootJsonFormat[T] = new RootJsonFormat[T]{ - def write(p: T) = JsObject( - productElement2Field[A](a, p, 0, - productElement2Field[B](b, p, 1, - productElement2Field[C](c, p, 2, - productElement2Field[D](d, p, 3, - productElement2Field[E](e, p, 4, - productElement2Field[F](f, p, 5, - productElement2Field[G](g, p, 6, - productElement2Field[H](h, p, 7, - productElement2Field[I](i, p, 8, - productElement2Field[J](j, p, 9, - productElement2Field[K](k, p, 10, - productElement2Field[L](l, p, 11, - productElement2Field[M](m, p, 12, - productElement2Field[N](n, p, 13)))))))))))))) - ) - def read(value: JsValue) = construct( - fromField[A](value, a), - fromField[B](value, b), - fromField[C](value, c), - fromField[D](value, d), - fromField[E](value, e), - fromField[F](value, f), - fromField[G](value, g), - fromField[H](value, h), - fromField[I](value, i), - fromField[J](value, j), - fromField[K](value, k), - fromField[L](value, l), - fromField[M](value, m), - fromField[N](value, n) - ) - } - - def jsonFormat15[A :JF, B :JF, C :JF, D :JF, E :JF, F :JF, G :JF, H :JF, I :JF, J :JF, K :JF, L: JF, M :JF, N :JF, O :JF, T <: Product :ClassManifest] - (construct: (A, B, C, D, E, F, G, H, I, J, K, L, M, N, O) => T): RootJsonFormat[T] = { - val Array(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o) = extractFieldNames(classManifest[T]) - jsonFormat(construct, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o) - } - def jsonFormat[A :JF, B :JF, C :JF, D :JF, E :JF, F :JF, G :JF, H :JF, I :JF, J :JF, K :JF, L :JF, M :JF, N :JF, O :JF, T <: Product] - (construct: (A, B, C, D, E, F, G, H, I, J, K, L, M, N, O) => T, a: String, b: String, c: String, d: String, - e: String, f: String, g: String, h: String, i: String, j: String, k: String, l: String, m: String, n: String, - o: String): RootJsonFormat[T] = new RootJsonFormat[T]{ - def write(p: T) = JsObject( - productElement2Field[A](a, p, 0, - productElement2Field[B](b, p, 1, - productElement2Field[C](c, p, 2, - productElement2Field[D](d, p, 3, - productElement2Field[E](e, p, 4, - productElement2Field[F](f, p, 5, - productElement2Field[G](g, p, 6, - productElement2Field[H](h, p, 7, - productElement2Field[I](i, p, 8, - productElement2Field[J](j, p, 9, - productElement2Field[K](k, p, 10, - productElement2Field[L](l, p, 11, - productElement2Field[M](m, p, 12, - productElement2Field[N](n, p, 13, - productElement2Field[O](o, p, 14))))))))))))))) - ) - def read(value: JsValue) = construct( - fromField[A](value, a), - fromField[B](value, b), - fromField[C](value, c), - fromField[D](value, d), - fromField[E](value, e), - fromField[F](value, f), - fromField[G](value, g), - fromField[H](value, h), - fromField[I](value, i), - fromField[J](value, j), - fromField[K](value, k), - fromField[L](value, l), - fromField[M](value, m), - fromField[N](value, n), - fromField[O](value, o) - ) - } - // helpers protected def productElement2Field[T](fieldName: String, p: Product, ix: Int, rest: List[JsField] = Nil) @@ -484,7 +36,7 @@ trait ProductFormats { } } - private def fromField[T](value: JsValue, fieldName: String)(implicit reader: JsonReader[T]) = { + protected def fromField[T](value: JsValue, fieldName: String)(implicit reader: JsonReader[T]) = { value match { case x: JsObject => var fieldFound = false diff --git a/src/test/scala/spray/json/ProductFormatsSpec.scala b/src/test/scala/spray/json/ProductFormatsSpec.scala index 5a07b4f..af04505 100644 --- a/src/test/scala/spray/json/ProductFormatsSpec.scala +++ b/src/test/scala/spray/json/ProductFormatsSpec.scala @@ -87,6 +87,42 @@ class ProductFormatsSpec extends Specification { json.convertTo[Test3[Int, String]] mustEqual obj } } + "A JsonFormat for a case class with 18 parameters and created with `jsonFormat`" should { + object Test18Protocol extends DefaultJsonProtocol { + implicit val test18Format = jsonFormat18(Test18) + } + case class Test18( + a1: String, + a2: String, + a3: String, + a4: String, + a5: Int, + a6: String, + a7: String, + a8: String, + a9: String, + a10: String, + a11: String, + a12: Double, + a13: String, + a14: String, + a15: String, + a16: String, + a17: String, + a18: String) + + import Test18Protocol._ + val obj = Test18("a1", "a2", "a3", "a4", 5, "a6", "a7", "a8", "a9", + "a10", "a11", 12d, "a13", "a14", "a15", "a16", "a17", "a18") + + val json = JsonParser("""{"a1":"a1","a2":"a2","a3":"a3","a4":"a4","a5":5,"a6":"a6","a7":"a7","a8":"a8","a9":"a9","a10":"a10","a11":"a11","a12":12.0,"a13":"a13","a14":"a14","a15":"a15","a16":"a16","a17":"a17","a18":"a18"}""") + "convert to a respective JsObject" in { + obj.toJson mustEqual json + } + "convert a JsObject to the respective case class instance" in { + json.convertTo[Test18] mustEqual obj + } + } "A JsonFormat for a generic case class with an explicitly provided type parameter" should { "support the jsonFormat1 syntax" in { |