Swift的MsgPack序列化和反序列化器

网友投稿 1106 2022-11-01

Swift的MsgPack序列化和反序列化器

Swift的MsgPack序列化和反序列化器

MsgPack for Swift... Its' like JSON but faster Made with ♥ in pure Swift, no dependencies, lightweight & fully portable

★★ Star our github repository to help us! ★★

Created by Daniele Margutti (@danielemargutti)

MessagePack is an efficient binary serialization format, which lets you exchange data among multiple languages like JSON, except that it's faster and smaller. Small integers are encoded into a single byte while typical short strings require only one extra byte in addition to the strings themselves. You can read more about specs directly from the main web site.

OTHER LIBRARIES YOU MAY LIKE

I'm also working on several other projects you may like. Take a look below:

LibraryDescription
SwiftDateThe best way to manage date/timezones in Swift
HydraWrite better async code: async/await & promises
FlowA new declarative approach to table managment. Forget datasource & delegates.
SwiftRichStringElegant & Painless NSAttributedString in Swift
SwiftLocationEfficient location manager
SwiftMsgPackFast/efficient msgPack encoder/decoder

Index

Current ReleaseHow to useSupported TypesInstallation (CocoaPods, SwiftPM and Carthage)TestsRequirementsCredits

Current Release

Swift 4.x: Latest is 1.0.0 Download here.Swift 3.x: Last is 0.9.1 Download here.

A complete list of changes for each release is available in the CHANGELOG file.

How to use: one-shot pack & unpack

Both serialization and deseralization happens inside a Data object.

In order to pack one or more objects you need to create an empty Data instance and call pack by passing the object(s) you want to serialize. It's pretty easy, take a look here:

var data = Data()do { let obj1 = "Hello World" let obj2 = 45.5 let obj3: [AnyHashable:Any?] = [ "key_1" : "value test","key_2" : 4,"key_3" : true, otherHashableKey: "value1"] // Now you can pack your instances by passing them to pack function try data.pack(obj1,obj2,obj3)} catch { print("Something went wrong while packing data: \(error)") }

Deserializing data is pretty simple too, just call unpack function to an instance of Data with msgpack data:

let data: Data = // msgpack data...do { let decodedObj: Any? = try data.unpack()} catch { print("Something went wrong while unpacking data: \(error)") }

Supported Types

SwiftMsgPack supports the following Swift types:

StringDataBoolnilNumeric values: Int & UInt (UInt8,Int8,UInt16,Int16,UInt32,Int32,UInt64,Int64), Float and DoubleDictionaries ([AnyHashable:Any?])Array ([Any?])

The following limitations are specified by MsgPack specs format:

a value of an Integer object is limited from -(2^63) upto (2^64)-1maximum length of a Binary object is (2^32)-1maximum byte size of a String object is (2^32)-1String objects may contain invalid byte sequence and the behavior of a deserializer depends on the actual implementation when it received invalid byte sequence Deserializers should provide functionality to get the original byte array so that applications can decide how to handle the object maximum number of elements of an Array object is (2^32)-1maximum number of key-value associations of a Map object is (2^32)-1

Installation

You can install Swiftline using CocoaPods, carthage and Swift package manager

CocoaPods

use_frameworks!pod 'SwiftMsgPack'

Carthage

github 'malcommac/SwiftMsgPack'

Swift Package Manager

Add swiftline as dependency in your Package.swift

import PackageDescription let package = Package(name: "YourPackage", dependencies: [ .Package(url: "https://github.com/malcommac/SwiftMsgPack.git", majorVersion: 0), ] )

Tests

SwiftMsgPack has an extensive coverage using XCTest. You can found a complete list of tests inside Tests/SwiftMsgPackTests folder. Tests can also be runned with XCode using the SwiftMsgPack project.

Requirements

Current version is compatible with:

Swift 4.x+ (Swift 3.x up to 0.9.1)iOS 9.0 or latertvOS 9.0 or latermacOS 10.10 or laterLinux compatible environments

Credits & License

SwiftMsgPack is owned and maintained by Daniele Margutti.

As open source creation any help is welcome!

The code of this library is licensed under MIT License; you can use it in commercial products without any limitation.

The only requirement is to add a line in your Credits/About section with the text below:

This software uses open source SwiftMsgPack's library to manage MsgPack data.Web: http://github.com/malcommac/SwiftMsgPack.Created by Daniele Margutti and licensed under MIT License.

msgpack.org[SwiftMsgPack]

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:iijs 一个基于nodejs+koa2构建的简单轻量级MVC框架
下一篇:springMvc全局异常的实现
相关文章

 发表评论

暂时没有评论,来抢沙发吧~