Questions tagged [codable]

Use this tag only for questions directly pertaining to the Swift Codable protocol introduced in Swift 4.

Serialization / deserialization protocol composed of Encodable and Decodable protocols. Introduced in Swift 4.

References

1568 questions
175
votes
8 answers

With JSONDecoder in Swift 4, can missing keys use a default value instead of having to be optional properties?

Swift 4 added the new Codable protocol. When I use JSONDecoder it seems to require all the non-optional properties of my Codable class to have keys in the JSON or it throws an error. Making every property of my class optional seems like an…
zekel
  • 9,227
  • 10
  • 65
  • 96
171
votes
16 answers

How to decode a property with type of JSON dictionary in Swift [45] decodable protocol

Let's say I have Customer data type which contains a metadata property that can contains any JSON dictionary in the customer object struct Customer { let id: String let email: String let metadata: [String: Any] } { "object": "customer", …
170
votes
7 answers

How to exclude properties from Swift Codable?

Swift's Encodable/Decodable protocols, released with Swift 4, make JSON (de)serialization quite pleasant. However, I have not yet found a way to have fine-grained control over which properties should be encoded and which should get decoded. I have…
RamwiseMatt
  • 2,717
  • 3
  • 16
  • 22
167
votes
16 answers

How can I use Swift’s Codable to encode into a dictionary?

I have a struct that implements Swift 4’s Codable. Is there a simple built-in way to encode that struct into a dictionary? let struct = Foo(a: 1, b: 2) let dict = something(struct) // now dict is ["a": 1, "b": 2]
zoul
  • 102,279
  • 44
  • 260
  • 354
166
votes
16 answers

Swift JSONDecode decoding arrays fails if single element decoding fails

While using Swift4 and Codable protocols I got the following problem - it looks like there is no way to allow JSONDecoder to skip elements in an array. For example, I have the following JSON: [ { "name": "Banana", "points": 200, …
Khriapin Dmitriy
  • 1,762
  • 2
  • 10
  • 9
144
votes
4 answers

How do I use custom keys with Swift 4's Decodable protocol?

Swift 4 introduced support for native JSON encoding and decoding via the Decodable protocol. How do I use custom keys for this? E.g., say I have a struct struct Address:Codable { var street:String var zip:String var city:String var…
chrismanderson
  • 4,603
  • 5
  • 31
  • 47
129
votes
7 answers

How to decode a nested JSON struct with Swift Decodable protocol?

Here is my JSON { "id": 1, "user": { "user_name": "Tester", "real_info": { "full_name":"Jon Doe" } }, "reviews_count": [ { "count": 4 } ] } Here is the structure I…
Just a coder
  • 15,480
  • 16
  • 85
  • 138
106
votes
12 answers

Codable class does not conform to protocol Decodable

Why am I getting a "Type 'Bookmark' does not conform to protocol 'Decodable'" error message? class Bookmark: Codable { weak var publication: Publication? var indexPath: [Int] var locationInText = 0 enum CodingKeys: String, CodingKey { …
Melodius
  • 2,505
  • 3
  • 22
  • 37
101
votes
7 answers

Using Decodable in Swift 4 with Inheritance

Should the use of class inheritance break the Decodability of class. For example, the following code class Server : Codable { var id : Int? } class Development : Server { var name : String? var userId : Int? } var json = "{\"id\" :…
Kevin McQuown
  • 1,277
  • 2
  • 9
  • 13
92
votes
6 answers

Encode nil value as null with JSONEncoder

I'm using Swift 4's JSONEncoder. I have a Codable struct with an optional property, and I'd like this property to show up as null value in the produced JSON data when the value is nil. However, JSONEncoder discards the property and does not add it…
dr_barto
  • 5,723
  • 3
  • 26
  • 47
71
votes
10 answers

Codable enum with default case in Swift 4

I have defined an enum as follows: enum Type: String, Codable { case text = "text" case image = "image" case document = "document" case profile = "profile" case sign = "sign" case inputDate = "input_date" case inputText =…
LucaRoverelli
  • 1,116
  • 1
  • 9
  • 15
69
votes
5 answers

Using codable with value that is sometimes an Int and other times a String

I have an API that will sometimes return a specific key value (in this case id) in the JSON as an Int and other times it will return that same key value as a String. How do I use codable to parse that JSON? struct GeneralProduct: Codable { var…
Nevin Jethmalani
  • 2,726
  • 4
  • 23
  • 58
66
votes
4 answers

How to convert a date string with optional fractional seconds using Codable in Swift?

I am replacing my old JSON parsing code with Swift's Codable and am running into a bit of a snag. I guess it isn't as much a Codable question as it is a DateFormatter question. Start with a struct struct JustADate: Codable { var date: Date …
Guillermo Alvarez
  • 1,695
  • 2
  • 18
  • 23
65
votes
1 answer

Custom Swift Encoder/Decoder for the Strings Resource Format

I have been playing around with Codable and reading and writing JSON from and to a file. Now I would like to write a custom Coder that can read and write iOS .strings files. Can anyone help me with this? I found the protocols Encoder and Decoder,…
Banana
  • 4,010
  • 9
  • 33
  • 49
62
votes
5 answers

Encode/Decode Array of Types conforming to protocol with JSONEncoder

I'm trying to find the best way to Encode/Decode an array of structs conforming to a swift protocol using the new JSONDecoder/Encoder in Swift 4. I made up a little example to illustrate the problem: First we have a protocol Tag and some Types that…
goesta
  • 649
  • 1
  • 6
  • 13
1
2 3
99 100