Skip to content
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

only superclass keys are encoded #4

Closed
laimis17 opened this issue Feb 18, 2020 · 1 comment
Closed

only superclass keys are encoded #4

laimis17 opened this issue Feb 18, 2020 · 1 comment

Comments

@laimis17
Copy link

laimis17 commented Feb 18, 2020

This is regarding chapter 7 playground MessagePackEncoder.
Where's my class info 🙂 ?

class PlaneClass: Encodable {
var manufacturer: String = "Cirrus"
var model: String = "SR22"
var seats: Int = 4
}

class Boeing: PlaneClass {
var engine: String = "ENGINE1"
}

var plane1 = Boeing()

let classencoder = MessagePackEncoder()
let data1 = try! classencoder.encode(plane1)

print(data1.map { String(format:"%02X", $0) })

Only superclass keys are encoded...
Expected Boeing class engine key to be encoded.
both playground and this code encodes same keys:

Playground(struct) code:

"encoding" CodingKeys(stringValue: "manufacturer", intValue: nil)
"encoding" CodingKeys(stringValue: "model", intValue: nil)
"encoding" CodingKeys(stringValue: "seats", intValue: nil)
["83", "A5", "73", "65", "61", "74", "73", "04", "A5", "6D", "6F", "64", "65", "6C", "A4", "53", "52", "32", "32", "AC", "6D", "61", "6E", "75", "66", "61", "63", "74", "75", "72", "65", "72", "A6", "43", "69", "72", "72", "75", "73"]

My code:

"encoding" CodingKeys(stringValue: "manufacturer", intValue: nil)
"encoding" CodingKeys(stringValue: "model", intValue: nil)
"encoding" CodingKeys(stringValue: "seats", intValue: nil)
["83", "AC", "6D", "61", "6E", "75", "66", "61", "63", "74", "75", "72", "65", "72", "A6", "43", "69", "72", "72", "75", "73", "A5", "6D", "6F", "64", "65", "6C", "A4", "53", "52", "32", "32", "A5", "73", "65", "61", "74", "73", "04"]
nil

@mattt
Copy link
Member

mattt commented Mar 5, 2021

@laimis17 This is a limitation of Codable synthesis, rather than a problem with MessagePackEncoder. You'll get the same behavior with JSONEncoder. See this thread for more information. I've made a note to myself to discuss this in the next update to the book.

@mattt mattt closed this as completed Mar 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants