-
Notifications
You must be signed in to change notification settings - Fork 34
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Subtypes Format definitions #29
Comments
Hi, can you add a sealed trait Point[+T <: Location] { val location: T; val calendar: Option[Calendar] } Tell me if it changes something. |
Hi, it does not change anything saddly :(. I think that the problem is not the type parameter but the subtypes of case class BookApi(
...
route: AddressLocation,
url: Option[String],
...
) I have the following error: [error] BookApi.scala:51: No implicit format for domain.AddressLocation available.
[error] implicit def format: Format[BookApi] = Json.format[BookApi]
[error] ^
|
I’ll have a deeper look at this issue later, I’ll keep you updated! |
Ok thanks ! I managed to make it compiles but the solution is ugly: object Location {
implicit val format: OFormat[Location] =
derived.flat.oformat(
(__ \ "type").format[String].inmap(
tpe => s"${tpe.capitalize}Location",
tpe => tpe.dropRight("Location".length).toLowerCase
)
)
}
object CityLocation {
implicit def format: OFormat[CityLocation] = Location.format.asInstanceOf[OFormat[CityLocation]]
}
object AddressLocation {
implicit def format: OFormat[AddressLocation] = Location.format.asInstanceOf[OFormat[AddressLocation]]
}
object Point {
implicit def format[T <: Location: Format]: OFormat[Point[T]] =
derived.flat.oformat(
(__ \ "type").format[String].inmap(
tpe => s"${tpe.capitalize}Point",
tpe => tpe.dropRight("Point".length).toLowerCase
)
)
}
object ShippingPoint {
implicit def format[T <: Location: Format]: OFormat[ShippingPoint[T]] = Point.format[T].asInstanceOf[OFormat[ShippingPoint[T]]]
}
object PickupPoint {
implicit def format[T <: Location: Format]: OFormat[PickupPoint[T]] = Point.format[T].asInstanceOf[OFormat[PickupPoint[T]]]
} After some tests, it works except that if I give the wrong play.api.http.HttpErrorHandlerExceptions$$anon$1: Execution exception[[ClassCastException: domain.CityLocation cannot be cast to domain.AddressLocation]] Same result with the wrong |
Hi, Indeed you cannot safely cast an But you were on the right path: to derive an object AddressLocation {
implicit val format: OFormat[AddressLocation] = derived.oformat
} And then: object BookApi {
implicit val format: OFormat[BookApi] = derived.oformat
} |
Indeed it works but it does not valid that the for example, this will be ok while it should not: {
"type": "city", // should be "address"
"society": "Facebook",
"first_name": "Mark",
...
} Same problem with {
"route": {
"type": "pickup", // should be "shipping"
"location" : {
"type": "address",
"society": "Facebook",
"first_name": "Mark",
...
}
}
} Can I add this constraint ? |
Indeed, but that’s what the type of case class BookApi(route: AddressLocation, …) So, since you know you expect an |
@guizmaii Can I close this issue? |
Yes I'll do. Thanks for your time ! |
Sorry to bother you but I don't understand how to use your lib.
This is what I have to serialize/deserialize:
and some example of corresponding JSONs:
Also, I have an API that is represented by the following case class:
As you showed me, I implemented the
Location
and thePoint
formats as follow:My problem is: the
BookApi
implicitFormat
does not compile:Should I implement a specific
Format
for each of the subtypes ofPoint
andLocation
?If yes, how could I do that ? Can I reuse the
Point
andLocation
definitions ?The text was updated successfully, but these errors were encountered: