Skip to content

Instantly share code, notes, and snippets.

@oconnelltoby
Created August 10, 2021 17:05
Show Gist options
  • Save oconnelltoby/8b3b6eec369ce9104a707e15e87b6c62 to your computer and use it in GitHub Desktop.
Save oconnelltoby/8b3b6eec369ce9104a707e15e87b6c62 to your computer and use it in GitHub Desktop.
A wrapper for potentially malformed JSON objects
struct OptionallyDecodable<Value: Decodable>: Decodable {
let value: Value?
init(from decoder: Decoder) throws {
do {
let container = try decoder.singleValueContainer()
self.value = try container.decode(Value.self)
} catch {
self.value = nil
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment