Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JsonConfiguration(SnakeCase) analogue? #47

Open
aaabramov opened this issue Feb 9, 2018 · 1 comment
Open

JsonConfiguration(SnakeCase) analogue? #47

aaabramov opened this issue Feb 9, 2018 · 1 comment

Comments

@aaabramov
Copy link

Is there any way to automatically convert camel case fields to underscored?

For example:

case case Person(firstName: String, lastName: String)

object Person {
  // will be used by format to map field names
  implicit val jsonConfig = JsonConfiguration(SnakeCase)
  implicit val format = Jsonx.formatCaseClass[Person]
}

Should produce:

{
  "first_name": "...",
  "last_name": "..."
}

I found some code in ai.x.play.json.SingletonEncoder but seems that it is used only in ai.x.play.json.Jsonx#formatSingleton

@mtsokol
Copy link
Contributor

mtsokol commented Jul 9, 2019

Hi @aaabramov!

I just tried to solve it here: #74

Now to have names in snake case you can:

case class Person(firstName: String, lastName: String)

implicit val encoder: NameEncoder = CamelToSnakeNameEncoder()
implicit val format = Jsonx.formatCaseClass[Person]

val person = Person("...", "...")

println(Json.toJson(person))

{ "first_name": "...", "last_name": "..." }

Due to the fact that macros does not support default/named arguments, for standard camel format implicit is also required. This solves the import:

import ai.x.play.json.Encoders._

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants