JSON encoding and decoding with Codable

6373
0

Codable protocol combines Encodable and Decodable for seamless JSON conversion. Swift structs and classes conforming to Codable automatically synthesize encoding/decoding logic when all properties are Codable. JSONEncoder converts Swift types to JSON data, while JSONDecoder parses JSON into Swift types. I customize key mapping with CodingKeys enum to match API snake_case or different field names. For complex transformations, I implement custom init(from decoder:) and encode(to encoder:) methods. Date strategies handle various date formats. The keyDecodingStrategy and keyEncodingStrategy automate case conversion. Codable works with property lists, UserDefaults, and file storage too.