Enum & custom type from primitive JSON type

Learn two techniques in Codable

Today we are going to learn how two cases that you might need to do when dealing with Codable. Parsing Enum and Parsing custom type from a primitive type like string.

Enum

If your enum has a raw value that can be represented as Int or String (which I think that should cover most cases). We can make enum Codable by assigning a raw value to it.

Let’s say you have JSON object like this:

{"name": "iOS developer", "status": "open"}

You can create Swift struct like this:

struct Job: Codable {
  enum Status: String, Codable {
    case open
    case close
  }
  let name: String
  let status: Status
}

If you have enum which isn’t String or Int representable you can still make it conform to Codable as long as that raw value is Codable.

Custom type from a primitive type

If you have a custom type that can be derived from basic JSON type, for example, you have an object with image sending as base64 encoded string:

{
  "name": "XXX",
  "image": "data:image/png;base64,iVBORw0KGgo......."
}


Since UIImage doesn’t conform to Codable and we can’t conform it with an extension, we have 2 ways to handle this:

  1. Create another wrapper class around this image.
struct Base64ImageWrapper: Codable {
    let image: UIImage
    
    public init(from decoder: Decoder) throws {
        let container = try decoder.singleValueContainer()        
        let base64String = try container.decode(String.self)        
        let components = base64String.split(separator: ",")        
        if components.count != 2 {
            throw DecodingError.dataCorruptedError(in: container, debugDescription: "Wrong data format")
        }
        
        let dataString = String(components[1])
        
        if let dataDecoded = Data(base64Encoded: dataString, options: .ignoreUnknownCharacters),
            let image = UIImage(data: dataDecoded) {
            self.image = image
        } else {
            throw DecodingError.dataCorruptedError(in: container, debugDescription: "Can't initialize image from data string")
        }
    }
    
    public func encode(to encoder: Encoder) throws {
        var container = encoder.singleValueContainer()
        let data = image.jpegData(compressionQuality: 1)
        let prefix = "data:image/jpeg;base64,"
        guard let base64String = data?.base64EncodedString(options: .lineLength64Characters) else {
            throw EncodingError.invalidValue(image, EncodingError.Context(codingPath: [], debugDescription: "Can't encode image to base64 string."))
        }
        
        try container.encode(prefix + base64String)
    }
}

The code above should be familiar to you with one key take away. Instead of:

let values = try decoder.container(keyedBy: CodingKeys.self)

We use:

let container = try decoder.singleValueContainer()


since we are dealing with one primitive value, base64 string, in this case. Then we can use this wrapper like this:

struct ModelUsingImageWrapper: Codable {
  let name: String
  let image: Base64ImageWrapper
}


2. Add custom encode/decode for UIImage in KeyedEncodingContainer:

extension KeyedEncodingContainer {
    
    mutating func encode(_ value: UIImage,
                         forKey key: KeyedEncodingContainer.Key) throws {
        
        let imageData = value.jpegData(compressionQuality: 1)
        let prefix = "data:image/jpeg;base64,"        
        
        guard let base64String = imageData?.base64EncodedString(options: .lineLength64Characters) else {
            throw EncodingError.invalidValue(value, EncodingError.Context(codingPath: codingPath, debugDescription: "Can't encode image to base64 string."))
        }
        
        try encode(prefix + base64String, forKey: key)
    }
    
}

extension KeyedDecodingContainer {
    
    public func decode(_ type: UIImage.Type, forKey key: KeyedDecodingContainer.Key) throws -> UIImage {
        let base64String = try decode(String.self, forKey: key)
        
        // data:image/svg+xml;base64,PD.....
        let components = base64String.split(separator: ",")
    
        if components.count != 2 {
            throw DecodingError.typeMismatch(type, DecodingError.Context(codingPath: codingPath, debugDescription: "Unsupported format"))
        }
        
        let dataString = String(components[1])
        if let dataDecoded = Data(base64Encoded: dataString, options: .ignoreUnknownCharacters), let image = UIImage(data: dataDecoded) {
            return image
        } else {
            throw DecodingError.typeMismatch(type, DecodingError.Context(codingPath: codingPath, debugDescription: "Unsupported format"))
        }
    }
    
}

And use this with explicit encode and decode.

struct ModelUsingKeyedEncodingContainer: Codable {
  let name: String
  let image: UIImage  enum CodingKeys: String, CodingKey {
    case name
    case image
  }
  public init(from decoder: Decoder) throws {
    let container = try decoder.container(keyedBy: CodingKeys.self)
    name = try container.decode(String.self, forKey: .name)
    image = try container.decode(UIImage.self, forKey: .image)
  }
  public func encode(to encoder: Encoder) throws {
    var container = encoder.container(keyedBy: CodingKeys.self)
    try container.encode(name, forKey: .name)
    try container.encode(image, forKey: .image)
  }
}

These 2 approaches should suffice for most of your use cases.
Like 174 likes
Art Wongpatcharapakorn
iOS Developer at Oozou
Share:

Join the conversation

This will be shown public
All comments are moderated

Get our stories delivered

From us to your inbox weekly.