I need to parse the following json string:
{"type": 1}
The case class I am using looks like:
case class MyJsonObj(
val type: Int
)
However, this confuses Scala since 'type' is a keyword. So, I tried using @JsonProperty annotation from Jacson/Jerkson as follows:
case class MyJsonObj(
@JsonProperty("type") val myType: Int
)
However, the Json parser still refuses to look for 'type' string in json instead of 'myType'. Following sample code illustrates the problem:
import com.codahale.jerkson.Json._
import org.codehaus.jackson.annotate._
case class MyJsonObj(
@JsonProperty("type") val myType: Int
)
object SimpleExample {
def main(args: Array[String]) {
val jsonLine = """{"type":1}"""
val JsonObj = parse[MyJsonObj](jsonLine)
}
I get the following error:
[error] (run-main-a) com.codahale.jerkson.ParsingException: Invalid JSON. Needed [myType], but found [type].
P.S: As seen above, I am using jerkson/jackson, but wouldn't mind switching to some other json parsing library if that makes life easier.