URLEncodedFormEncoder
public final class URLEncodedFormEncoder
An object that encodes instances into URL-encoded query strings.
ArrayEncoding can be used to configure how Array values are encoded. By default, the .brackets encoding is
used, encoding array values with brackets for each value. e.g array[]=1&array[]=2.
BoolEncoding can be used to configure how Bool values are encoded. By default, the .numeric encoding is used,
encoding true as 1 and false as 0.
DataEncoding can be used to configure how Data values are encoded. By default, the .deferredToData encoding is
used, which encodes Data values using their default Encodable implementation.
DateEncoding can be used to configure how Date values are encoded. By default, the .deferredToDate
encoding is used, which encodes Dates using their default Encodable implementation.
KeyEncoding can be used to configure how keys are encoded. By default, the .useDefaultKeys encoding is used,
which encodes the keys directly from the Encodable implementation.
KeyPathEncoding can be used to configure how paths within nested objects are encoded. By default, the .brackets
encoding is used, which encodes each sub-key in brackets. e.g. parent[child][grandchild]=value.
NilEncoding can be used to configure how nil Optional values are encoded. By default, the .dropKey encoding
is used, which drops nil key / value pairs from the output entirely.
SpaceEncoding can be used to configure how spaces are encoded. By default, the .percentEscaped encoding is used,
replacing spaces with %20.
This type is largely based on Vapor’s url-encoded-form project.
-
Encoding to use for
See moreArrayvalues.Declaration
Swift
public enum ArrayEncoding -
Encoding to use for
See moreBoolvalues.Declaration
Swift
public enum BoolEncoding -
Encoding to use for
See moreDatavalues.Declaration
Swift
public enum DataEncoding -
Encoding to use for
See moreDatevalues.Declaration
Swift
public enum DateEncoding -
Encoding to use for keys.
This type is derived from
See moreJSONEncoder‘sKeyEncodingStrategyandXMLEncodersKeyEncodingStrategy.Declaration
Swift
public enum KeyEncoding -
Encoding to use for nested object and
Encodablevalue key paths.["parent" : ["child" : ["grandchild": "value"]]]This encoding affects how the
See moreparent,child,grandchildpath is encoded. Brackets are used by default. e.g.parent[child][grandchild]=value.Declaration
Swift
public struct KeyPathEncoding : Sendable -
Encoding to use for
See morenilvalues.Declaration
Swift
public struct NilEncoding : Sendable -
Encoding to use for spaces.
See moreDeclaration
Swift
public enum SpaceEncoding -
See moreURLEncodedFormEncodererror.Declaration
Swift
public enum Error : Swift.Error -
Whether or not to sort the encoded key value pairs.
Note
This setting ensures a consistent ordering for all encodings of the same parameters. When set tofalse, encodedDictionaryvalues may have a different encoded order each time they’re encoded due toDictionary‘s random storage order, butEncodabletypes will maintain their encoded order.Declaration
Swift
public let alphabetizeKeyValuePairs: Bool -
The
ArrayEncodingto use.Declaration
Swift
public let arrayEncoding: ArrayEncoding -
The
BoolEncodingto use.Declaration
Swift
public let boolEncoding: BoolEncoding -
THe
DataEncodingto use.Declaration
Swift
public let dataEncoding: DataEncoding -
The
DateEncodingto use.Declaration
Swift
public let dateEncoding: DateEncoding -
The
KeyEncodingto use.Declaration
Swift
public let keyEncoding: KeyEncoding -
The
KeyPathEncodingto use.Declaration
Swift
public let keyPathEncoding: KeyPathEncoding -
The
NilEncodingto use.Declaration
Swift
public let nilEncoding: NilEncoding -
The
SpaceEncodingto use.Declaration
Swift
public let spaceEncoding: SpaceEncoding -
The
CharacterSetof allowed (non-escaped) characters.Declaration
Swift
public var allowedCharacters: CharacterSet -
init(alphabetizeKeyValuePairs:arrayEncoding: boolEncoding: dataEncoding: dateEncoding: keyEncoding: keyPathEncoding: nilEncoding: spaceEncoding: allowedCharacters: ) Creates an instance from the supplied parameters.
Declaration
Swift
public init(alphabetizeKeyValuePairs: Bool = true, arrayEncoding: ArrayEncoding = .brackets, boolEncoding: BoolEncoding = .numeric, dataEncoding: DataEncoding = .base64, dateEncoding: DateEncoding = .deferredToDate, keyEncoding: KeyEncoding = .useDefaultKeys, keyPathEncoding: KeyPathEncoding = .brackets, nilEncoding: NilEncoding = .dropKey, spaceEncoding: SpaceEncoding = .percentEscaped, allowedCharacters: CharacterSet = .afURLQueryAllowed)Parameters
alphabetizeKeyValuePairsWhether or not to sort the encoded key value pairs.
trueby default.arrayEncodingThe
ArrayEncodingto use..bracketsby default.boolEncodingThe
BoolEncodingto use..numericby default.dataEncodingThe
DataEncodingto use..base64by default.dateEncodingThe
DateEncodingto use..deferredToDateby default.keyEncodingThe
KeyEncodingto use..useDefaultKeysby default.nilEncodingThe
NilEncodingto use..dropby default.spaceEncodingThe
SpaceEncodingto use..percentEscapedby default.allowedCharactersThe
CharacterSetof allowed (non-escaped) characters..afURLQueryAllowedby default. -
Encodes the
valueas a URL form encodedString.Throws
An
ErrororEncodingErrorinstance if encoding fails.Declaration
Swift
public func encode(_ value: any Encodable) throws -> StringParameters
valueThe
Encodablevalue.Return Value
The encoded
String. -
Encodes the value as
Data. This is performed by first creating an encodedStringand then returning the.utf8data.Throws
An
ErrororEncodingErrorinstance if encoding fails.Declaration
Swift
public func encode(_ value: any Encodable) throws -> DataParameters
valueThe
Encodablevalue.Return Value
The encoded
Data.
View on GitHub
Install in Dash