I know there is an xpath like api to get fields out of a JObject in Json4s
val x = (obj \ "Type").asInstanceOf[JString].values
However it feels a bit cumbersome and I'm not a fan of symbolic like apis. I kind of want something like this:
implicit class JsonExtensions(json: JObject) {
def get[T <: JValue](key: String) : T.Values = {
(json \ key).asInstanceOf[T].values
}
}
and use it something like this
val x = obj.get[String]("type")
However it doesn't compile, T's upper bound is a JValue so Id expect to be able to reference the type member Values which is on all JValues. For reference his is a snipper of JValue:
sealed abstract class JValue extends Diff.Diffable with Product with Serializable {
type Values
def values: Values
...
}
I'm new to scala, how can i make the compiler happy?
Here is the solution if anyone is interested:
You have to specify the expected type but x will the underlying Values of the Json Ast type. So for JString is a string, for JObject its a Map[String,Any]
Thank you insan-e