Scala convert tuple to JSON using Jerkson

Code snippet in REPL

scala> import com.codahale.jerkson.Json._
scala> val t = (1, 3.14, "Fred")
scala> generate(t)
res5: String = {"_1":1,"_2":3.14,"_3":"Fred"}

On the way out, I want to assign a label attribute instead of _1, _2, _3. How should I do it?

+3
source share
1 answer

Use case classinstead of a tuple:

case class Named(myInt: Int, thisDouble: Double, desc: String)
generate(Named(1, 3.14, "Fred"))

gives:

{"myInt": 1.0,"thisDouble":3.14,"desc":"Fred"}
+3
source

All Articles