JSONDecodeKit
一个轻量级的 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)
协议
原始类型
原始类型关注类型转换和从字符串转换
- 类型转换
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
JSONDecodable是一个协议,将JSON
映射到自定义的结构体。
操作符
操作符 | 解码元素 |
---|---|
<|? |
T? |
<| |
T |
<|| |
[T] |