Giter Site home page Giter Site logo

a2 / messagepack.swift Goto Github PK

View Code? Open in Web Editor NEW
283.0 11.0 60.0 207 KB

It's like JSON, but fast and small…and Swift! – msgpack.org[Swift]

Home Page: http://msgpack.org

License: MIT License

Ruby 0.72% Swift 99.02% Objective-C 0.27%
messagepack swift

messagepack.swift's Introduction

MessagePack.swift

Version License Platform

A fast, zero-dependency MessagePack implementation written in Swift 4. Supports Apple platforms and Linux.

Installation

CocoaPods

To use CocoaPods, add the following to your Podfile:

pod 'MessagePack.swift', '~> 4.0'

Carthage

To use Carthage, add the following to your Cartfile:

github "a2/MessagePack.swift" ~> 4.0

SPM (Swift Package Manager)

You can easily integrate MessagePack.swift in your app with SPM. Just add MessagePack.swift as a dependency:

import PackageDescription

let package = Package(
    name: "MyAwesomeApp",
    dependencies: [
        .Package(url: "https://github.com/a2/MessagePack.swift.git", majorVersion: 4),
    ]
)

Version

  • Versions 4.x support Swift 5.2.
  • Versions 3.x support Swift 4.
  • Support for Swift 3 was dropped after 2.1.1.
  • Support for Swift 2 was dropped after 1.2.0.

Authors

Alexsander Akers, [email protected]

License

MessagePack.swift is available under the MIT license. See the LICENSE file for more info.

messagepack.swift's People

Contributors

a2 avatar brettthepark avatar cherrywoods avatar christiansteffens avatar chunkerchunker avatar jarrodmoldrich avatar jimhh avatar joshchngs avatar litso avatar mgadda avatar monyschuk avatar narsail avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

messagepack.swift's Issues

Swift4 Encoder/Decoder

Hey Alexsander,

so I recently started adding the Codable protocol conformance to my KDTree framework.

After I finished the conformance to Encodable/Decodable and tested it with Apples JSONEncoder and PropertyListEncoder I noticed that decoding a tree from file took about 3 times as long as just loading my plain data from a csv AND building the tree again.
I know it's because the Apple Encoders use NSNumber and NSString internally which makes them slow. But I'm having a hard time accepting that loading the tree structure from a file is slower then running through all the sorting required to create a new tree.

So, my question to you, are you planning to update MessagePack.swift so that it will Encode/Decode Codable swift files?
I feel with Swift4 there will be a big need for faster alternatives to the JSONEncoder and PropertyListEncoder Apple provides :)

Thank you!

Buffer pointer instead of uint8 array generator

First, you have a great packer.
I have only one idea or suggestion.
Did you think about using the UnsafePointer < UInt8 > instead of [Uint8]?
It would be faster for unpacking the strings because you can advance pointer for n places instead of using generator that is always advancing for one next byte.

Check this performance improvements that are used in swift for unpacking bytes packed with similar flatbuffers protocol:
https://medium.com/@icex33/10-thousand-times-faster-swift-737b1accd973#.x9281mi2x

Integer overflow build Errors in XCode 9 Beta 5

In Pack.swift: packPositiveInteger and Pack.swift: packwe get the following errors when comparing either UInt64 or UInt32 values to 0xffff_ffff

Integer literal '4294967295' overflows when stored into 'Int'

and

Integer literal '4294967295' overflows when stored into signed 'Builtin.Int32'

MessagePack.swift hello world

This is working 'hello world' type code from python that follows message pack conventions, what is the equivalent in this library?

exampleMessage = {
"login": {
"email": email,
"password": password
}
}

MESSAGE = msgpack.packb(exampleMessage, use_bin_type=True)

Unpacking of "Umlaute" doesn't work

Characters like ä,ö,ü,é,ß doesn't unpack correctly. Packing works, but the problem with unpacking is, that these characters consist of 2 bytes instead of 1. So the unpackString method handles the character as 2 characters.

Also the determination of the string length looks wrong. The length should be "value & 0x1f" and not "value - 0xa0" if I understand it correctly.

Pack Dictionary

Can you please provide me with an example how to pack a Dictionary<String, AnyObject> to msg-pack?

Ordered dictionary

The pack is using a dictionary [MessagpackValue: MessagepackValue] so that when unpacking that value, it is not ordered like the original input value

Any -> MessagePackValue conversion

I am adding MsgPack support to SWAMP. The SWAMP serializer protocol operates on an array of [Any], so I had to write code to recursively convert each item to the corresponding MessagePackValue variant.

Do you have any interest in integrating this code, either as a standalone function, a ConvenienceInitializer, or an overload of pack() ?

Heck it's a pretty small amount of code.. here it is if you don't want it in case someone else finds it useful:

    open func anyToMPValue(anyVal : Any) -> MessagePackValue? {

        switch(anyVal) {

        case is Bool:
            print("Bool")
            return MessagePackValue(anyVal as! Bool)

        case is Int: // will not handle Int8, Int16, Int32, Int64
            print("Int")
            return MessagePackValue(anyVal as! Int)

        case is UInt: // does not handle UInt8, UInt16, UInt32, UInt64
            print("UInt")
            return MessagePackValue(anyVal as! UInt)

        case is Float:
            print("Float")
            return MessagePackValue(anyVal as! Float)

        case is Double:
            print("Double")
            return MessagePackValue(anyVal as! Double)

        case is String:
            print("String")
            return MessagePackValue(anyVal as! String)

        case is Array<Any>:
            print("Array")
            var mpArray = Array<MessagePackValue>()

            let arrayVal = anyVal as! Array<Any>
            for value in arrayVal {
                if let mpValue = anyToMPValue(anyVal: value) {
                    mpArray.append(mpValue)
                } else {
                    print("failed to convert")
                    print(value)
                }
            }
            return MessagePackValue(mpArray)

        case is Dictionary<String, Any>:
            print("Dictionary")
            var mpDict = [MessagePackValue : MessagePackValue]()

            let dictVal = anyVal as! Dictionary<String, Any>
            for (key,value) in dictVal {
                let mpKey = MessagePackValue(key as! String)
                if let mpValue = anyToMPValue(anyVal: value) {
                    mpDict[mpKey] = mpValue
                } else {
                    print("failed to convert")
                    print(value)
                }
            }
            return MessagePackValue(mpDict)

        case is Data:
            print("Data")
            return MessagePackValue(anyVal as! Data)

        default:
            print("Unknown type")
            return nil;
        }
    }

How do I do if I want to pack small uint32 values?

Hi,

I'm trying implement MessagePack-RPC using this library. And I need to pack a value of uint32.

I have tryed,

let value = MessagePackValue(1 as UInt32)
let data = MessagePack.pack(value) //=> 0x01

But I got a fixnum instead of uint32.

How do I do if I want to pack small uint32 values?

Why .float and .double?

Why are there .float and .double cases in MessagePackValue?
Why does this framework not convert to the smallest possible representation here as it does with integers and as the msgpack specification suggests?

I just can't see a reason for this...
To me it seems as if this could be implemented very easily using Float(exactly:).

Termination key for Pack in Swift 4

IOS app is packing the data to send to Linux Device by this framework.

Linux Device is unpacking by mpack library.

When I used Message pack v1.2.0 for Swift 2, pack in IOS App and unpack in Linux Device are working.

After I upgrade the Message Pack v3.0 for Swift 4, Pack is not working fine.

  1. Termination key is not generated in Pack for Swift 4 and whereas that key is generating for Swift 2.
  2. One extra byte is adding to before string value after packing the data in Swift 2. This is not happening in Swift 4.
  3. Which means if we compared Swift 2 and Swift 4 Hexa values, one Byte difference is appearing in Pack. Because of this unpacking is not happening in Device side.

Example Data :-

I used same String in Array data for Message pack v1.2.0 and v3.0

Message pack v1.2.0

32 Bytes :-
String : abcdefghijklmnopqrstuvwxyzabcdef

Hexa : 91d92061 62636465 66676869 6a6b6c6d 6e6f7071 72737475 76777879 7a616263 646566

64 Bytes :-
String : abcdefghijklmnopqrstuvwxyzabcdefabcdefghijklmnopqrstuvwxyzabcdef
Hexa : 91d94061 62636465 66676869 6a6b6c6d 6e6f7071 72737475 76777879 7a616263 64656661 62636465 66676869 6a6b6c6d 6e6f7071 72737475 76777879 7a616263 646566

128 Bytes :-
String : abcdefghijklmnopqrstuvwxyzabcdefabcdefghijklmnopqrstuvwxyzabcdefabcdefghijklmnopqrstuvwxyzabcdefabcdefghijklmnopqrstuvwxyzabcdef
Hexa : 

91d98061 62636465 66676869 6a6b6c6d 6e6f7071 72737475 76777879 7a616263 64656661 62636465 66676869 6a6b6c6d 6e6f7071 72737475 76777879 7a616263 64656661 62636465 66676869 6a6b6c6d 6e6f7071 72737475 76777879 7a616263 64656661 62636465 66676869 6a6b6c6d 6e6f7071 72737475 76777879 7a616263 646566

Message pack v3.0

32 Bytes :-
String : abcdefghijklmnopqrstuvwxyzabcdef
Hexa : 916511d1 0380d103 800078aa 31313332 31323331 323391d9 61626364 65666768 696a6b6c 6d6e6f70 71727374 75767778 797a6162 63646566

64 Bytes :-
String : abcdefghijklmnopqrstuvwxyzabcdefabcdefghijklmnopqrstuvwxyzabcdef
Hexa : 91d96162 63646566 6768696a 6b6c6d6e 6f707172 73747576 7778797a 61626364 65666162 63646566 6768696a 6b6c6d6e 6f707172 73747576 7778797a 61626364 6566

128 Bytes :-
String : abcdefghijklmnopqrstuvwxyzabcdefabcdefghijklmnopqrstuvwxyzabcdefabcdefghijklmnopqrstuvwxyzabcdefabcdefghijklmnopqrstuvwxyzabcdef
Hexa : 91d96162 63646566 6768696a 6b6c6d6e 6f707172 73747576 7778797a 61626364 65666162 63646566 6768696a 6b6c6d6e 6f707172 73747576 7778797a 61626364 65666162 63646566 6768696a 6b6c6d6e 6f707172 73747576 7778797a 61626364 65666162 63646566 6768696a 6b6c6d6e 6f707172 73747576 7778797a 61626364 6566

Please provide the solution for this issue.

Thanks in Advance.

Swift 3.1 warnings

There are 4 warnings in swift 3.1 about the unsafeBitCast. Would you look at it?

Add WatchOS Support

Hello,

I really like what you have done here and I think that MessagePack.swift can be used with WatchOS 2.0 projects for communication using the WatchConnectivity Framework. Can you please introduce the watch OS target into the pod spec?

  • s.watchos.deployment_target = '2.0'

Example:
AliSoftware/OHHTTPStubs@9fb400f

Thank you!

Compilation fails on 32bit devices

Swift 3.0 support

Please add Swift 3.0 support (code doesn't compile at all).
Thanks

swift-tools-version in Package.swift

When trying to import using SPM, I get the following in Xcode 11.3.1:
The package dependency graph can not be resolved; unable find any available tag for the following requirements:
https://github.com/a2/MessagePack.swift — 3.0.1..<4.0.0

Using 11.4 beta 2, I get a different (likely more accurate) error:
because MessagePack.swift >=2.0.0 contains incompatible tools version and root depends on MessagePack.swift 3.0.1..<4.0.0, version solving failed.

Is this as simple as adding "// swift-tools-version:5.0" (or similar) to the Package.swift? If so, I'm happy to submit a PR. Otherwise if this relates to #70 or I'm just not doing something correctly, let me know?

Thanks

Xcode9 carthage MessagePack

*** Building scheme "MessagePack iOS" in MessagePack.xcodeproj
Build Failed
Task failed with exit code 65:
/usr/bin/xcrun xcodebuild -project /Users/fanrong/ios_DeviceManager/DeviceManager/Class/Anima/Carthage/Carthage/Checkouts/MessagePack.swift/MessagePack.xcodeproj -scheme MessagePack\ iOS -configuration Release -derivedDataPath /Users/fanrong/Library/Caches/org.carthage.CarthageKit/DerivedData/9.0_9A235/MessagePack.swift/2.1.0 -sdk iphoneos ONLY_ACTIVE_ARCH=NO BITCODE_GENERATION_MODE=bitcode CODE_SIGNING_REQUIRED=NO CODE_SIGN_IDENTITY= CARTHAGE=YES build (launched in /Users/fanrong/ios_DeviceManager/DeviceManager/Class/Anima/Carthage/Carthage/Checkouts/MessagePack.swift)

This usually indicates that project itself failed to compile. Please check the xcodebuild log for more details: /var/folders/qp/vvlmg9ps3zn45sx_hlvbb1_c0000gn/T/carthage-xcodebuild.IYnGn8.log

Unpacking fails with the error invalidArgument in Swift 3

Hi,

I'm encountering an issue unpacking a msgpack payload in Swift 3.

I'm getting a msgpack payload from an API and I'm using this library to unpack it. It used to work perfectly fine back in Swift 2 with the v1.2. I have created a sample payload as a byte array below.

do {
    let arr: [UInt8] = [133, 164, 99, 111, 100, 101, 162, 79, 75, 164, 100, 97, 116, 97, 132, 171, 97, 99, 99, 101, 115, 115, 84, 111, 107, 101, 110, 218, 1, 60, 101, 121, 74, 104, 98, 71, 99, 105, 79, 105, 74, 73, 85, 122, 73, 49, 78, 105, 73, 115, 73, 110, 82, 53, 99, 67, 73, 54, 73, 107, 112, 88, 86, 67, 74, 57, 46, 101, 121, 74, 106, 98, 71, 108, 108, 98, 110, 81, 105, 79, 105, 74, 109, 78, 50, 89, 121, 78, 84, 108, 107, 89, 83, 49, 104, 78, 122, 82, 108, 76, 84, 82, 109, 78, 50, 81, 116, 79, 87, 85, 52, 79, 67, 48, 50, 90, 68, 89, 52, 77, 122, 89, 120, 90, 106, 107, 51, 78, 84, 103, 105, 76, 67, 74, 119, 90, 88, 74, 122, 98, 50, 52, 105, 79, 105, 74, 105, 77, 84, 82, 119, 98, 110, 90, 48, 98, 109, 53, 122, 97, 106, 99, 120, 99, 122, 86, 122, 99, 50, 70, 104, 90, 121, 73, 115, 73, 109, 82, 108, 100, 109, 108, 106, 90, 83, 73, 54, 73, 107, 81, 53, 82, 68, 100, 71, 81, 122, 99, 49, 76, 84, 73, 52, 78, 85, 89, 116, 78, 69, 70, 68, 82, 83, 48, 53, 82, 84, 77, 53, 76, 84, 65, 120, 79, 84, 81, 122, 78, 106, 104, 66, 77, 106, 107, 122, 81, 121, 73, 115, 73, 110, 82, 53, 99, 71, 85, 105, 79, 106, 73, 115, 73, 109, 86, 52, 99, 67, 73, 54, 77, 84, 81, 52, 77, 84, 73, 50, 77, 106, 73, 51, 77, 83, 119, 105, 97, 88, 78, 122, 73, 106, 111, 105, 84, 87, 57, 114, 98, 121, 49, 84, 90, 88, 74, 50, 97, 87, 78, 108, 73, 110, 48, 46, 103, 80, 99, 81, 120, 68, 65, 76, 119, 85, 105, 117, 57, 95, 110, 79, 80, 97, 114, 65, 71, 104, 45, 106, 71, 106, 119, 88, 83, 84, 54, 107, 118, 74, 76, 87, 105, 98, 107, 107, 100, 75, 89, 169, 101, 120, 112, 105, 114, 101, 115, 65, 116, 206, 88, 74, 68, 191, 166, 112, 101, 114, 115, 111, 110, 136, 169, 102, 105, 114, 115, 116, 78, 97, 109, 101, 165, 73, 115, 117, 114, 117, 166, 104, 97, 110, 100, 108, 101, 160, 162, 105, 100, 180, 98, 49, 52, 112, 110, 118, 116, 110, 110, 115, 106, 55, 49, 115, 53, 115, 115, 97, 97, 103, 168, 105, 109, 97, 103, 101, 85, 114, 108, 160, 170, 108, 97, 115, 116, 65, 99, 116, 105, 118, 101, 206, 88, 73, 169, 165, 168, 108, 97, 115, 116, 78, 97, 109, 101, 171, 78, 97, 110, 97, 121, 97, 107, 107, 97, 114, 97, 165, 112, 104, 111, 110, 101, 172, 43, 57, 52, 55, 55, 49, 51, 53, 52, 56, 53, 54, 168, 116, 104, 117, 109, 98, 85, 114, 108, 160, 172, 114, 101, 102, 114, 101, 115, 104, 84, 111, 107, 101, 110, 218, 1, 60, 101, 121, 74, 104, 98, 71, 99, 105, 79, 105, 74, 73, 85, 122, 73, 49, 78, 105, 73, 115, 73, 110, 82, 53, 99, 67, 73, 54, 73, 107, 112, 88, 86, 67, 74, 57, 46, 101, 121, 74, 106, 98, 71, 108, 108, 98, 110, 81, 105, 79, 105, 74, 109, 78, 50, 89, 121, 78, 84, 108, 107, 89, 83, 49, 104, 78, 122, 82, 108, 76, 84, 82, 109, 78, 50, 81, 116, 79, 87, 85, 52, 79, 67, 48, 50, 90, 68, 89, 52, 77, 122, 89, 120, 90, 106, 107, 51, 78, 84, 103, 105, 76, 67, 74, 119, 90, 88, 74, 122, 98, 50, 52, 105, 79, 105, 74, 105, 77, 84, 82, 119, 98, 110, 90, 48, 98, 109, 53, 122, 97, 106, 99, 120, 99, 122, 86, 122, 99, 50, 70, 104, 90, 121, 73, 115, 73, 109, 82, 108, 100, 109, 108, 106, 90, 83, 73, 54, 73, 107, 81, 53, 82, 68, 100, 71, 81, 122, 99, 49, 76, 84, 73, 52, 78, 85, 89, 116, 78, 69, 70, 68, 82, 83, 48, 53, 82, 84, 77, 53, 76, 84, 65, 120, 79, 84, 81, 122, 78, 106, 104, 66, 77, 106, 107, 122, 81, 121, 73, 115, 73, 110, 82, 53, 99, 71, 85, 105, 79, 106, 69, 115, 73, 109, 86, 52, 99, 67, 73, 54, 77, 84, 99, 53, 77, 106, 73, 49, 79, 84, 65, 51, 77, 83, 119, 105, 97, 88, 78, 122, 73, 106, 111, 105, 84, 87, 57, 114, 98, 121, 49, 84, 90, 88, 74, 50, 97, 87, 78, 108, 73, 110, 48, 46, 85, 50, 113, 81, 109, 50, 99, 111, 95, 101, 67, 120, 77, 56, 102, 68, 82, 116, 65, 105, 53, 72, 87, 117, 121, 103, 98, 110, 73, 88, 119, 112, 77, 87, 84, 95, 117, 87, 88, 55, 48, 100, 69, 171, 100, 101, 115, 99, 114, 105, 112, 116, 105, 111, 110, 178, 82, 101, 113, 117, 101, 115, 116, 32, 115, 117, 99, 99, 101, 115, 115, 102, 117, 108, 167, 109, 101, 115, 115, 97, 103, 101, 178, 82, 101, 113, 117, 101, 115, 116, 32, 115, 117, 99, 99, 101, 115, 115, 102, 117, 108, 167, 115, 117, 99, 99, 101, 115, 115, 195]
    
    let data = Data(bytes: arr, count: arr.count)
    let msgpack = try unpack(data)
    print(msgpack)
} catch {
    print("Unpacking failed: \(error)")
}

Like I mentioned earlier, the unpacking happened successfully without error in Swift 2. I was using the v1.2 of this library. Here I tested it again with a sample Xcode 7.3 project.

When I converted the project to Swift 3 and upgraded the library to v2.0 and ran it against the same payload, it would fail with the error invalidArgument. I can't figure out why. All I could pin point was the error was occurring within the unpackString method.

I attached the Swift 3 converted Xcode 8 project as well. Same code. But the error occurs here.

Default version on CocoaPods doesn't work with Swift 2

I just installed this pod using this:

pod 'MessagePack.swift'

But that errors out with code syntax errors.

So instead, I installed it like this:

pod 'MessagePack.swift', :git => 'https://github.com/a2/MessagePack.swift.git'

And that works (it uses the master branch).

Is it possible to update the version on CocoaPods to avoid this problem?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.