Skip to content

Refactor DictionaryEncoder/DictionaryDecoder to struct + Sendable #3

Description

@leogdion

Summary

DictionaryEncoder and DictionaryDecoder are currently open class. They should be converted to public struct and made Sendable.

Proposed Changes

struct + Sendable

  • open class DictionaryEncoderpublic struct DictionaryEncoder: Sendable
  • open class DictionaryDecoderpublic struct DictionaryDecoder: Sendable
  • open var properties → public var

userInfo

  • userInfo: [CodingUserInfoKey: Any]userInfo: [CodingUserInfoKey: any Sendable] on both types
  • Update DictionaryEncoderOptions and DictionaryDecoderOptions to match
  • Bridge [CodingUserInfoKey: any Sendable][CodingUserInfoKey: Any] in DictionaryEncoderImpl/DictionaryDecoderImpl to satisfy the Encoder/Decoder protocol requirement

Remove DateEncodingStrategy / DateDecodingStrategy

Unlike JSON, [String: Any] can hold a Date value natively — no serialization to string or number is needed. The strategies .secondsSince1970, .millisecondsSince1970, .iso8601, and .formatted(DateFormatter) only make sense for formats that cannot represent Date directly. For a dictionary, .deferredToDate is the only meaningful strategy, so the entire DateEncodingStrategy/DateDecodingStrategy enums can be removed. This also eliminates the DateFormatter / Sendable problem entirely and allows the struct to conform to Sendable without @unchecked.

Other Sendable fixes

  • MissingValueDecodingStrategy.useDefault(defaults: [String: Any])[String: any Sendable]
  • Add @Sendable to all closure-typed enum cases (DataEncodingStrategy.custom, KeyEncodingStrategy.custom, DataDecodingStrategy.custom, KeyDecodingStrategy.custom)

Files to Update

  • Sources/DictionaryCoding/DictionaryEncoder.swift
  • Sources/DictionaryCoding/DictionaryDecoder.swift
  • Sources/DictionaryCoding/DictionaryEncoderOptions.swift
  • Sources/DictionaryCoding/DictionaryDecoderOptions.swift
  • Sources/DictionaryCoding/DictionaryEncoderImpl.swift
  • Sources/DictionaryCoding/DictionaryDecoderImpl.swift
  • Sources/DictionaryCoding/DictionaryEncoderImpl+Box.swift
  • Sources/DictionaryCoding/DictionaryDecoderImpl+UnboxDecodable.swift
  • Sources/DictionaryCoding/ISO8601DateFormatter+Shared.swift (can be deleted)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions