play-json 㧠snake_case 㪠json ã camelCase 㪠case class ã«ãããã³ã°ãã
play-json ã使ãã° json ã case class ã«ãããã³ã°ãããã¨ã¯ç°¡åã«ã§ãã¾ãããjson ã®ãã¼ããã®ã¾ã¾ case class ã®ãã£ã¼ã«ãã«å¯¾å¿ãããããsnake_case 㪠json API ã case class ã«ãããã³ã°ããããã«ã¯ãcase class ã®ãã£ã¼ã«ãã snake_case ã«ããå¿ è¦ãããã¾ããScala 㯠camelCase ã«ããã®ã主æµã§ãããå°ãæ°æã¡æªãã§ããã
snake_case 㪠json ã camelCase 㪠case class ã«ãããã³ã°ããããã«ã¯ããã¤ã使ã£ã¦ãã Json.format[T]
ã®ã©ããã¼ãä½ãå¿
è¦ãããã¾ããä½ãã¾ããã
tototoshi/play-json-naming · GitHub
build.sbt ã«
libraryDependencies += "com.github.tototoshi" %% "play-json-naming" % "0.1.0"
ãã¦ä½¿ã£ã¦ãã ããã
使ãæ¹ã¯ãJsonNaming.snakecase
ãå¼ã³åºãã ãã®åæ©è½ã©ã¤ãã©ãªã§ãã
import com.github.tototoshi.play.json.JsonNaming case class Name(firstName: String, lastName: String) case class User(id: Int, nameData: Name) // Json.format[T] ãã©ãããã implicit val nameFormat = JsonNaming.snakecase(Json.format[Name]) implicit val userFormat = JsonNaming.snakecase(Json.format[User]) val jsonString = """{"id":1,"name_data":{"first_name":"Toshiyuki","last_name":"Takahashi"}}""" Json.parse(jsonString).validate[User] Json.toJson(User(1, Name("Toshiyuki", "Takahashi")))
ã¤ã¡ã¼ã¸ã¨ãã¦ã¯
+---------------+ | json string | +---------------+ â +---------------+ | JsValue | +---------------+ â JsonNaming.snakecase +---------------+ | JsValue | +---------------+ â +---------------+ | case class | +---------------+
ãããªãããã§ãjson ã®å é¨è¡¨ç¾ã§ãã JsValue ã«å¯¾ãã¦å¤æãããã¦ãã¾ãã
snake_case 以å¤ã«ãããããè¦åãããã°åãè¾¼ã¿ã¾ãã®ã§ pull-req ãã ããã