hassan-shahbazi / cborswift Goto Github PK
View Code? Open in Web Editor NEWSwift implementation for CBOR
Home Page: http://cbor.io/impls.html#swift
License: MIT License
Swift implementation for CBOR
Home Page: http://cbor.io/impls.html#swift
License: MIT License
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
The code crashes
It would be awesome if you added support for UUIDs as per https://github.com/lucas-clemente/cbor-specs/blob/master/uuid.md
I'm willing to code support myself, tried using the custom tag support as well but couldn't. Please help, thanks!
I'm able to get a UUID's representation as [UInt8] but I can't figure out what to do next after that.
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?
It has caused a lot of frustration
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)
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.