Using Codable on a dynamic type/object

Since you linked to my answer to another question, I will expand that one to answer yours. Truth is, all keys are known at runtime if you know where to look: struct GenericCodingKeys: CodingKey { var intValue: Int? var stringValue: String init?(intValue: Int) { self.intValue = intValue; self.stringValue = “\(intValue)” } init?(stringValue: String) { self.stringValue … Read more

Swift String escaping when serializing to JSON using Codable

For iOS 13+ / macOS 10.15+ You can use .withoutEscapingSlashes option to json decoder to avoid escaping slashes let user = User(username: “John”, profileURL: “http://google.com”) let jsonEncoder = JSONEncoder() jsonEncoder.outputFormatting = .withoutEscapingSlashes let json = try? jsonEncoder.encode(user) if let data = json, let str = String(data: data, encoding: .utf8) { print(str) } Console O/P {“profileURL”:”http://google.com“,”username”:”John”} … Read more

Swift 4 Codable – Bool or String values

Since you can’t always be in control of the APIs you’re working with, here’s one simple way to solve this with Codable directly, by overriding init(from:): struct Product : Decodable { // Properties in Swift are camelCased. You can provide the key separately from the property. var manageStock: Bool? private enum CodingKeys : String, CodingKey … Read more

Swift 4 Decodable – Dictionary with enum as key

The problem is that Dictionary‘s Codable conformance can currently only properly handle String and Int keys. For a dictionary with any other Key type (where that Key is Encodable/Decodable), it is encoded and decoded with an unkeyed container (JSON array) with alternating key values. Therefore when attempting to decode the JSON: {“dictionary”: {“enumValue”: “someString”}} into … Read more

How to exclude properties from Swift Codable?

The list of keys to encode/decode is controlled by a type called CodingKeys (note the s at the end). The compiler can synthesize this for you but can always override that. Let’s say you want to exclude the property nickname from both encoding and decoding: struct Person: Codable { var firstName: String var lastName: String … Read more

Swift Codable with dynamic keys

Assuming you left out the { and } that would surround this block and are required for this to be valid JSON, the following is the simplest solution to getting things parsed, you really don’t need to deal with CodingKey at all since your names match the keys in the JSON, so the synthesized CodingKey … Read more

Encode/Decode Array of Types conforming to protocol with JSONEncoder

The reason why your first example doesn’t compile (and your second crashes) is because protocols don’t conform to themselves – Tag is not a type that conforms to Codable, therefore neither is [Tag]. Therefore Article doesn’t get an auto-generated Codable conformance, as not all of its properties conform to Codable. Encoding and decoding only the … Read more

Swift 4 JSON Codable – value returned is sometimes an object, others an array

You might encapsulate the ambiguity of the result using an Enum with Associated Values (String and Array in this case), for example: enum MetadataType: Codable { case array([String]) case string(String) init(from decoder: Decoder) throws { let container = try decoder.singleValueContainer() do { self = try .array(container.decode(Array.self)) } catch DecodingError.typeMismatch { do { self = try … Read more