一个轻量级 JSON 映射器。
受到一些 JSON 库的启发,例如:
根据 JSONShootout 的测试,我们测试了 json 映射速度,并且我们的性能与 Marshal 相当。
struct Sample:JSONDecodable {
let temp:Int
static func decode(_ dic: JSON) throws -> Sample {
return try Sample(temp: dic <| "temp")
}
}
let json = JSON(any: ["temp":1234])
let sample:Sample? = try? Sample.decode(json)
PrimitiveType 专注于类型转换和从字符串转换
return self as? T
// return 1 as? Int
// return "1" as? Int
String -> T
)return Int("1") // "1" -> 1
return Int("true") // "true" -> true
Int Int8 Int16 Int32 Int64
UInt UInt8 UInt16 UInt32 UInt64
Float Double
Bool
String
JSONDecodable 是协议,用于将 JSON
映射到您的自定义结构体。
操作符 | 解码元素 |
---|---|
<|? |
T? |
<| |
T |
<|| |
[T] |