Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading snake_case attributes as camelCase in Play Json

I want to extract json as a case class within Play application. The attributes in case class are defined in camelCase and json data comes in snake_case.

case class User(userId: Long, userName: String)

and json would be like this {"user_name":"Vishal","user_id":67}

Is there an easy way to instruct play json to automatically do the mapping and extract, like providing some annotations etc.

like image 297
Vishal John Avatar asked Aug 13 '13 06:08

Vishal John


1 Answers

This is a quite old question, but I didn't find any answer for it, so I went to the Play JSON Github repository and found this:

implicit val config = JsonConfiguration(SnakeCase)

implicit val userReads: Reads[PlayUser] = Json.reads[PlayUser]

So, now seems that exists an official way of doing this

https://github.com/playframework/playframework/blob/d96d42e4baa2261d0e0a9c36518f6921e247e402/documentation/manual/working/scalaGuide/main/json/code/ScalaJsonAutomatedSpec.scala#L128

like image 84
Glauber Campinho Avatar answered Oct 05 '22 20:10

Glauber Campinho