Last active
December 27, 2016 10:54
-
-
Save kenmaz/24e4d436d537bb3ed8dca461c35aa298 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
protocol JSONSerializable { | |
func serializable() -> AnyObject | |
} | |
extension JSONSerializable { | |
func serializable() -> AnyObject { | |
return self as AnyObject | |
} | |
} | |
extension Int:JSONSerializable {} | |
extension String:JSONSerializable {} | |
struct JSONArray:JSONSerializable { | |
let array:[JSONSerializable] | |
func serializable() -> AnyObject { | |
return array.map{ $0.serializable() } as AnyObject | |
} | |
} | |
struct JSONDictionary:JSONSerializable { | |
let dictionary:[String:JSONSerializable] | |
func serializable() -> AnyObject { | |
var res:[String:AnyObject] = [:] | |
dictionary.forEach { (k,v) in | |
res[k] = v.serializable() | |
} | |
return res as AnyObject | |
} | |
} | |
//example | |
let params = JSONDictionary( | |
dictionary:[ | |
"string" : "foo", | |
"number:": 1, | |
"array": JSONArray( | |
array: [ | |
1, | |
2, | |
3 | |
] | |
), | |
//"date": Date() // error | |
] | |
) | |
let res = params.serializable() | |
let json = try! JSONSerialization.data(withJSONObject: res, options: []) | |
print(String(data: json, encoding: .utf8)!) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment