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
23 changes: 22 additions & 1 deletion Sources/Decoder/URLQueryDecoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public final class URLQueryDecoder: Sendable {
}

public func decode<T: Decodable>(
_ type: T.Type,
_ type: T.Type = T.self,
from query: String
) throws -> T {
let options = optionsMutex.withLock { $0 }
Expand All @@ -68,4 +68,25 @@ public final class URLQueryDecoder: Sendable {

return try T(from: decoder)
}

@available(macOS 12, iOS 15, tvOS 15, watchOS 8, *)
public func decode<T: DecodableWithConfiguration>(
_ type: T.Type = T.self,
from query: String,
configuration: T.DecodingConfiguration
) throws -> T {
let options = optionsMutex.withLock { $0 }

let deserializer = URLQueryDeserializer()
let value = try deserializer.deserialize(query)

let decoder = URLQuerySingleValueDecodingContainer(
value: value,
options: options,
userInfo: userInfo,
codingPath: []
)

return try T(from: decoder, configuration: configuration)
}
}
32 changes: 32 additions & 0 deletions Sources/Encoder/URLQueryEncoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,36 @@ public final class URLQueryEncoder: Sendable {

return serializer.serialize(urlEncodedForm)
}

@available(macOS 12, iOS 15, tvOS 15, watchOS 8, *)
public func encode<T: EncodableWithConfiguration>(
_ value: T,
configuration: T.EncodingConfiguration
) throws -> String {
let options = optionsMutex.withLock { $0 }

let encoder = URLQuerySingleValueEncodingContainer(
options: options,
userInfo: userInfo,
codingPath: []
)

try value.encode(to: encoder, configuration: configuration)

guard case let .dictionary(urlEncodedForm) = encoder.resolveValue() else {
let errorContext = EncodingError.Context(
codingPath: [],
debugDescription: "Root component cannot be encoded in URL"
)

throw EncodingError.invalidValue(value, errorContext)
}

let serializer = URLQuerySerializer(
arrayEncodingStrategy: arrayEncodingStrategy,
spaceEncodingStrategy: spaceEncodingStrategy
)

return serializer.serialize(urlEncodedForm)
}
}