Comments (4)
Hm, you actually can easily encode the map in-order manually.
Instead of building an outer CBOR enum with dictionaryLiteral
, just directly return the bytes for the map.
Encoding a map is simple, you just encode the count, OR the first byte with 0b101_00000
, and encode the key-value pairs.
Something like
public func encode() -> [UInt8] {
var res: [UInt8] = Int(3).encode() // well, your actual count
res[0] = res[0] | 0b101_00000
res.append(contentsOf: 0.encode())
res.append(contentsOf: 1001.encode())
res.append(contentsOf: 1.encode())
res.append(contentsOf: self.some_int32.encode())
res.append(contentsOf: 2.encode())
res.append(contentsOf: self.hasSome_int8() ? CBOR(integerLiteral: self.some_int8!) : CBOR(nilLiteral: ())) // CBOR enum itself is encodable
return res
}
from swiftcbor.
would it be possible to get the keys (integerLiteral) ordered in ascending order?
Not currently, our map representation is a Swift map, which doesn't preserve order :(
case map([CBOR : CBOR])
It would make sense to use a list of key-value pairs instead, but that's a breaking change (not that there's been a 1.0 release haha) and I won't be the one making that change (I don't have a Swift dev environment currently), sorry.
How do I get an Int value from a CBOR.unsignedInt ?
You get a UInt64
(or just UInt
before recent changes) by pattern matching.
if case let CBOR.unsignedInt(int_val) = cbor_val {
do_things_with(int_val);
}
I guess I should put a link to a pattern matching tutorial in the readme, since this is a frequently asked question...
from swiftcbor.
Thanks alot for the guide on the ordering of the map! I've managed to implement it.
I'm still relatively new to Swift, so I'm reading up on Swift pattern matching to get the UInt64 value.
Again, thanks alot for the quick replies :)
from swiftcbor.
Thanks alot for your help! I managed to get pattern matching to work for extracting the values from a CBOR object.
from swiftcbor.
Related Issues (20)
- Decoding an array using CodableCBORDecoder() HOT 6
- Unable to launch the app in Device HOT 1
- Error on encoding empty Array inside a Map HOT 4
- incorrect work with Codable
- Extracting/accessing tags from a cbor, HOT 1
- fatalError terminates application
- New release 0.4.4 HOT 2
- UnkeyedDecodingContainer - corrupted data causes crash HOT 1
- Orderd Dictionary/Map HOT 7
- CodableCBORDecoder crashes when trying to decode HOT 2
- Breaking Changes in 0.4.5 HOT 2
- `Codable` support should be updated to support `CodingKeyRepresentable`
- Clarify Carthage Support
- Semantic versioning not followed HOT 1
- iOS 16.1 UnsafeRawPointer is depreciated and decodeItem returns nil value HOT 3
- Crash when decoding optional UnkeyContainer
- Please follow Semantic Versioning HOT 2
- CBOR decode crashes HOT 2
- Error string
- CBOR Decode error with special characters HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from swiftcbor.