Closed alexandru-calinoiu closed 6 years ago
Hi @alexandru-calinoiu,
Ty for contacting us with abut this issue.
Unfortunately I was unable to get the error while decoding your input.
This is the Japx
output for your input:
{
"data":{
"id":"1",
"links":{
"self":"example.com\/member\/1\/schedule"
},
"type":"schedule",
"requests":[
{
"status":"requested",
"id":"5",
"attribute1":"",
"type":"pickup_request",
"attribute2":""
},
{
"status":"requested",
"id":"25",
"attribute3":"",
"type":"swap_request"
}
]
}
}
I've also created a sample macOS app that decodes your json in app delegate: JapxTest.zip
I hope I was able to help. If you have any additional questions feel free to ask :)
Thank for the quick respones @Truba
I got this far, but the problem I have is when I try to decode this with codable:
I have my base Request struct
struc Request : JapxCodable {
let id: UInt
}
and a couple of specialized structs
struct PickupRequest: Request {
let attribute1: String
}
struct SwapRequest: Request {
let attribute3: String
}
I am not able to figure out a way to create this objects from the above json.
Hi @alexandru-calinoiu, ty for more details about your problem.
From what you have said it seems that you are having issues with Swift's Codable
and class inheritance, not with this library. For this reason I'm closing this issue. ;)
Feel free to reopen it if you feel that I got something wrong. :)
Please note that JapxCodable
is just Codable
with a friendly reminder for a coder to write type: String
and id: String
in his/hers implementation. :)
With that said, a few words that might help you with this issue:
I said class inheritance, because I'm assuming your Request
, PickupRequest
and SwapRequest
are class, since there is no struct inheritance in Swift :)
Here you can find a nice article on how to manage class inheritance with Swift's Codable
:
https://medium.com/tsengineering/swift-4-0-codable-decoding-subclasses-inherited-classes-heterogeneous-arrays-ee3e180eb556
Thanks a lot, indeed I meant classes :+1:
Expected Behavior
I am trying to deserialize a response like this:
As you notice the included items have different types.
Actual Behavior
An error is thrown
Steps to Reproduce the Problem
Specifications