Circe Support - Change class name key to Json field 'discriminator'
tperrigo opened this issue · 3 comments
I'm using akka-http-json's Circe support for a simple Akka-Http GET endpoint:
val route = get {
pathPrefix("report" / "discovery" / "listreports") {
pathEndOrSingleSlash {
implicit val configuration: Configuration =
Configuration.default.withDiscriminator("type")
complete(getReportList())
}
}
}
I'm trying to override the configuration (io.circe.generic.extras.Configuration
) using the implicit listed above to transform the generated Json from a list of elements keyed by class name to a list where the class name is a 'discriminator' field. This works when I simply take a list of Report
objects and explicitly transform them to Json with Circe (eg, reports.asJson.noSpaces
), but when I retrieve the list through the route (using the Akka-Http testkit), I still get the 'standard' Circe encoding, where each element in the result list is keyed by class name).
Is there a way to use akka-http-json's Circe support, yet override the Configuration
so that rather than using class names as keys, they are returned as fields in the resulting list? Ie, I'd like to have the results in the form:
[
{
"id" : "1",
...
"url" : "somereport/1",
"format" : "Csv",
"type" : "Report"
},
...
]
Instead of:
["Report" : {
"id" : "1"
...
]
Basically, I'd just like to return a json array of elements, rather than a list keyed by the class name. As mentioned, the implicit configuration
above produces the output as desired when explicitly converted to Json, but (even with the implicit in scope), does not seem to affect the format of the results returned through the endpoint using akka-http-json's Circe support. If there is any way I can easily accomplish this, please let me know. Any suggestions would be appreciated!
I currently neither have an idea nor spare cycles to investigate this feature, but a PR would be appreciated.
Actually, the error was on my end-- akka-http-json does pick up the implicit configuration...
and returns the Json response in the form I desired. For others who may run into this issue-- my problem was that I didn't have the same configuration conversion within my Akka-Http testkit spec. Once it was put into both places, everything seems to be working. Thank you for this great library @hseeberger, and I apologize for taking up your time.
Mi worries 😉