# 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:** 1\
**Showing post:** 46

<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))

---

_[View the full topic](https://forums.swift.org/t/codingkeypath-add-support-for-encoding-and-decoding-nested-objects-with-dot-notation/34710)._
