Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions Sources/Decoder/DictionaryDecoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,18 @@ public final class DictionaryDecoder: Sendable {
public func decode<T: Decodable>(from dictionary: [String: Any]) throws -> T {
try decode(T.self, from: dictionary)
}

@available(macOS 12, iOS 15, tvOS 15, watchOS 8, *)
public func decode<T: DecodableWithConfiguration>(_ 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)
}
}
24 changes: 24 additions & 0 deletions Sources/Encoder/DictionaryEncoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,28 @@ public final class DictionaryEncoder: Sendable {

return dictionary
}

@available(macOS 12, iOS 15, tvOS 15, watchOS 8, *)
public func encode<T: EncodableWithConfiguration>(_ 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
}
}
Loading