Repository navigation
Fix encoding optional, nullable-valued, and nested dictionaries - #56
Merged
Merged
Conversation
A Skip Dictionary is also a Sequence of Tuple2, so dictionaries that reached Codable's type-erased paths were encoded as lists of Tuple2 pairs, which crashed real encoders ("Tuple2 cannot be cast to Encodable").
The reified KeyedEncodingContainer.encodeIfPresent for optional dictionaries forwarded to encode(value, forKey), which could not select the reified Dictionary overload, so it fell back to the generic path. It now chooses a keyed or unkeyed container from its reified key type directly, encoding a keyed container only for non-optional String or Int keys, as Swift does.
The generic encode entry points (TopLevelEncoder and the keyed, unkeyed, and single-value containers) and codableDictionaryKeyedEncode now check for Dictionary before Sequence. Those paths have lost the static key type, so they infer it from the runtime keys: a keyed container if all keys are Strings or all are Ints, otherwise an array of alternating keys and values. This covers dictionaries with nullable values, dictionaries nested in dictionaries or arrays, and generic-keyed dictionaries. An erased empty dictionary is encoded as a keyed container, so an empty nested dictionary with other key types encodes as {} where Swift encodes [].
codableUnkeyedEncode and codableSingleValueEncode now pass collections to their container's encode, which handles both sequences and dictionaries.
Enables the #54 tests in CodableTests and adds tests for generic-keyed dictionaries and optional dictionaries with optional keys.
Fixes #54
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
A Skip Dictionary is also a Sequence of Tuple2, so dictionaries that reached Codable's type-erased paths were encoded as lists of Tuple2 pairs, which crashed real encoders ("Tuple2 cannot be cast to Encodable").
The reified KeyedEncodingContainer.encodeIfPresent for optional dictionaries forwarded to encode(value, forKey), which could not select the reified Dictionary overload, so it fell back to the generic path. It now chooses a keyed or unkeyed container from its reified key type directly, encoding a keyed container only for non-optional String or Int keys, as Swift does.
The generic encode entry points (TopLevelEncoder and the keyed, unkeyed, and single-value containers) and codableDictionaryKeyedEncode now check for Dictionary before Sequence. Those paths have lost the static key type, so they infer it from the runtime keys: a keyed container if all keys are Strings or all are Ints, otherwise an array of alternating keys and values. This covers dictionaries with nullable values, dictionaries nested in dictionaries or arrays, and generic-keyed dictionaries. An erased empty dictionary is encoded as a keyed container, so an empty nested dictionary with other key types encodes as {} where Swift encodes [].
codableUnkeyedEncode and codableSingleValueEncode now pass collections to their container's encode, which handles both sequences and dictionaries.
Enables the #54 tests in CodableTests and adds tests for generic-keyed dictionaries and optional dictionaries with optional keys.
Fixes #54
Skip Pull Request Checklist:
swift testThe I reviewed the tests in #54. The tests pass on macOS, which validates the tests, and now, they pass on Android.