swift-cbor

0.0.3

swift-cbor is a library of CBOR encoder & decoder for Swift based on Codable.
nnabeyang/swift-cbor

What's New

v0.0.3

2024-05-12T08:37:11Z

Fixes

  • Add support for decoding BinaryString-encoded data to String
  • Modified CborScanner's scan method to eliminate error throwing

swift-cbor

swift-cbor is a library of CBOR encoder & decoder for Swift based on Codable.

Usage

import SwiftCbor

struct Coordinate: Codable {
    let latitude: Double
    let longitude: Double
}

struct Landmark: Codable {
    let name: String
    let foundingYear: Int
    let location: Coordinate
}

let input = Landmark(
    name: "Mojave Desert",
    foundingYear: 0,
    location: Coordinate(
        latitude: 35.0110079,
        longitude: -115.4821313
    )
)
let encoder = CborEncoder()
let decoder = CborDecoder()
let data = try! encoder.encode(input)
let out = try! decoder.decode(Landmark.self, from: data)

print([UInt8](data))
// [163, 100, 110, 97, 109, 101, 109, 77, 111, 106,
//  97, 118, 101, 32, 68, 101, 115, 101, 114, 116,
//  108, 102, 111, 117, 110, 100, 105, 110, 103, 89,
//  101, 97, 114, 0, 104, 108, 111, 99, 97, 116,
//  105, 111, 110, 162, 104, 108, 97, 116, 105, 116,
//  117, 100, 101, 251, 64, 65, 129, 104, 180, 245,
//  63, 179, 105, 108, 111, 110, 103, 105, 116, 117,
//  100, 101, 251, 192, 92, 222, 219, 61, 61, 120,
//  49]

print(out)
// Landmark(
//   name: "Mojave Desert",
//   foundingYear: 0,
//   location: example.Coordinate(
//     latitude: 35.0110079,
//     longitude: -115.4821313
//   )
// )

Installation

SwiftPM

Add the SwiftCbor as a dependency:

let package = Package(
    // name, platforms, products, etc.
    dependencies: [
        // other dependencies
        .package(url: "https://github.com/nnabeyang/swift-cbor", from: "0.0.3"),
    ],
    targets: [
        .executableTarget(name: "<executable-target-name>", dependencies: [
            // other dependencies
                .product(name: "SwiftCbor", package: "swift-cbor"),
        ]),
        // other targets
    ]
)

License

swift-cbor is published under the MIT License, see LICENSE.

Author

Noriaki Watanabe@nnabeyang

Description

  • Swift Tools 5.10.0
View More Packages from this Author

Dependencies

Last updated: Fri Jul 19 2024 10:58:59 GMT-0900 (Hawaii-Aleutian Daylight Time)