Skip to content

Instantly share code, notes, and snippets.

@yusuke024
Last active September 24, 2015 21:39
Show Gist options
  • Save yusuke024/4da504393a5fef2d147e to your computer and use it in GitHub Desktop.
Save yusuke024/4da504393a5fef2d147e to your computer and use it in GitHub Desktop.
enum JSON {
case StringType(String)
enum Number {
case IntType(Int)
case FloatType(Float)
}
case NumberType(Number)
case BoolType(Bool)
indirect case ObjectType([String: JSON])
indirect case ArrayType([JSON])
case Null
var int: Int? {
guard case .NumberType(let num) = self else {
return nil
}
guard case .IntType(let val) = num else {
return nil
}
return val
}
var string: String? {
guard case .StringType(let val) = self else {
return nil
}
return val
}
}
extension JSON: StringLiteralConvertible {
typealias ExtendedGraphemeClusterLiteralType = StringLiteralType
typealias UnicodeScalarLiteralType = StringLiteralType
init(unicodeScalarLiteral value: UnicodeScalarLiteralType) {
self = .StringType("\(value)")
}
init(extendedGraphemeClusterLiteral value: ExtendedGraphemeClusterLiteralType) {
self = .StringType(value)
}
init(stringLiteral value: StringLiteralType) {
self = .StringType(value)
}
}
let j: JSON = "HELLO"
j.string
let a = JSON.NumberType(.IntType(5))
let i = a.int
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment