Giter Site home page Giter Site logo

cborswift's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

phisakel

cborswift's Issues

Problem with big integers

Hello, I'm implementing encode/decode and noticed a problem with both of them when using big numbers. I'm using the following set of data.

let barr = [**1531842146400**,[1332, 0, "0.3.0-dev", "0.3.0-dev", "4.0.2", "866191031649643", "8935101811542082547", "M95FAR02A08","AXN_2.32_3337_15010801", 3, 0],[1398, 8, NSSimpleValue(nil), NSSimpleValue(nil), NSSimpleValue(nil), NSSimpleValue(nil), NSSimpleValue(nil)],[1376, 7, 1]] as [Any]

 let cc = CBOR.encode(barr as NSArray)

which encoded using the playground results in 9f1b00000164a8e830608b1905340069302e332e302d64657669302e332e302d64657665342e302e326f38363631393130333136343936343373383933353130313831313534323038323534376b4d393546415230324130387641584e5f322e33325f333333375f313530313038303103008719057608f6f6f6f6f6831905600701ff

My problem is that the first value 1531842146400 since it's a bit integer can't be encoded (crashes) and when decoding it returns an empty array not showing any values. But if I use a smaller number everything works fine. What can I do to work around this situation?

Thanks

Problem with NSData as Value when Decoding

I have this sample use case where I have a JSON-like NSDictionary but I want to put some random NSData to prove that I can add something like a small UIImage/Audio file into CBOR.

func test_2_encodeMap_2() {

        // begin generating some random NSData
        let length = 2048
        let bytes = [UInt32](repeating: 0, count: length).map { _ in arc4random() }
        let data = Data(bytes: bytes, count: length)
        // end generating some random NSData
        
        let json: NSDictionary = [
            "_id": "123abc",
            "color": "Red",
            "age": 56,
            "mileage": 56.444,
            "tags": [
                "a", "b", "d", 4
            ],
            "data": data
        ]
        
        
        var encoded = CBOR.encode(json as NSDictionary)
        var decoded = CBOR.decode(encoded!) as! NSDictionary
}

It encodes fine but crashes saying "Thread 1: Fatal error: Array index is out of range" at Decoder.swift on Line 77

What am I doing wrong?

Supporting Unicode languages

CBOR Swift does not support Unicode languages and to solve this problem you should implement as follows:

In "Extensions" :

public var ascii_bytes: [UInt8] {
        // To supporting unicode languages it should return with .utf8 encoding
        return self.data(using: .ascii)?.bytes ?? self.data(using: .utf8)!.bytes
    }

In "Encoder":

@objc override func encode() -> String {
        
        // To support Unicode languages, the string encoding must be specified before giving the string length
        let asciiBytes = self.ascii_bytes
        let encodedArray = Encoder.prepareByteArray(major: .major3, measure: asciiBytes.count)
        let headerData  = Data(bytes: encodedArray).binary_decimal.hex
        let strData     = Data(bytes: asciiBytes).hexString
        
        return headerData.appending(strData)
    }

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.