# Using Property Wrappers with Codable

**URL:** <https://forums.swift.org/t/using-property-wrappers-with-codable/29804>\
**Category:** Using Swift\
**Tags:** codable, propertywrapper\
**Created:** [October 14, 2019, 10:40am UTC](https://forums.swift.org/t/using-property-wrappers-with-codable/29804 "2019-10-14T10:40:49Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![Lantua](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lantua/32/8868_2.png) [@Lantua](https://forums.swift.org/u/Lantua)\
**Post date:** [October 14, 2019, 10:42pm UTC](https://forums.swift.org/t/using-property-wrappers-with-codable/29804/4 "2019-10-14T22:42:01Z")

</div>

The way it is done in normal synthesizer is that:

1. Generate `CodingKeys` if not already existed.
2. Generate `init(from:)` if not already existed.

Further, the synthesizer uses appropriate variation of [`decodeIfPresent`](https://developer.apple.com/documentation/swift/keyeddecodingcontainer/2892670-decodeifpresent) when the type is `Optional`, and normal `decode` otherwise. That's why it'd work nicely if your value is `Double?`--the synthesizer will use `decodeIfPresent`.

Now, what you need is something that is expanded to

```swift
struct Test: Decodable {
  var value: StringRepresentation<Double>?
}

```

but your code actually expands into (at least from codable synthesizer perspective)

```swift
struct Test: Decodable {
  var value: StringRepresentation<Double?>
}

```

And so the synthesizer will uses normal `decode` function, which will throw error if there is no key in the JSON data. The error is thrown before the initializer is called, so there's nothing from inside `propertyWrapper` that can be done.

We'll probably need a better system to express propertyWrapper with optional.

---

_[View the full topic](https://forums.swift.org/t/using-property-wrappers-with-codable/29804)._
