111

GitHub - Flight-School/AnyCodable: Type-erased wrappers for Encodable, Decodable...

 5 years ago
source link: https://github.com/Flight-School/AnyCodable
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

README.md

AnyCodable

Type-erased wrappers for Encodable, Decodable, and Codable values.

This functionality is discussed in Chapter 3 of Flight School Guide to Swift Codable.

AnyEncodable

import AnyCodable

let dictionary: [String: AnyEncodable] = [
    "boolean": true,
    "integer": 1,
    "double": 3.14159265358979323846,
    "string": "string",
    "array": [1, 2, 3],
    "nested": [
        "a": "alpha",
        "b": "bravo",
        "c": "charlie"
    ]
]

let encoder = JSONEncoder()
let json = try! encoder.encode(dictionary)

AnyDecodable

let json = """
     {
         "boolean": true,
         "integer": 1,
         "double": 3.14159265358979323846,
         "string": "string",
         "array": [1, 2, 3],
         "nested": {
             "a": "alpha",
             "b": "bravo",
             "c": "charlie"
         }
     }
""".data(using: .utf8)!

let decoder = JSONDecoder()
let dictionary = try! decoder.decode([String: AnyCodable].self, from: json)

AnyCodable

AnyCodable can be used to wrap values for encoding and decoding.

License

MIT

Contact

Mattt (@mattt)


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK