A Swift library to serialize Codable to and from [String: DictionaryValue].
struct User {
let id: Int
let name: String
let age: Int?
}
let encoder = DictionaryEncoder()
let user0 = User(id: 0, name: "sheat")
let dictionary0 = try! encoder.encode(user0) as! [String: DictionaryValue]
// -> ["id": 0, "name": "sheat"]
let user1 = User(id: 1, name: "sheat", age: 21)
let dictionary1 = try! encoder.encode(user1) as! [String: DictionaryValue]
// -> ["id": 1, "name": "sheat", age: 21]
let decoder = DictionaryDecoder()
let _user0 = try! decoder.decode(User.self, from: dictionary0)
// -> User(id: 0, name: "sheat", age: nil)
let _user1 = try! decoder.decode(User.self, from: dictionary1)
// -> User(id: 1, name: "sheat", age: 21)This protocol applies to
BoolIntFloatDoubleDecimalStringArray(Element == DictionaryValue?)Dictionary(Key == String, Value == DictionaryValue?)
- Integers are stored as
Int. All fixed-width integer types (Int8…UInt64) are accepted as long as the value fits inInt. A value outside that range (for exampleUInt64.max) throwsEncodingError.invalidValuerather than crashing. Floatis stored asFloat(not widened toDouble), so it round-trips with its own precision.Doubleis stored asDouble.- Key strategies match
JSONEncoder/JSONDecoderexactly, including the well-known asymmetry for acronyms:lastURLencodes tolast_url, which decodes back tolastUrl.
The following options are available and can be used in the same way as JSONEncoder / JSONDecoder.
- DateEncodingStrategy / DateDecodingStrategy
- DataEncodingStrategy / DataDecodingStrategy
- KeyEncodingStrategy / KeyDecodingStrategy