Giter Club home page Giter Club logo

Comments (3)

nicolaischneider avatar nicolaischneider commented on June 15, 2024
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.

hamchapman avatar hamchapman commented on June 15, 2024

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.

hamchapman avatar hamchapman commented on June 15, 2024

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 structs.

from swiftcbor.

Related Issues (20)

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.