# CodingKeyPath: Add support for Encoding and Decoding nested objects with dot notation

**URL:** <https://forums.swift.org/t/codingkeypath-add-support-for-encoding-and-decoding-nested-objects-with-dot-notation/34710>\
**Category:** Pitches\
**Tags:** codable\
**Created:** [March 22, 2020, 9:49pm UTC](https://forums.swift.org/t/codingkeypath-add-support-for-encoding-and-decoding-nested-objects-with-dot-notation/34710 "2020-03-22T21:49:49Z")\
**Posts on this page:** 4\
**Page:** 3

<div class="post-metadata">

**Author:** ![tomerd](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tomerd/32/3402_2.png) [@tomerd](https://forums.swift.org/u/tomerd)\
**Post date:** [August 4, 2021, 6:13pm UTC](https://forums.swift.org/t/codingkeypath-add-support-for-encoding-and-decoding-nested-objects-with-dot-notation/34710/43 "2021-08-04T18:13:05Z")

</div>

> [@Philippe\_Hausler](#):
>
> That almost seems like it should be an extension on `TopLevelDecoder` only right? Which currently lives in Combine, and I think I can safely say that was something we have wanted to move lower for a while now. Perhaps this is the impetus to do so?

personally, I would love to see that. Since its useful regardless, maybe a separate pitch to add a TopLevelDecoder/TopLevelEncoder to the Stdlib is in place, then if we decide TopLevelDecoder is the right place for such API, we can relate the second pitch to the TopLevelEncoder/TopLevelDecoder one.

---

<div class="post-metadata">

**Author:** ![Tony\_Parker](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tony_parker/32/273_2.png) [@Tony\_Parker](https://forums.swift.org/u/Tony_Parker)\
**Post date:** [August 11, 2021, 11:42pm UTC](https://forums.swift.org/t/codingkeypath-add-support-for-encoding-and-decoding-nested-objects-with-dot-notation/34710/44 "2021-08-11T23:42:50Z")

</div>

Is this possible to implement in a generic context like `TopLevelDecoder`? I imagined that `JSONDecoder` would be able to do a far more efficient implementation by descending into the JSON data manually first.

---

<div class="post-metadata">

**Author:** ![Tony\_Parker](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tony_parker/32/273_2.png) [@Tony\_Parker](https://forums.swift.org/u/Tony_Parker)\
**Post date:** [August 11, 2021, 11:43pm UTC](https://forums.swift.org/t/codingkeypath-add-support-for-encoding-and-decoding-nested-objects-with-dot-notation/34710/45 "2021-08-11T23:43:25Z")

</div>

If we decided here to add it to `JSONDecoder`, I will help make that happen for Foundation.

---

<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:** [August 12, 2021, 1:13am UTC](https://forums.swift.org/t/codingkeypath-add-support-for-encoding-and-decoding-nested-objects-with-dot-notation/34710/46 "2021-08-12T01:13:14Z")

</div>

One thought here: if this does get expressed in a generic way at a top-level context, I think it would be nice to spell the method as

```swift
static func decode<T: Decodable>(_ type: T.Type, from data: Data, at codingKeyPath: [CodingKey])

```

to allow key paths to include keys which have a delimiter in them (e.g. `"my.key"`, which would otherwise be parsed as `["my", "key"]`), as well as integer keys.

It's then possible to also offer

```swift
static func decode<T: Decodable>(_ type: T.Type, from data: Data, at codingKeyPath: String, separator: String = ".")

```

with a default implementation which splits `codingKeyPath` on the delimiter, turns each component into a `CodingKey`, then calls the `[CodingKey]` variant. `JSONDecoder` (and others) could also override this implementation to do something even more efficient if relevant.

(The `[CodingKey]` version can also made significantly easier to call using a general key type, e.g. `AnyCodingKey` pitched in [[Pitch] Allow coding of non-`String`/`Int` keyed `Dictionary` into a `KeyedContainer`](https://forums.swift.org/t/pitch-allow-coding-of-non-string-int-keyed-dictionary-into-a-keyedcontainer/44593))

[Previous page](https://forums.swift.org/t/codingkeypath-add-support-for-encoding-and-decoding-nested-objects-with-dot-notation/34710.md?page=2)
