# How do I declare an all-encompassing Codable Dictionary?

**URL:** <https://forums.swift.org/t/how-do-i-declare-an-all-encompassing-codable-dictionary/12910>\
**Category:** Using Swift\
**Tags:** codable\
**Created:** [May 23, 2018, 4:24am UTC](https://forums.swift.org/t/how-do-i-declare-an-all-encompassing-codable-dictionary/12910 "2018-05-23T04:24:43Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![CTMacUser](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ctmacuser/32/1802_2.png) [@CTMacUser](https://forums.swift.org/u/CTMacUser)\
**Post date:** [May 23, 2018, 4:24am UTC](https://forums.swift.org/t/how-do-i-declare-an-all-encompassing-codable-dictionary/12910/1 "2018-05-23T04:24:44Z")

</div>

A `Dictionary` that can accept anything can be declared as `[AnyHashable: Any]`. But what if it needs to be `Codable` too? Would `[AnyHashable & Codable: Codable]` work?

---

<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:** [May 23, 2018, 5:17am UTC](https://forums.swift.org/t/how-do-i-declare-an-all-encompassing-codable-dictionary/12910/2 "2018-05-23T05:17:25Z")

</div>

`[AnyHashable & Codable : Codable]` expresses the requirement that these types be `Codable`, but note that this is not terribly useful as existential `Codable` values cannot be encoded or decoded. In order to decode a value, you need to have the concrete type you want — decoding needs to call `init(from:)` on a type, and calling `(AnyHashable & Codable).init(from:)` isn’t possible since which concrete type would that give you?

It is possible to express [an existential `AnyEncodable` type](https://forums.swift.org/t/how-to-encode-objects-of-unknown-type/12253/5), but without knowing your use case better, I wouldn’t recommend doing so unless you really need it, since you run the risk of encoding values you later cannot decode ([caveat to this approach](https://forums.swift.org/t/how-to-encode-objects-of-unknown-type/12253/9)).

What are you looking to do here specifically?

---

<div class="post-metadata">

**Author:** ![CTMacUser](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ctmacuser/32/1802_2.png) [@CTMacUser](https://forums.swift.org/u/CTMacUser)\
**Post date:** [May 23, 2018, 5:42am UTC](https://forums.swift.org/t/how-do-i-declare-an-all-encompassing-codable-dictionary/12910/3 "2018-05-23T05:42:46Z")

</div>

> [@itaiferber](#):
>
> What are you looking to do here specifically?

Send over JSON data. The data can be of Apple’s usual types: string, integer, floating, Boolean, and arrays, sets, and dictionaries of such.

---

<div class="post-metadata">

**Author:** ![zoul](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/zoul/32/9559_2.png) [@zoul](https://forums.swift.org/u/zoul)\
**Post date:** [May 23, 2018, 6:49am UTC](https://forums.swift.org/t/how-do-i-declare-an-all-encompassing-codable-dictionary/12910/4 "2018-05-23T06:49:12Z")

</div>

Generic JSON data can also be typed strongly:

```swift
public enum JSON {
    case string(String)
    case number(Float)
    case object([String:JSON])
    case array([JSON])
    case bool(Bool)
    case null
}

```

Then you can easily convert from `Codable` to this. [Sample code here](https://github.com/zoul/generic-json-swift). (But now that I think about it I don’t know if this helps your case at all.)

---

<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:** [May 23, 2018, 5:16pm UTC](https://forums.swift.org/t/how-do-i-declare-an-all-encompassing-codable-dictionary/12910/5 "2018-05-23T17:16:39Z")

</div>

@zoul's answer is the appropriate one — the right way to represent this is by expressing the type not as an existential, but as a concrete union of types (i.e. "string or integer or floating or boolean or array or set or dictionary or..."); the way this is done in Swift is by declaring an `enum` with associated values for the different cases that you have.

@zoul links to his JSON code to do this — I have a similar (if more simplified and naïve) version [here](https://gist.github.com/itaiferber/d26cf93355f3415f4f684510f21ca202).
