Scala Json with capabilities for Scala Object Serialization
Ruminations of a Programmer
I am quite frustrated with sbt and pk11/steps
(Why are these things never work out of the box for me?)
I am just trying to run "jetty-run", but i got so many dependency errors, it's not fun anymore.
I am stuck with unresolved dependencies for sjson 0.3
Does anyone know which mvn repo can I get sjson 0.3 from?
Source: (StackOverflow)
I was taking a look at the source code of sjson when I discovered this weird piece of code:
<#list 2..22 as i>
<#assign typeParams><#list 1..i as j>T${j}<#if i !=j>,</#if></#list></#assign>
def asProduct${i}[S, ${typeParams}](<#list 1..i as j>f${j}: String<#if i != j>,</#if></#list>)(apply : (${typeParams}) => S)(unapply : S => Product${i}[${typeParams}])(implicit <#list 1..i as j>bin${j}: Format[T${j}]<#if i != j>,</#if></#list>) = new Format[S]{
def writes(s: S) = {
val product = unapply(s)
JsObject(
List(
<#list 1..i as j>
(tojson(f${j}).asInstanceOf[JsString], tojson(product._${j}))<#if i != j>,</#if>
</#list>
))
}
def reads(js: JsValue) = js match {
case JsObject(m) => // m is the Map
apply(
<#list 1..i as j>
fromjson[T${j}](m(JsString(f${j})))<#if i != j>,</#if>
</#list>
)
case _ => throw new RuntimeException("object expected")
}
}
</#list>
At first sight, it looks like a macro but I'm not sure because the ones I have seen are different and they modify Scala AST using classes like WeakTypeTag and so on. I know that this method is generating asProduct methods with 20 different parameters list. However, when I copy this piece of code in my REPL it's not parsed correctly and I get the following error:
<console>:1: error: identifier expected but double literal found.
And a pile of more errors.
I would like to know:
- Why and how this method works
- More resources to learn to master this
- Why in other libraries they don't use it and they prefer to copy/paste the methods manually (is there actually a reason for this or maybe they don't know its existence?)
Source: (StackOverflow)