Skip to content

fix: implement superEncoder methods for Codable class hierarchies#123

Open
hamchapman wants to merge 1 commit intohc/fix-unsafe-error-handling-and-allocation-crashesfrom
hc/fix-unimplemented-codable-superencoder-methods
Open

fix: implement superEncoder methods for Codable class hierarchies#123
hamchapman wants to merge 1 commit intohc/fix-unsafe-error-handling-and-allocation-crashesfrom
hc/fix-unimplemented-codable-superencoder-methods

Conversation

@hamchapman
Copy link
Copy Markdown
Collaborator

Previously, superEncoder() and superEncoder(forKey:) methods in both KeyedEncodingContainer and UnkeyedEncodingContainer would crash with fatalError("Unimplemented").

This commit implements these methods to properly support Swift class inheritance with Codable, allowing subclasses to encode their superclass properties into nested containers.

Changes:

  • KeyedEncodingContainer.superEncoder(): Creates encoder under "super" key
  • KeyedEncodingContainer.superEncoder(forKey:): Creates encoder under custom key
  • UnkeyedEncodingContainer.superEncoder(): Appends encoder as array element
  • Make _CBOREncoder conform to CBOREncodingContainer protocol

Previously, `superEncoder()` and `superEncoder(forKey:)` methods in both
`KeyedEncodingContainer` and `UnkeyedEncodingContainer` would crash with
`fatalError("Unimplemented")`.

This commit implements these methods to properly support Swift class inheritance
with Codable, allowing subclasses to encode their superclass properties into
nested containers.

**Changes:**

- `KeyedEncodingContainer.superEncoder()`: Creates encoder under "super" key
- `KeyedEncodingContainer.superEncoder(forKey:)`: Creates encoder under custom key
- `UnkeyedEncodingContainer.superEncoder()`: Appends encoder as array element
- Make `_CBOREncoder` conform to `CBOREncodingContainer` protocol
@hamchapman hamchapman self-assigned this Feb 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant