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

106,731

Solution 1

If you don't mind a bit of shifting of data around you could use something like this:

extension Encodable {
  func asDictionary() throws -> [String: Any] {
    let data = try JSONEncoder().encode(self)
    guard let dictionary = try JSONSerialization.jsonObject(with: data, options: .allowFragments) as? [String: Any] else {
      throw NSError()
    }
    return dictionary
  }
}

Or an optional variant

extension Encodable {
  var dictionary: [String: Any]? {
    guard let data = try? JSONEncoder().encode(self) else { return nil }
    return (try? JSONSerialization.jsonObject(with: data, options: .allowFragments)).flatMap { $0 as? [String: Any] }
  }
}

Assuming Foo conforms to Codable or really Encodable then you can do this.

let struct = Foo(a: 1, b: 2)
let dict = try struct.asDictionary()
let optionalDict = struct.dictionary

If you want to go the other way(init(any)), take a look at this Init an object conforming to Codable with a dictionary/array

Solution 2

Here are simple implementations of DictionaryEncoder / DictionaryDecoder that wrap JSONEncoder, JSONDecoder and JSONSerialization, that also handle encoding / decoding strategies…

class DictionaryEncoder {

    private let encoder = JSONEncoder()

    var dateEncodingStrategy: JSONEncoder.DateEncodingStrategy {
        set { encoder.dateEncodingStrategy = newValue }
        get { return encoder.dateEncodingStrategy }
    }

    var dataEncodingStrategy: JSONEncoder.DataEncodingStrategy {
        set { encoder.dataEncodingStrategy = newValue }
        get { return encoder.dataEncodingStrategy }
    }

    var nonConformingFloatEncodingStrategy: JSONEncoder.NonConformingFloatEncodingStrategy {
        set { encoder.nonConformingFloatEncodingStrategy = newValue }
        get { return encoder.nonConformingFloatEncodingStrategy }
    }

    var keyEncodingStrategy: JSONEncoder.KeyEncodingStrategy {
        set { encoder.keyEncodingStrategy = newValue }
        get { return encoder.keyEncodingStrategy }
    }

    func encode<T>(_ value: T) throws -> [String: Any] where T : Encodable {
        let data = try encoder.encode(value)
        return try JSONSerialization.jsonObject(with: data, options: .allowFragments) as! [String: Any]
    }
}

class DictionaryDecoder {

    private let decoder = JSONDecoder()

    var dateDecodingStrategy: JSONDecoder.DateDecodingStrategy {
        set { decoder.dateDecodingStrategy = newValue }
        get { return decoder.dateDecodingStrategy }
    }

    var dataDecodingStrategy: JSONDecoder.DataDecodingStrategy {
        set { decoder.dataDecodingStrategy = newValue }
        get { return decoder.dataDecodingStrategy }
    }

    var nonConformingFloatDecodingStrategy: JSONDecoder.NonConformingFloatDecodingStrategy {
        set { decoder.nonConformingFloatDecodingStrategy = newValue }
        get { return decoder.nonConformingFloatDecodingStrategy }
    }

    var keyDecodingStrategy: JSONDecoder.KeyDecodingStrategy {
        set { decoder.keyDecodingStrategy = newValue }
        get { return decoder.keyDecodingStrategy }
    }

    func decode<T>(_ type: T.Type, from dictionary: [String: Any]) throws -> T where T : Decodable {
        let data = try JSONSerialization.data(withJSONObject: dictionary, options: [])
        return try decoder.decode(type, from: data)
    }
}

Usage is similar to JSONEncoder / JSONDecoder

let dictionary = try DictionaryEncoder().encode(object)

and

let object = try DictionaryDecoder().decode(Object.self, from: dictionary)

For convenience, I've put this all in a repo… https://github.com/ashleymills/SwiftDictionaryCoding

Solution 3

I have create a library called CodableFirebase and it's initial purpose was to use it with Firebase Database, but it does actually what you need: it creates a dictionary or any other type just like in JSONDecoder but you don't need to do the double conversion here like you do in other answers. So it would look something like:

import CodableFirebase

let model = Foo(a: 1, b: 2)
let dict = try! FirebaseEncoder().encode(model)

Solution 4

There is no built in way to do that. As answered above if you have no performance issues then you can accept the JSONEncoder + JSONSerialization implementation.

But I would rather go the standard library's way to provide an encoder/decoder object.

class DictionaryEncoder {
    private let jsonEncoder = JSONEncoder()

    /// Encodes given Encodable value into an array or dictionary
    func encode<T>(_ value: T) throws -> Any where T: Encodable {
        let jsonData = try jsonEncoder.encode(value)
        return try JSONSerialization.jsonObject(with: jsonData, options: .allowFragments)
    }
}

class DictionaryDecoder {
    private let jsonDecoder = JSONDecoder()

    /// Decodes given Decodable type from given array or dictionary
    func decode<T>(_ type: T.Type, from json: Any) throws -> T where T: Decodable {
        let jsonData = try JSONSerialization.data(withJSONObject: json, options: [])
        return try jsonDecoder.decode(type, from: jsonData)
    }
}

You can try it with following code:

struct Computer: Codable {
    var owner: String?
    var cpuCores: Int
    var ram: Double
}

let computer = Computer(owner: "5keeve", cpuCores: 8, ram: 4)
let dictionary = try! DictionaryEncoder().encode(computer)
let decodedComputer = try! DictionaryDecoder().decode(Computer.self, from: dictionary)

I am force-trying here to make the example shorter. In production code you should handle the errors appropriately.

Solution 5

I'm not sure if it's the best way but you definitely can do something like:

struct Foo: Codable {
    var a: Int
    var b: Int

    init(a: Int, b: Int) {
        self.a = a
        self.b = b
    }
}

let foo = Foo(a: 1, b: 2)
let dict = try JSONDecoder().decode([String: Int].self, from: JSONEncoder().encode(foo))
print(dict)
Share:
106,731
zoul
Author by

zoul

Updated on September 12, 2020

Comments

  • zoul
    zoul almost 4 years

    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]
    
  • Leo Dabus
    Leo Dabus over 6 years
    This would only work for structures with all properties of the same kind
  • Am1rFT
    Am1rFT almost 6 years
    I just tried " let dict = try JSONDecoder().decode([String: Int].self, from: JSONEncoder().encode(foo)) " and I got "Expected to decode Dictionary<String, Any> but found an array instead." could u help pls
  • user1046037
    user1046037 over 5 years
    Thanks a lot !, the alternate would be to use inheritance but the calling site wouldn't be able to infer the type as a dictionary as there would be 2 functions of different return types.
  • zoul
    zoul over 5 years
    I have to admit I still don’t understand why this is downvoted :–) Is the caveat not true? Or the framework not useful?
  • Simon Moshenko
    Simon Moshenko over 4 years
    Your example does not show how to solve the problem
  • Iron John Bonney
    Iron John Bonney almost 4 years
    The optional var implementation is great, clean, swifty, and perfect for guard let statements. Really cleans up API calls.
  • DawnSong
    DawnSong over 3 years
    Coding into data then decoding from data, when decoding a big chunk data, the punishment on performance must be obvious.
  • Pranav Kasetti
    Pranav Kasetti over 2 years
    Way better than the accepted answer. +1
  • Lachtan
    Lachtan over 2 years
    OP is asking about the other way arount: Codable -> Dictionary