A client library I'm using returns a java.util.Map[String, Object] and I'd like to be able to serialize this into JSON and have my endpoint return a JSON HTTP response.
I've tried passing it directly to play.api.libs.json.Json.toJson but I get this compiler error:
No Json serializer found for type java.util.Map[String,Object]. Try to implement an implicit Writes or Format for this type.
com.google.gson.Gson.toJson seems to handle it just fine, but that method returns a String and I'd prefer my endpoint to return actual JSON and a plain text String.
Here's the best I've got so far:
def endpoint = Action {
val myMap: java.util.Map[String, Object] = service.getResponse
val gsonString: String = new Gson().toJson(myMap)
Ok(Json.parse(gsonString))
}
But this feels like a bit of a hack since I'm essentially serializing (with Gson), deserializing (with Play's Json), and then serializing again.
Should I just stick with what I have or is there a better way to do this?