This is a XML encoder and decoder that integrates with the Swift Codable system. It can create an XML tree from a Codable class or create Codable classes from an XML tree. The encoder uses its own XML classes which fairly closely follow the format of the XMLNode classes found in the macOS and Linux Foundation classes.
Using the classes
The basic method for saving a XML file from a Codable class or struct is as follows
let xml = try XMLEncoder().encode(codable)
let xmlString = xml.xmlString
And to create a Codable class from xml data
let xmlDocument = try XML.Document(data: data)
let codable = try XMLDecoder().decode(Codable.self, from: xmlDocument.rootElement)
In XML the way collections are encoded can vary quite a bit. In a previous version of this library I tried to implement this inside the encoder/decoder. I have removed this code in favour of using codable property wrappers. These allow us to mark up member variables of a class with how we want them to serialize. The library comes with a series of properties wrappers to control collection serialization. There are two property wrappers @CustomCoding to be used with non-optional member variables and @CustomOptionalCoding to be used with optional member variables. Both property wrappers have a CustomCoder generic variable. This CustomCoder is a protocol which has two static functions decode and encode. These functions are used to provide custom encoding and decoding for an object. The library comes with CustomCoder classes for arrays and dictionaries.
There is a typealias DefaultArrayCoder for the common element name “member” which reduces the verbosity of the code a little. With this the struct would look like this.
struct Object {
@CustomCoding<DefaultArrayCoder> var array: [Int]
}
There are similar structures for defining Dictionary encoding and decoding. Except they have three possible variables, the name of each element node, the name of the key node and the name of the value node.
XML Encoder
This is a XML encoder and decoder that integrates with the Swift Codable system. It can create an XML tree from a Codable class or create Codable classes from an XML tree. The encoder uses its own XML classes which fairly closely follow the format of the XMLNode classes found in the macOS and Linux Foundation classes.
Using the classes
The basic method for saving a XML file from a Codable class or struct is as follows
And to create a Codable class from xml data
Reference documentation can be found here.
Encoding arrays and dictionaries
In XML the way collections are encoded can vary quite a bit. In a previous version of this library I tried to implement this inside the encoder/decoder. I have removed this code in favour of using codable property wrappers. These allow us to mark up member variables of a class with how we want them to serialize. The library comes with a series of properties wrappers to control collection serialization. There are two property wrappers
@CustomCoding
to be used with non-optional member variables and@CustomOptionalCoding
to be used with optional member variables. Both property wrappers have aCustomCoder
generic variable. ThisCustomCoder
is a protocol which has two static functionsdecode
andencode
. These functions are used to provide custom encoding and decoding for an object. The library comes withCustomCoder
classes for arrays and dictionaries.By default an object like the following
would serialize as follows
By using the
ArrayCoder
object and aArrayCoderProperties
object to define the element names along with the@CustomCoding
property wrapperit now serialize as follows
There is a typealias
DefaultArrayCoder
for the common element name “member” which reduces the verbosity of the code a little. With this the struct would look like this.There are similar structures for defining Dictionary encoding and decoding. Except they have three possible variables, the name of each element node, the name of the key node and the name of the value node.