From 999ae061422954535a495a89beac3eda0013b9a5 Mon Sep 17 00:00:00 2001 From: Dzmitry Letko Date: Sun, 11 Jan 2026 00:57:33 +0100 Subject: [PATCH] Add support of CodableWithConfiguration types --- Sources/Decoder/DictionaryDecoder.swift | 14 ++++++++++++++ Sources/Encoder/DictionaryEncoder.swift | 24 ++++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/Sources/Decoder/DictionaryDecoder.swift b/Sources/Decoder/DictionaryDecoder.swift index 63dce5b..dc067a6 100644 --- a/Sources/Decoder/DictionaryDecoder.swift +++ b/Sources/Decoder/DictionaryDecoder.swift @@ -70,4 +70,18 @@ public final class DictionaryDecoder: Sendable { public func decode(from dictionary: [String: Any]) throws -> T { try decode(T.self, from: dictionary) } + + @available(macOS 12, iOS 15, tvOS 15, watchOS 8, *) + public func decode(_ type: T.Type = T.self, from dictionary: [String: Any], configuration: T.DecodingConfiguration) throws -> T { + let options = optionsMutex.withLock(\.self) + + let decoder = DictionarySingleValueDecodingContainer( + component: dictionary, + options: options, + userInfo: userInfo, + codingPath: [] + ) + + return try T(from: decoder, configuration: configuration) + } } diff --git a/Sources/Encoder/DictionaryEncoder.swift b/Sources/Encoder/DictionaryEncoder.swift index 21fa463..37d945f 100644 --- a/Sources/Encoder/DictionaryEncoder.swift +++ b/Sources/Encoder/DictionaryEncoder.swift @@ -83,4 +83,28 @@ public final class DictionaryEncoder: Sendable { return dictionary } + + @available(macOS 12, iOS 15, tvOS 15, watchOS 8, *) + public func encode(_ value: T, configuration: T.EncodingConfiguration) throws -> [String: Sendable] { + let options = optionsMutex.withLock(\.self) + + let encoder = DictionarySingleValueEncodingContainer( + options: options, + userInfo: userInfo, + codingPath: [] + ) + + try value.encode(to: encoder, configuration: configuration) + + guard let dictionary = encoder.resolveValue() as? [String: Sendable] else { + let errorContext = EncodingError.Context( + codingPath: [], + debugDescription: "Root component cannot be encoded in Dictionary" + ) + + throw EncodingError.invalidValue(value, errorContext) + } + + return dictionary + } }