spray-json: customize field names for case classes

2,024 views
Skip to first unread message

Stevo Slavić

unread,
Jan 5, 2015, 4:22:46 AM1/5/15
to spray...@googlegroups.com
Hello spray community,

I have a case class whose constructor parameter names do not match its expected JSON field names.

Is it possible, instead of extending (Root)JsonFormat, to just provide (a function with) custom property to field name mapping when defining format for a case class?

Kind regards,
Stevo Slavic.

P.S.
In my case expected JSON field was named "type" which is a reserved word in Scala. Initially I used slightly renamed constructor parameter in case class, but I just found out one can escape keywords in Scala with backticks (see http://stackoverflow.com/a/8198810/381140 ). Although I've found this workaround, just after writing the question, I'm still interested if it's possible to provide property to field name customization without writing full (Root)JsonFormat extension. If it is possible, it would be nice to have an example documented. Currently only spray-json documentation seems to be README file.

Age Mooij

unread,
Jan 5, 2015, 7:39:01 AM1/5/15
to spray...@googlegroups.com
Yes, this is already possible. What you can do for any case class is the following:

case class Foo(bar: Long, theType: String)
object Foo {
  implicit val format = jsonFormat(Foo.apply, “bar”, “type”)
}

So instead of calling jsonFormat2(Foo.apply), you call jsonFormat(Foo.apply, …) with the list of target json field names in the same order as in the class.

I just realized it is undocumented so I’ll create an issue for updating the docs.

Hope this helps
Age


--
You received this message because you are subscribed to the Google Groups "spray.io User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email to spray-user+...@googlegroups.com.
Visit this group at http://groups.google.com/group/spray-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/spray-user/CAAUywg9Txzvp%2Bz6y9%2B4P12DgOo4C_u7gXrTpeG4ekrrHpOJbpQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Stevo Slavić

unread,
Jan 5, 2015, 9:08:07 AM1/5/15
to spray...@googlegroups.com
Great, thank you very much!

Nick Sabol

unread,
Jul 28, 2015, 2:12:06 PM7/28/15
to spray.io User List, ssl...@gmail.com
That's a great tip, Age. Thank you. If I could ask a follow-up, is there a good way to apply a blanket strategy, like snake casing, to all field names in an object being serialized? Using the method you described, one could specify the name for each field individually in jsonFormat(...), but with large or deeply nested objects this would be cumbersome. Libraries like Java's Jackson allow for a developer to annotate a class with a Property Naming Strategy, such as the LowerCaseWithUnderscoresStrategy, which changes all field names to snake case automatically from camel case. Is any similar functionality available in Spray? Thanks.

Age Mooij

unread,
Jul 29, 2015, 3:38:22 AM7/29/15
to spray...@googlegroups.com, ssl...@gmail.com
Hi Nick. Unfortunately there is not, although it should not be that hard to add.

A few years back a colleague of mine wrote a custom jsonFormat wrapper that applied snake casing using two regexes. This performed pretty well because they were only applied to the format itself once upon creation so if snake casing in particular is what you need, I can see if I can find that code somewhere.

Hope this helps
Age

Sam Halliday

unread,
Jul 29, 2015, 6:24:49 AM7/29/15
to spray.io User List, ssl...@gmail.com

Age Mooij

unread,
Jul 30, 2015, 3:10:41 AM7/30/15
to spray...@googlegroups.com, ssl...@gmail.com
Nice! Would it be possible to do that at the "default" level too? 

In the example it looks like you have to provide one hint per type, which produces more boilerplate than sjs removes. Field naming transformations are just general functions of String => String so you should be able to specify them at the top level IMHO.

Age

--
You received this message because you are subscribed to the Google Groups "spray.io User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email to spray-user+...@googlegroups.com.
Visit this group at http://groups.google.com/group/spray-user.

Sam Halliday

unread,
Jul 30, 2015, 3:17:06 AM7/30/15
to spray...@googlegroups.com

Yes, you can override the default. I'm on a phone now, so can't link you, but have a look it FamilyFormats.scala to see where the default in defined, just override it. :-)

You received this message because you are subscribed to a topic in the Google Groups "spray.io User List" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/spray-user/H0Hr6_ZOkK0/unsubscribe.
To unsubscribe from this group and all its topics, send an email to spray-user+...@googlegroups.com.

Age Mooij

unread,
Jul 30, 2015, 3:53:00 AM7/30/15
to spray...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages