# Proper way to structure containers in new coders?

**URL:** <https://forums.swift.org/t/proper-way-to-structure-containers-in-new-coders/11712>\
**Category:** Using Swift\
**Tags:** codable\
**Created:** [April 6, 2018, 1:01am UTC](https://forums.swift.org/t/proper-way-to-structure-containers-in-new-coders/11712 "2018-04-06T01:01:53Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![itaiferber](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/itaiferber/32/33840_2.png) [@itaiferber](https://forums.swift.org/u/itaiferber)\
**Post date:** [April 6, 2018, 8:51pm UTC](https://forums.swift.org/t/proper-way-to-structure-containers-in-new-coders/11712/2 "2018-04-06T20:51:07Z")

</div>

Sorry for the delay in responding to this — I just got back from vacation, so I’m catching up on a lot of threads here.

* * *

> [@DeFrenZ](#):
>
> So, without having to deal with this at the implementation level, I'm having troubles imagining how the encoding/decoding process would structure the containers stack to properly represent the formats. My understanding is that the container stack — which has keyed, unkeyed and single containers — is a generalised view over the concrete underlying format (e.g. `enum JSON`, `struct XMLElement`, etc). So does this mean that it supports only formats that can be represented as a combination of `Dictionary` (with `String` or `Int` key), `Array` and `Any`?

Formats whose contents are represented in terms of `Dictionary` (with `String` or `Int` keys), `Array`, numbers, `String`s, and null values offer representations in the _vast_ majority of formats. It's also entirely possible for an `Encoder` to collect an object graph in this representation and transform it into a more natural representation, too; there isn't a strict requirement that the object graph passed in is represented in the same order when serialized.

But yes, if you have a format which is truly incompatible with this model, then it might not be well-suited to fit the `Codable` API. This is a tradeoff we made in the design: if you try to capture something representative of _all_ serialization formats, you end up with very weak and vague API because there are ends of the spectrum which don't align with one another at all. But I would say that ~90% of serialization formats are extremely similar to one another and can be captured well here.

> [@DeFrenZ](#):
>
> #### XML
> 
> With the XML value I don't see issues on the decoding, but I think the encoding is ambiguous: how does the model give information to the `Encoder` whether a property will go into an XML attribute or a child XML element?  
> Given how `Codable` is structured I don't think this should come from info custom fed into the `Encoder` (e.g. through `userInfo`), but should come from the model itself. The suitable place for this seems to be `CodingKeys`, which specify _where_ the properties get coded.  
> Though the protocol doesn't leave options to define additional details about the keys, so I believe the option here is only to define something like `protocol XMLCodingKey: CodingKey` which allows for that. But `Encoder` will still work with a keyed container which will be defined with a `CodingKey`... should the encoder fail (i.e. `throw`) at runtime if the key is not `XMLCodingKey`?  
> Also this means that you have to give up the synthesised implementation, but I don't think there are ways around it...

This is the type of decision that I would posit is really up to the `Encoder`, not what's being encoded. XML has many, many ways to represent similar concepts, and I think the specific format choices should be up to, and controlled by the `Encoder`. For instance, the XML you give above could be represented in at least few ways off the top of my head:

- Your original (properties in XML attributes, except for complex values):

- Properties in child nodes with custom node types:

- Properties in child nodes with generalized node types (e.g. property-value pairs):

In all, this is up to the DTD/schema you're working with, and how the `Encoder` chooses to support these representations. This isn't a choice that the actual type should control (or even care about); a flag on the `Encoder` could potentially allow you to choose the representation you'd want.

> [@DeFrenZ](#):
>
> #### Protocol Buffers
> 
> In this case there are mainly two issues:
> 
> - the first, that it's easily solvable, is to provide the corresponding tag for each property. `CodingKey.intValue` is a perfect match for it. Unluckily the default implementation doesn't offer anything with this (1-indexed property order would be a nice default here, but not sure it fits elsewhere), but still it's easy to fix this with a manual declaration of `CodingKeys`

The manual declaration of `CodingKeys` is indeed the answer here. We didn't want the compiler to unwittingly introduce fragility into your `CodingKeys` enum by deciding on integer values on your behalf. Since all enum cases have names, string values are easy; integer values fall prey to reordering and naming concerns. `String` backing is relatively safe (short of renaming your property and the associated key); `Int` backing you'd have to do yourself.

> [@DeFrenZ](#):
>
> - the second, which is the real issue, is that the `Encoder`/`Decoder` needs information about the wire type used for each specific property (i.e. which of the multiple binary representations is used for the value). This _might_ be okay for decoding, but when encoding this information is necessary. I believe this is more or less the same issue as in XML and could be solved the same way.

Again, I think that depending on the model you're going for, this can be solved either by the `Encoder` itself, or by the type directly without need for anything special:

- If the desired format is such that the produced protobuf output is consistent in wire types across the board (e.g. use varints everywhere, or use fixed-width values everywhere, etc.), then there is an easy solution which is that the `Encoder` applies the same strategy across the board. The types themselves don't need to care about the wire formats of their individual properties, since they're all the same
- If different types care about the wired formats, then the protobuf `Encoder` can offer specific types for those wire formats — e.g., `Parent.value` would be of type `ProtobufEncoder.FixedInt64` (or something like that). `FixedInt64` would just wrap an `Int64` (and would encode as a regular `Int64` through all other encoders), but would be special for `ProtobufEncoder`: the encoder could intercept this type specifically to get its value to write out in the fixed format as needed. `ProtobufEncoder` would also offer `VarInt64`, and the Protobuf compiler could generate the different types based on your schema

* * *

> [@DeFrenZ](#):
>
> So, the actual question here is: am I thinking about this right? Are more specific `CodingKey` subprotocols the way to go when dealing with formats that needs more details about the properties, and `throw` at runtime if a model with a non-conforming `CodingKeys` is passed to the `Encoder`/`Decoder`?

To get at the core of this: I don't think refining the `CodingKey` protocol is necessary given the tools we already have at hand. Besides the additional complexity (conceptually, of implementation, etc.), I think between splitting some of the responsibilities here among the `Encoder` and the types being encoded, it's possible to do everything here today.

With a definition of

```swift
struct Parent : Codable {
    let value: ProtobufEncoder.FixedInt64
    let child: Child

    private enum CodingKeys : Int, CodingKey {
        case value = 1
        case child = 2
    }
}

struct Child : Codable {
    let childValue: String

    private enum CodingKeys : Int, CodingKey {
        case childValue = 1
    }
}

```

it should be possible to write `Parent` to JSON, XML, and Protobuf as-is today.

---

_[View the full topic](https://forums.swift.org/t/proper-way-to-structure-containers-in-new-coders/11712)._
