a2 / MessagePack.swift

It's like JSON, but fast and small…and Swift! – msgpack.org[Swift]
http://msgpack.org
MIT License
283 stars 60 forks source link

Any -> MessagePackValue conversion #36

Closed eliburke closed 8 years ago

eliburke commented 8 years ago

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;
        }
    }
a2 commented 8 years ago

I don't think it's in the scope of this project to integrate this code, which adds support for another library.

eliburke commented 8 years ago

Fair enough. But to be clear... this doesn't add support for another library. It's just utility code to create MessagePackValues.

a2 commented 8 years ago

@eliburke Ah, I understand now. I would say that converting from Array<Any> to MessagePackValue is not in the scope of this project.