Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- protocol JSONRepresentation{
- func to_json() -> String
- }
- extension JSONRepresentation{
- func to_json() -> String
- {
- let reflection = Mirror(reflecting: self)
- let root = String(describing: reflection.subjectType).lowercased()
- var children :[String:Any] = [:]
- reflection.children.forEach { (child) in
- children[child.label!] = child.value
- }
- let hash = [
- root:children
- ]
- let jsonData = try! JSONSerialization.data(withJSONObject: hash, options: .prettyPrinted)
- let json = String(bytes: jsonData, encoding: .utf8)!
- return json
- }
- }
Add Comment
Please, Sign In to add comment