Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public protocol Serializable {
- func dictionary() -> [String: Any]
- }
- extension Serializable {
- func dictionary() -> [String: Any] {
- var result = [String: Any]()
- let mirror = Mirror(reflecting: self)
- for child in mirror.children {
- guard let label = child.label else { continue }
- switch child.value {
- case let serializable as Serializable:
- result[label] = serializable.dictionary()
- case let rawRepresentable as RawRepresentable:
- result[label] = rawRepresentable.rawValue
- default:
- result[label] = child.value
- }
- }
- return result
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement