Last active
September 24, 2015 21:39
-
-
Save yusuke024/4da504393a5fef2d147e to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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