I want to parse a Json Object. I have defined the following class:
case class Metadata(
val messageTime: Option[String],
val messageUUID: Option[String],
val messageSource: Option[String],
val messageFormat: Option[String])
I generate a dummy object of this type:
jsonMsg = """{"messageTime": "2018-09-19T11:03:22.382+04:00",
"messageUUID": "4ef7fce7-81cf-4a27-82d8-9019c2cf09df",
"messageSource" : "SDG",
"messageFormat" : "json"}"""
Now I want to use mapper.readValue
to parse that string into an Object of type Metadata:
fromJson(jsonMsg)
def fromJson(json : String) = {
mapper.readValue[Metadata](json, classOf[Metadata])
}
This works fine. But in this case fromJson can only parse Metadata objects.
Now I tried to make my method fromJson more generic, passing the class of the object to be parsed like this [I used this other question as a reference ]:
val obj = fromJson2(jsonMsg, classOf[Metadata])
def fromJson2(json : String, c: Class[_]) = {
mapper.readValue(json, c)
}
But I get the error:
forward reference extends over definition of value obj
What am I missing or how could I achieve what I am trying?
Just in case
mapper.readValue
belongs to import com.fasterxml.jackson.databind.ObjectMapper
And this is the definition:
public <T> T readValue(String content, Class<T> valueType)
throws IOException, JsonParseException, JsonMappingException
{
return (T) _readMapAndClose(_jsonFactory.createParser(content), _typeFactory.constructType(valueType));
}