DeepCodable: Encode and decode deeply-nested data into flat Swift objects
Have you ever gotten a response from an API that looked like this and wanted to pull out and flatten the values you care about?
(This is a real response from the GitHub GraphQL API, with only the actual values changed)
Conform a type you want to decode to DeepDecodable by defining a coding tree representing which nodes are bound to which values:
struct DeeplyNestedResponse: DeepDecodable {
static let codingTree = CodingTree {
Key("topLevel") {
Key("secondLevel") {
Key("thirdLevel", containing: \._property)
}
}
}
/*
Also valid is the flattened form:
static let codingTree = CodingTree {
Key("topLevel", "secondLevel", "thirdLevel", containing: \._property)
}
*/
@Value var property: String
}
/*
Corresponding JSON would look like:
{
"topLevel": {
"secondLevel": {
"thirdLevel: "{some value}"
}
}
}
*/
Nodes in your codingTree are made of Keys initialized one of the following ways:
Key("name") { /* More Keys */ }: node that don’t capture values directly, but contain other nodes
This maps to a serialized representation like {"name": { ... } }
Key("name", containing: \._value): node that should be decoded into the value property
All values to decode must be wrapped with the @Value property wrapper, and the \._{name} syntax refers directly to the wrapping instance (\.{name} without the underscore refers to the actual underlying value).
Decode a value into an instance of your type:
let instance = try JSONDecoder().decode(Response.self, from: jsonData)
While decoding is probably the most common use-case for this type of nested decoding, this package also supports encoding a flat Swift struct into a deeply nested one with the same pattern:
struct DeeplyNestedRequest: DeepEncodable {
static let codingTree = CodingTree {
Key("topLevel") {
Key("secondLevel") {
Key("thirdLevel", containing: \.bareProperty)
}
Key("otherSecondLevel", containing: \._wrappedProperty)
}
}
/*
Also valid is the flattened form:
static let codingTree = CodingTree {
Key("topLevel") {
Key("secondLevel", "thirdLevel", containing: \.bareProperty)
Key("otherSecondLevel", containing: \._wrappedProperty)
}
}
*/
let bareProperty: String
@Value var wrappedProperty: String
}
/*
Corresponding JSON would look like:
{
"topLevel": {
"secondLevel": {
"thirdLevel: "{bareProperty}"
},
"otherSecondLevel": "{wrappedProperty}"
}
}
*/
let instance: DeeplyNestedRequest = ...
let jsonData = try JSONEncoder().encode(instance)
With encoding, you don’t have to use the @Value wrappers, though you can if you’d like to support decoding and encoding on the same type (in which case you can conform to DeepCodable as an alias for the two).
Key features
Encoding and decoding a Swift object to/from an arbitrarily complex deeply nested serialized representation without manually writing Codable implementations
Preservation of existing Codable behavior on the values being encoded/decoded, including custom types
Since DeepCodable is just a custom implementation of the Codable requirements, this also means you can nest DeepCodable objects like in the GithubGraphqlResponse example
When conforming to DeepEncodable or DeepDecodable, don’t interfere with the opposite normal Codable implementation (Decodable/Encodable, respectively)
You can declare something like struct Response: DeepDecodable, Encodable { ... } and decode from a deeply nested tree, and then re-encode back to a flat structure like normal Encodable objects
No requirement for @Value property wrapper for types only conforming to DeepEncodable
Omission of the corresponding tree sections when all values at the leaves are nil
This makes it so trying to encode an object with a nil value doesn’t result in something like {"top": {"second": {"third": null} } }
Flattened shortcuts using variadic parameters for long paths with no branching:
DeepCodable
: Encode and decode deeply-nested data into flat Swift objectsHave you ever gotten a response from an API that looked like this and wanted to pull out and flatten the values you care about? (This is a real response from the GitHub GraphQL API, with only the actual values changed)
DeepCodable
lets you easily do so in Swift while maintaining type-safety, with the magic of result builders, key paths, and property wrappers:Quick start
Add to your
Package.Swift
:Conform a type you want to decode to
DeepDecodable
by defining a coding tree representing which nodes are bound to which values:Nodes in your
codingTree
are made ofKey
s initialized one of the following ways:Key("name") { /* More Keys */ }
: node that don’t capture values directly, but contain other nodes{"name": { ... } }
Key("name", containing: \._value)
: node that should be decoded into thevalue
propertyAll values to decode must be wrapped with the
@Value
property wrapper, and the\._{name}
syntax refers directly to the wrapping instance (\.{name}
without the underscore refers to the actual underlying value).Decode a value into an instance of your type:
DeepCodable
is built on top of normalCodable
, so any decoder (like the property list decoder inFoundation
or the excellent third-party YAML decoder, Yams) can be used to decode values.Encoding
While decoding is probably the most common use-case for this type of nested decoding, this package also supports encoding a flat Swift struct into a deeply nested one with the same pattern:
With encoding, you don’t have to use the
@Value
wrappers, though you can if you’d like to support decoding and encoding on the same type (in which case you can conform toDeepCodable
as an alias for the two).Key features
Encoding and decoding a Swift object to/from an arbitrarily complex deeply nested serialized representation without manually writing
Codable
implementationsPreservation of existing
Codable
behavior on the values being encoded/decoded, including custom typesDeepCodable
is just a custom implementation of theCodable
requirements, this also means you can nestDeepCodable
objects like in theGithubGraphqlResponse
exampleWhen conforming to
DeepEncodable
orDeepDecodable
, don’t interfere with the opposite normalCodable
implementation (Decodable
/Encodable
, respectively)struct Response: DeepDecodable, Encodable { ... }
and decode from a deeply nested tree, and then re-encode back to a flat structure like normalEncodable
objectsNo requirement for
@Value
property wrapper for types only conforming toDeepEncodable
Omission of the corresponding tree sections when all values at the leaves are
nil
nil
value doesn’t result in something like{"top": {"second": {"third": null} } }
Flattened shortcuts using variadic parameters for long paths with no branching:
Key("topLevel", "secondLevel", containing: \._property)
instead ofKey("topLevel") { Key("secondLevel", containing: \._property) }