Advertisement
Guest User

Untitled

a guest
Oct 27th, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.85 KB | None | 0 0
  1. public protocol Serializable {
  2. func dictionary() -> [String: Any]
  3. }
  4.  
  5. extension Serializable {
  6. func dictionary() -> [String: Any] {
  7. var result = [String: Any]()
  8. let mirror = Mirror(reflecting: self)
  9.  
  10. for child in mirror.children {
  11. guard let label = child.label else { continue }
  12.  
  13. switch child.value {
  14. case let serializable as Serializable:
  15. result[label] = serializable.dictionary()
  16.  
  17. case let rawRepresentable as RawRepresentable:
  18. result[label] = rawRepresentable.getValueAsAny()
  19.  
  20. default:
  21. result[label] = child.value
  22. }
  23. }
  24.  
  25. return result
  26. }
  27. }
  28.  
  29. extension RawRepresentable {
  30. public func getValueAsAny() -> Any {
  31. return rawValue
  32. }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement