The macro for scala Json is great!I do as follows:case class TestUser(id: String, loginUrl: String)implicit val testUserReads = Json.reads[TestUser]jsValue.as[TestUser]however,{ id: 34242, login_url: "fsdafsa" } will not work, because the names "login_url" and "loginUrl" do not match.Is it possible to tell the json macro to convert field names to their camelcase version?
If not, is it possible to manually override the field name mapping?
--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to play-framewor...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
@pascal, maybe we could add a parameter to the macro, indicating which naming translation scheme use (e.g. camel_case to lowerCamelCase, or identity — which would be the default)
But if this feature becomes an identified requirement, let's think about the best way to implement it (without annotations if possible ;)) because it's not just modifying the macro IMHO.
> Actually what you ask is a runtime behavior and not a compile-time behavior.
not at all, I am asking for a configuration used at compile-time behavior.the configuration could allow to override some fields of the mappings, such as (login_url <> "loginUrl).a camelCase to under_score_convention automatic mapping is also possible at compile-time,meaning that if it finds login_url, it will not try to find a val in the case class named "login_url", but "loginUrl".(and without using annotations)I agree with Haoyi Li, the library needs to be parametrizable (as in Jackson) so that the library is actually useful for most situations.and the macro substitution can be done at compile-time, not penalized at run-time
On Friday, May 17, 2013 10:33:12 AM UTC+2, David Portabella wrote:The macro for scala Json is great!I do as follows:case class TestUser(id: String, loginUrl: String)implicit val testUserReads = Json.reads[TestUser]jsValue.as[TestUser]however,{ id: 34242, login_url: "fsdafsa" } will not work, because the names "login_url" and "loginUrl" do not match.Is it possible to tell the json macro to convert field names to their camelcase version?
If not, is it possible to manually override the field name mapping?
On Fri, May 17, 2013 at 2:12 PM, David Portabella <david.po...@gmail.com> wrote:
> Actually what you ask is a runtime behavior and not a compile-time behavior.
not at all, I am asking for a configuration used at compile-time behavior.the configuration could allow to override some fields of the mappings, such as (login_url <> "loginUrl).a camelCase to under_score_convention automatic mapping is also possible at compile-time,meaning that if it finds login_url, it will not try to find a val in the case class named "login_url", but "loginUrl".(and without using annotations)I agree with Haoyi Li, the library needs to be parametrizable (as in Jackson) so that the library is actually useful for most situations.and the macro substitution can be done at compile-time, not penalized at run-timeYes I've understood your point of view now... Stupid I am ;)
From this point of view, yes it's possible I think!
I guess the most common are:camelCasePascalCaseunder_scoreddash-separatedallsmallALLCAPSThis isn't just a thought experiment; working with Python/Javascript/HTML/CSS/SQL application, you end up with a mix of *every single one of those* conventions in the same code base! It's horrible!I think ideally, instead of hard-coding in which ones we want to support, the user would be able to pass in a (String) => String function, maybe as an implicit parameter, that will do the transformation for him? That would be really elegant and generic. it should then be pretty easy to provide the common ones (i.e. those above) since writing the (String) => String transforms isn't that hard really.