Skip to content

Instantly share code, notes, and snippets.

@khanlou
Created December 16, 2016 01:45
Show Gist options
  • Select an option

  • Save khanlou/e03e86abba9616d31da2041544a384ff to your computer and use it in GitHub Desktop.

Select an option

Save khanlou/e03e86abba9616d31da2041544a384ff to your computer and use it in GitHub Desktop.
protocol JSONValueConvertible {
var jsonValue: JSONValue { get }
}
protocol JSONValue: JSONValueConvertible { }
extension JSONValue {
var jsonValue: JSONValue {
return self
}
}
extension String: JSONValue { }
extension Int: JSONValue { }
extension Double: JSONValue { }
extension Bool: JSONValue { }
extension Array: JSONValue { }
extension Array where Element: JSONValueConvertible {
var jsonValue: JSONValue {
return self.map({ $0.jsonValue })
}
}
struct Convertible: JSONValueConvertible {
let string: String
var jsonValue: JSONValue {
return string
}
}
let a = [Convertible(string: "bing")]
let b = a.jsonValue
print(a)
print(b)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment