MessagePack for Swift
Swift 的 MessagePack 实现。
打包
protocol Packable {
func packToBytes() -> Bytes
func pack() -> Data
}可打包的 stdlib 类型
Bool,Double,Int,UInt,Int64,UInt64包装类型 Optional,其中 Wrapped : Packable数组,其中 Element : Packable字典,其中 Key: Hashable,Key: Packable,Value: Packable
可打包扩展类型
二进制扩展
如果您想要打包具有混合类型的数据,请使用 ValueBox。
解包
struct Unpacker {
static func unpack(bytes: Bytes) -> ValueBox?
}从数据中解包,为 Unpacker.unpack(bytes:) 提供方便
extension Data {
func unpack() -> ValueBox?
}可解包的 stdlib 类型包含
BoolDoubleFloatIntInt64StringUIntUInt64
对于其他混合类型,您始终可以使用 ValueBox。
enum ValueBox {
case array([ValueBox])
case binary(Binary)
case bool(Bool)
case dictionary([ValueBox: ValueBox])
case double(Double)
case `extension`(Extension)
case float(Float)
case int64(Int64)
case `nil`
case string(String)
case uint64(UInt64)
}