Adrian,
the thing to remember when working with type classes (like the spray-json `JsonFormat`) is that
all the resolution of what type class instance to supply for which method call is done by the
*compiler* at *compile time*!
In your case a user is receiving a set of `Device` instances, whose exact types are only known at *runtime*.
Therefore you need to provide a custom `JsonFormat[Device]` and write the logic for how to determine *at runtime* whether an incoming piece of JSON contains a `PhoneDevice` or a `SIPDevice`. The compiler cannot do this for you (after all there might be even more subclasses of `Device` written at a later time).
Note that, if you model the user like this:
case class User(id: Option[UUID] = None, devices: Set[Either[PhoneDevice, SIPDevice] = Set.empty)
things would be different. This way you make it explicit which exact types are allowed in the set, thereby enabling the compiler to make use of the `eitherFormat` that is already available with spray-json. When reading JSON this `JsonFormat` will simply try deserializing into both types and succeed only if exactly one of the two alternatives worked out.
HTH and cheers,
Mathias
---
mat...@spray.io
http://spray.io
> --
> You received this message because you are subscribed to the Google Groups "spray-user" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to
spray-user+...@googlegroups.com.
> For more options, visit
https://groups.google.com/groups/opt_out.
>
>