Class
XMLEncoder
open class XMLEncoder
XMLEncoder
facilitates the encoding of Encodable
values into XML.
Relationships
Nested Types
XMLEncoder.OutputFormatting
The formatting of the output XML data.
XMLEncoder.PrettyPrintIndentation
The indentation to use when XML is pretty-printed.
XMLEncoder.NodeEncoding
A node's encoding type. Specifies how a node will be encoded.
XMLEncoder.DateEncodingStrategy
The strategy to use for encoding
Date
values.XMLEncoder.StringEncodingStrategy
The strategy to use for encoding
String
values.XMLEncoder.DataEncodingStrategy
The strategy to use for encoding
Data
values.XMLEncoder.NonConformingFloatEncodingStrategy
The strategy to use for non-XML-conforming floating-point values (IEEE 754 infinity and NaN).
XMLEncoder.KeyEncodingStrategy
The strategy to use for automatically changing the value of keys before encoding.
XMLEncoder.NodeEncodingStrategy
Set of strategies to use for encoding of nodes.
Nested Type Aliases
NodeEncodingStrategies
@available(*, deprecated, renamed: "NodeEncodingStrategy")
public typealias NodeEncodingStrategies = NodeEncodingStrategy
XMLNodeEncoderClosure
public typealias XMLNodeEncoderClosure = (CodingKey) -> NodeEncoding?
XMLEncodingClosure
public typealias XMLEncodingClosure = (Encodable.Type, Encoder) -> XMLNodeEncoderClosure
Initializers
Properties
charactersEscapedInAttributes
open var charactersEscapedInAttributes = [
("&", "&"),
("<", "<"),
(">", ">"),
("'", "'"),
("\"", """),
]
Characters and their escaped representations to be escaped in attributes
charactersEscapedInElements
open var charactersEscapedInElements = [
("&", "&"),
("<", "<"),
(">", ">"),
("'", "'"),
("\"", """),
]
Characters and their escaped representations to be escaped in elements
outputFormatting
open var outputFormatting: OutputFormatting = []
The output format to produce. Defaults to []
.
prettyPrintIndentation
open var prettyPrintIndentation: PrettyPrintIndentation
The indentation to use when XML is printed. Defaults to .spaces(4)
.
dateEncodingStrategy
open var dateEncodingStrategy: DateEncodingStrategy = .deferredToDate
The strategy to use in encoding dates. Defaults to .deferredToDate
.
dataEncodingStrategy
open var dataEncodingStrategy: DataEncodingStrategy = .base64
The strategy to use in encoding binary data. Defaults to .base64
.
nonConformingFloatEncodingStrategy
open var nonConformingFloatEncodingStrategy: NonConformingFloatEncodingStrategy = .throw
The strategy to use in encoding non-conforming numbers. Defaults to .throw
.
keyEncodingStrategy
open var keyEncodingStrategy: KeyEncodingStrategy = .useDefaultKeys
The strategy to use for encoding keys. Defaults to .useDefaultKeys
.
nodeEncodingStrategy
open var nodeEncodingStrategy: NodeEncodingStrategy = .deferredToEncoder
The strategy to use in encoding encoding attributes. Defaults to .deferredToEncoder
.
stringEncodingStrategy
open var stringEncodingStrategy: StringEncodingStrategy = .deferredToString
The strategy to use in encoding strings. Defaults to .deferredToString
.
userInfo
open var userInfo: [CodingUserInfoKey: Any] = [:]
Contextual user-provided information for use during encoding.
Methods
encode(_:withRootKey:rootAttributes:header:doctype:)
open func encode<T: Encodable>(_ value: T,
withRootKey rootKey: String? = nil,
rootAttributes: [String: String]? = nil,
header: XMLHeader? = nil,
doctype: XMLDocumentType? = nil) throws -> Data
Encodes the given top-level value and returns its XML representation.
Parameters
Name | Type | Description |
---|---|---|
value | T |
The value to encode. |
withRootKey | String? |
the key used to wrap the encoded values. The default value is inferred from the name of the root type. |
rootAttributes | [String: String]? |
the list of attributes to be added to the root node |
header | XMLHeader? |
the XML header to start the encoded data with. |
Throws
An error if any value throws an error during encoding.
Returns
A new Data
value containing the encoded XML data.
encode(_:)
open func encode<T>(_ value: T) throws -> Data where T: Encodable