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
[SR-5431] JSON does not Encode/Decode subclass properties #4278
Comments
I think SR-4772 tracks this fine. You can still override the encoding and decoding members yourself, so it's not a blocking issue. |
Comment by John E. Miller (JIRA) It remains a problem unless I am overriding incorrectly. Indeed, now I get an EXC_BAD_INSTRUCTION on encode. Decode just fails to decode the subclass property as before. |
cc @itaiferber |
SR-4772 does track this correctly. There are issues with the class case that I'm fixing now — overriding also has issues. JE Miller (JIRA User) Can you please provide the failing code? |
Comment by John E. Miller (JIRA) I added new class and subclass definition as Products.swift and new version of test code SerializationTestTests.swift. |
JE Miller (JIRA User) The crash you're seeing is because you're passing your |
Comment by John E. Miller (JIRA) Thanks, the call to encoder via container does not crash; I put the call last. And the result is better: Although I hadn't expected the use of "super" as another level, it makes sense. Though I'm not sure how this will play out in the decode side. Or over multiple levels of subclassing. I suppose I will need to do something similar for my init with decode as well? I'm embarrassed to admit that I don't know which build of Swift I am running. |
JE Miller (JIRA User) Yes, you'll need to do something similar in decode: For multiple levels of subclassing, you can expect to see the following structure: {
sub-subclass properties...,
"super":
{
subclass properties...,
"super": { base class properties }
}
} And no worries — just wanted to make sure this isn't still reproducing on the latest builds of Swift. If you get Xcode 9 beta 3 or one of the latest toolchains, you shouldn't see that particular crash anymore. |
Comment by John E. Miller (JIRA) Cool. Xcode 9 beta 3 works fine with my subclass example - both encoding and decoding. |
Great! The core issue here still needs to be resolved, but glad this case works for you now. |
Attachment: Download
Environment
Xcode 9.0 beta. Swift 4. Mac OSX 10.12.5
Additional Detail from JIRA
md5: 1e8b12f9f95b483d1f9167dfda98f90c
duplicates:
Issue Description:
Using the simplest possible class structure and defaulting everything, subclass String properties are not encoded nor decoded. Base class properties no problem. Here is the class structure, the test code which demonstrates the problem is given as an attachment. This is a much simpler case than SR-4772 and SR-5125.
class Product: Codable {
var family: String = ""
}
class SubProduct: Product {
var name: String = ""
}
The text was updated successfully, but these errors were encountered: