Avoid creating nearly identical case classes

faces this situation often

slick generated classes case

case class Person(firstname:String,lastname:String)
case class Address(zip:String,city:String,state:String)

      

usually the front contract JSON has Person

along with Address

. traditionally requiring you to create a class like

case class PersonWithAddress(firstname:String,lastname:String,address:Address)

      

This requirement is common to all boards.

Its a bit cumbersome to create and maintain such placeholder classes. Any nifty trick can help create a data structure on the fly that can be JSON friendly. Is there a use case for Shapeless here? (note: JSON transformations happen using playframework JSON lib)

+3


source to share


1 answer


You can define the JSON case class as case class PersonWithAddress(person: Person, address:Address)

, but then you would need to write your own serializer if you want to flatten the first and last name (instead of leaving them nested inside the helper document).



This is probably more code than just repeating the case class, but at least you would do a compile-time check if the main class classes needed to change (which you don't have in copy / paste classes).

0


source







All Articles