Skip to content

Fix encoding optional, nullable-valued, and nested dictionaries - #56

Merged
dfabulich merged 1 commit into
mainfrom
codable-dictionary-encoding
Dec 4, 2026
Merged

dfabulich merged 1 commit into
mainfrom
codable-dictionary-encoding

Conversation

@dfabulich

Copy link
Copy Markdown
Member

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:

  • REQUIRED: I have tested my change locally with swift test
  • OPTIONAL: I have tested my change on an iOS simulator or device
  • OPTIONAL: I have tested my change on an Android emulator or device

  • AI was used to generate or assist with generating this PR. Please specify below how you used AI to help you, and what steps you have taken to manually verify the changes.

The I reviewed the tests in #54. The tests pass on macOS, which validates the tests, and now, they pass on Android.

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
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.

Encoding optional or nested Dictionary values fails with Tuple2 ClassCastException

1 participant