diff options
author | Johannes Rudolph <johannes.rudolph@gmail.com> | 2013-05-29 12:22:48 +0200 |
---|---|---|
committer | Johannes Rudolph <johannes.rudolph@gmail.com> | 2013-05-29 12:22:48 +0200 |
commit | 94ab29ee4bf5fb7d9ba83a533dd8c02082bbd3e6 (patch) | |
tree | e64f426f1b244ca178a2047d1cd40664c127cdba /src/main/scala/spray | |
parent | f12d95eda2783340c22441faf1dc86eb8e0fb3b2 (diff) | |
download | spray-json-94ab29ee4bf5fb7d9ba83a533dd8c02082bbd3e6.tar.gz spray-json-94ab29ee4bf5fb7d9ba83a533dd8c02082bbd3e6.tar.bz2 spray-json-94ab29ee4bf5fb7d9ba83a533dd8c02082bbd3e6.zip |
use sbt-boilerplate for generating stubs
(cherry picked from commit b4b2208d045fd7ee1549f56c2b9d897c5caceb48)
This allows to have jsonFormat for more than 15 case class parameters. Fixes #48
Conflicts:
build.sbt
project/plugins.sbt
src/main/boilerplate/cc/spray/json/ProductFormatsInstances.scala.template
Diffstat (limited to 'src/main/scala/spray')
-rw-r--r-- | src/main/scala/spray/json/ProductFormats.scala | 452 |
1 files changed, 2 insertions, 450 deletions
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 |