Comments (3)
func getDictionary (map: CBOR) -> [CBOR:CBOR]? {
var extractedDict = [CBOR:CBOR]()
switch map {
case .map(let dict): extractedDict = dict
default: break
}
return extractedDict
}
All elements inside the map are CBOR objects that need to be converted aswell.
from swiftcbor.
I've used some code like this in the past. It's not pretty though, and it might have some bugs in it.
func convertCBORMapToDictionary(_ cborMap: [CBOR: CBOR]) throws -> [String: Any?] {
var result = [String: Any?]()
for pair in cborMap {
guard case let .utf8String(str) = pair.key else {
fatalError("Non-String key in CBOR document: \(cborMap)")
}
result[str] = try convertToAny(pair.value)
}
return result
}
func convertCBORMapToDictionary<T>(_ cborMap: [CBOR: CBOR]) throws -> [String: T?] {
var result = [String: T?]()
for pair in cborMap {
guard case let .utf8String(str) = pair.key else {
fatalError("Non-String key in CBOR document: \(cborMap)")
}
result[str] = try convertTo(pair.value)
}
return result
}
func convertToAny(_ val: CBOR) throws -> Any? {
return try convertTo(val)
}
func convertTo<T>(_ val: CBOR) throws -> T? {
switch val {
case .boolean(let inner):
return inner as? T
case .unsignedInt(let inner):
return Int(inner) as? T
case .negativeInt(let inner):
return -Int(inner) - 1 as? T
case .double(let inner):
return inner as? T
case .float(let inner):
return inner as? T
case .half(let inner):
return inner as? T
case .simple(let inner):
return inner as? T
case .byteString(let inner):
return inner as? T
case .null:
return nil as T?
case .undefined:
return nil as T?
case .date(let inner):
return inner as? T
case .utf8String(let inner):
return inner as? T
case .array(let innerArr):
return try innerArr.map(convertToAny) as? T
case .map(let innerMap):
return try convertCBORMapToDictionary(innerMap) as? T
default:
return nil as T?
}
}
from swiftcbor.
Given the library has Codable
support you can also define a struct
that represents the structure of the data you are expecting in the CBOR and then try and decode the data into an instance of one of your newly created struct
s.
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.