I'm implementing some rest API that use spray and akka The API should expose some kind of user CRUD. I'll use only create user in this question...
case class User(id:String, name:String)
case class Register(user:User, registrationId:String)
trait DefaultJsonFormats extends DefaultJsonProtocol with SprayJsonSupport with MetaMarshallers {}
class RegistrationService(registration: ActorRef)
(implicit executionContext: ExecutionContext)
extends Directives with DefaultJsonFormats {
implicit val timeout = Timeout(2.seconds)
implicit val userFormat = jsonFormat3(User)
implicit val registerFormat = jsonFormat1(Register)
implicit val registeredFormat = jsonFormat1(Registered)
val route =
path("register") {
post { handleWith { ru: Register => (registration ? ru).mapTo[Registered] } }
}
Now Lets suppose that User class has 30 fields but there is no jsonFormat30(...) How can I use such implicits for any case class object?
If you are referring to the methods in the
ProductFormatsInstances
trait, there are versions ofjsonFormat
up to 22 parameters. If you have a case class with more than 22 parameters, I see two immediate options. Suppose you haveOptions 1: reduce the number of parameters by breaking down the
Client
class into finer-grained classes taking fewer parameters.. For example, you can refactor to the following.Options 2: write a custom serialiser by implementing
RootJsonFormat
. See here for an example.