Last active
June 27, 2017 14:19
-
-
Save EdgeCaseBerg/e2b366644802eec1263ddbf130f9df4f to your computer and use it in GitHub Desktop.
Over 22 fields in play-json case class. How to deal.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import play.api.libs.json._ | |
import play.api.libs.functional.syntax._ | |
case class AToM( | |
a: Int, | |
b: Int, | |
c: Int, | |
d: Int, | |
e: Int, | |
f: Int, | |
g: Int, | |
h: Int, | |
i: Int, | |
j: Int, | |
k: Int, | |
l: Int, | |
m: Int | |
) | |
case class NToZ( | |
n: Int, | |
o: Int, | |
p: Int, | |
q: Int, | |
r: Int, | |
s: Int, | |
t: Int, | |
u: Int, | |
v: Int, | |
w: Int, | |
x: Int, | |
y: Int, | |
z: Int | |
) | |
case class Large( | |
a: Int, | |
b: Int, | |
c: Int, | |
d: Int, | |
e: Int, | |
f: Int, | |
g: Int, | |
h: Int, | |
i: Int, | |
j: Int, | |
k: Int, | |
l: Int, | |
m: Int, | |
n: Int, | |
o: Int, | |
p: Int, | |
q: Int, | |
r: Int, | |
s: Int, | |
t: Int, | |
u: Int, | |
v: Int, | |
w: Int, | |
x: Int, | |
y: Int, | |
z: Int | |
) | |
implicit AToMFormat = Json.format[AToM] | |
implicit NToZFormat = Json.format[NToZ] | |
implicit val largeReader: Reads[Large] = { | |
(AToMFormat.reads and NToZReads.reads)( (aToM, nToZ) => | |
Large( | |
aToM.a, | |
aToM.b, | |
aToM.c, | |
aToM.d, | |
aToM.e, | |
aToM.f, | |
aToM.g, | |
aToM.h, | |
aToM.i, | |
aToM.j, | |
aToM.k, | |
aToM.l, | |
aToM.m, | |
nToZ.n, | |
nToZ.o, | |
nToZ.p, | |
nToZ.q, | |
nToZ.r, | |
nToZ.s, | |
nToZ.t, | |
nToZ.u, | |
nToZ.v, | |
nToZ.w, | |
nToZ.x, | |
nToZ.y, | |
nToZ.z | |
) | |
) | |
} | |
val s = """{"a": 0,"b": 1,"c": 2,"d": 3,"e": 4,"f": 5,"g": 6,"h": 7,"i": 8,"j": 9,"k": 10,"l": 11,"m": 12,"n": 13,"o": 14,"p": 15,"q": 16,"r": 17,"s": 18,"t": 19,"u": 20,"v": 21,"w": 22,"x": 23,"y": 24,"z": 25}""" | |
scala> Json.parse(s).as[Large] | |
//Large(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import play.api.libs.json._ | |
import play.api.libs.functional.syntax._ | |
case class Large( | |
a: Int, | |
b: Int, | |
c: Int, | |
d: Int, | |
e: Int, | |
f: Int, | |
g: Int, | |
h: Int, | |
i: Int, | |
j: Int, | |
k: Int, | |
l: Int, | |
m: Int, | |
n: Int, | |
o: Int, | |
p: Int, | |
q: Int, | |
r: Int, | |
s: Int, | |
t: Int, | |
u: Int, | |
v: Int, | |
w: Int, | |
x: Int, | |
y: Int, | |
z: Int | |
) | |
implicit val largeReader: Reads[Large] = { | |
val aToMReads: Reads[Tuple13[Int, Int, Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int]] = { | |
(JsPath \ "a").read[Int] and | |
(JsPath \ "b").read[Int] and | |
(JsPath \ "c").read[Int] and | |
(JsPath \ "d").read[Int] and | |
(JsPath \ "e").read[Int] and | |
(JsPath \ "f").read[Int] and | |
(JsPath \ "g").read[Int] and | |
(JsPath \ "h").read[Int] and | |
(JsPath \ "i").read[Int] and | |
(JsPath \ "j").read[Int] and | |
(JsPath \ "k").read[Int] and | |
(JsPath \ "l").read[Int] and | |
(JsPath \ "m").read[Int] | |
}.apply(Tuple13.apply[Int, Int, Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int] _) | |
val nToZReads: Reads[Tuple13[Int, Int, Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int]] = { | |
(JsPath \ "n").read[Int] and | |
(JsPath \ "o").read[Int] and | |
(JsPath \ "p").read[Int] and | |
(JsPath \ "q").read[Int] and | |
(JsPath \ "r").read[Int] and | |
(JsPath \ "s").read[Int] and | |
(JsPath \ "t").read[Int] and | |
(JsPath \ "u").read[Int] and | |
(JsPath \ "v").read[Int] and | |
(JsPath \ "w").read[Int] and | |
(JsPath \ "x").read[Int] and | |
(JsPath \ "y").read[Int] and | |
(JsPath \ "z").read[Int] | |
}.apply(Tuple13.apply[Int, Int, Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int] _) | |
(aToMReads and nToZReads)( (aToM, nToZ) => | |
Large( | |
aToM._1, | |
aToM._2, | |
aToM._3, | |
aToM._4, | |
aToM._5, | |
aToM._6, | |
aToM._7, | |
aToM._8, | |
aToM._9, | |
aToM._10, | |
aToM._11, | |
aToM._12, | |
aToM._13, | |
nToZ._1, | |
nToZ._2, | |
nToZ._3, | |
nToZ._4, | |
nToZ._5, | |
nToZ._6, | |
nToZ._7, | |
nToZ._8, | |
nToZ._9, | |
nToZ._10, | |
nToZ._11, | |
nToZ._12, | |
nToZ._13 | |
) | |
) | |
} | |
val s = """{"a": 0,"b": 1,"c": 2,"d": 3,"e": 4,"f": 5,"g": 6,"h": 7,"i": 8,"j": 9,"k": 10,"l": 11,"m": 12,"n": 13,"o": 14,"p": 15,"q": 16,"r": 17,"s": 18,"t": 19,"u": 20,"v": 21,"w": 22,"x": 23,"y": 24,"z": 25}""" | |
scala> Json.parse(s).as[Large] | |
//Large(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment