# propertyWrapper decoding behaviour

**URL:** <https://forums.swift.org/t/propertywrapper-decoding-behaviour/61076>\
**Category:** Using Swift\
**Tags:** propertywrapper\
**Created:** [October 27, 2022, 11:24pm UTC](https://forums.swift.org/t/propertywrapper-decoding-behaviour/61076 "2022-10-27T23:24:21Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![swhitty](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/swhitty/32/10082_2.png) [@swhitty](https://forums.swift.org/u/swhitty)\
**Post date:** [October 28, 2022, 7:15am UTC](https://forums.swift.org/t/propertywrapper-decoding-behaviour/61076/2 "2022-10-28T07:15:12Z")

</div>

I agree that using `@propertyWrapper` is a great way to customise the behaviour of `Codable` in a declarative manner. There are a few tricks required which make sense when you think about what the compiler is doing for us.

The wrapper must conform to `Codable` because it is encoded and decoded, but you probably would like to omit its synthesised container from encoding `{"wrappedValue": ....}` and skip directly to the wrapped value using `singleValueContainer()`

```swift
@propertyWrapper
struct Passthrough<T: Codable>: Codable {
  var wrappedValue: T

  init(wrappedValue: T) {
    self.wrappedValue = wrappedValue
  }

  init(from decoder: Decoder) throws {
    let container = try decoder.singleValueContainer()
    self.wrappedValue = try container.decode(T.self)
  }
}

```

Next when the compiler synthesises the decoding of `struct Person` it looks like this:

```swift
init(from decoder: Decoder) throws {
  let container = try decoder.container(keyedBy: CodingKeys.self)
  _foo = try container.decode(Passthrough<String?>.self, forKey: .foo)
}

```

This fails when `CodingKeys.foo` does not exist within the container so we should use `decodeIfPresent()` instead.

While we cannot change the synthesised code at all we can add a specialised extension to `KeyedDecodingContainer` that the synthesised code will call instead.

```swift
extension KeyedDecodingContainer {
  func decode<T>(_ type: Passthrough<T?>.Type, forKey key: Key) throws -> Passthrough<T?> {
    try decodeIfPresent(type, forKey: key) ?? Passthrough<T?>(wrappedValue: nil)
  }
}

```

This extension will appear to be dead code but the synthesised `init(from decoder: Decoder)` will be calling it.

---

_[View the full topic](https://forums.swift.org/t/propertywrapper-decoding-behaviour/61076)._
