Easy CBOR encoding and decoding for iOS, macOS, tvOS and watchOS.

Overview

CBORCoding

Codacy Badge License MIT CocoaPods Compatible Carthage Compatible Platform Code Coverage

Carthage Cocoapods Documentation Swift Package SwiftLint XCFramework Xcode Project

CBORCoding is a lightweight framework containing a coder pair for encoding and decoding Codable conforming types to and from CBOR document format for iOS, macOS, tvOS, and watchOS.

Installation

CBORCoding is available through CocoaPods, Carthage and the Swift Package Manager.

To install via CocoaPods, simply add the following line to your Podfile:

pod 'CBORCoding'

To install via Carthage, simply add the following line to your Cartfile:

github "SomeRandomiOSDev/CBORCoding"

To install via the Swift Package Manager add the following line to your Package.swift file's dependencies:

.package(url: "https://github.com/SomeRandomiOSDev/CBORCoding.git", from: "1.0.0")

Usage

First import CBORCoding at the top of your Swift file:

import CBORCoding

After importing, the use is nearly identical to that of the JSONEncoder/JSONDecoder class pair provided by the Swift Foundation library. This example shows how to encode an instance of a simple Car type to a CBOR object:

struct Car: Codable {
    var manufacturer: String
    var model: String
    var horsePower: Int
    var description: String?
}

let stinger = Car(manufacturer: "Kia", model: "Stinger GT2", horsePower: 365, description: nil)  
let encoder = CBOREncoder()

let encodedData = try encoder.encode(stinger)
print("CBOR: \(hexString(encodedData))")

/* Prints:
 CBOR: 0xA36C6D616E756661637475726572634B6961656D6F64656C6B5374696E676572204754326A686F727365506F77657219016D
*/

This example shows how to decode that encoded Car value at a later time:

let decoder = CBORDecoder()
let stinger = try decoder.decode(Car.self, from: encodedData)

CBOR

Concise Binary Object Representation is a data format for being able to encode formatted data with a goal of a having as small a message size as possible.

While this framework implements as much of the specification as possible, there are a few notable exceptions:

  • CBOR supports keys of any defined type, however, since Codable relies on CodingKey for encoding/decoding its keyed containers, this framework is limited in its supported key types to Int and String.
  • CBOR supports DecimalFractions and Bigfloats whose mantissa is a Bignum. With the current implementation, this is limited to Bignums whose content fits into either a Int64 or UInt64 type.

For more information about the CBOR format see: CBOR & RFC 8949.

Contributing

Whether it's submitting a feature request, reporting a bug, or writing code yourself, all contributions to this library are welcome! Please see CONTRIBUTING for more information on how you can contribute.

Author

Joe Newton, [email protected]

Credits

CBORCoding is based heavily on the JSONEncoder/JSONDecoder classes provided by Swift. See ATTRIBUTIONS for more details.

License

CBORCoding is available under the MIT license. See the LICENSE file for more info.

Comments
  • `xcodebuild` can't find the `Half` module when building from the command line

    `xcodebuild` can't find the `Half` module when building from the command line

    I'm not completely sure this is something that is the fault of this repo, but my app builds fine in Xcode itself, but my release script that automates several steps uses the xcodebuild command line and it fails with the following:

    CompileSwiftSources normal x86_64 com.apple.xcode.tools.swift.compiler (in target 'CBORCoding' from project 'CBORCoding')
        cd /Users/toadzky/Library/Developer/Xcode/DerivedData/File_Buddy_11-avyycioolgtstddodlblxuzxcrtc/SourcePackages/checkouts/CBORCoding
        export DEVELOPER_DIR=/Applications/Xcode.app/Contents/Developer
        export SDKROOT=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk
        /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swiftc -incremental -module-name CBORCoding -O -whole-module-optimization -enforce-exclusivity=checked @/Users/toadzky/Library/Developer/Xcode/DerivedData/File_Buddy_11-avyycioolgtstddodlblxuzxcrtc/SourcePackages/checkouts/CBORCoding/build/CBORCoding.build/Release/CBORCoding.build/Objects-normal/x86_64/CBORCoding.o.SwiftFileList -DSWIFT_PACKAGE -DXcode -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk -target x86_64-apple-macos10.10 -g -Xfrontend -serialize-debugging-options -swift-version 5 -I /Users/toadzky/Library/Developer/Xcode/DerivedData/File_Buddy_11-avyycioolgtstddodlblxuzxcrtc/SourcePackages/checkouts/CBORCoding/build/Release -F /Users/toadzky/Library/Developer/Xcode/DerivedData/File_Buddy_11-avyycioolgtstddodlblxuzxcrtc/SourcePackages/checkouts/CBORCoding/build/Release -F /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks -c -num-threads 8 -output-file-map /Users/toadzky/Library/Developer/Xcode/DerivedData/File_Buddy_11-avyycioolgtstddodlblxuzxcrtc/SourcePackages/checkouts/CBORCoding/build/CBORCoding.build/Release/CBORCoding.build/Objects-normal/x86_64/CBORCoding-OutputFileMap.json -parseable-output -serialize-diagnostics -emit-dependencies -emit-module -emit-module-path /Users/toadzky/Library/Developer/Xcode/DerivedData/File_Buddy_11-avyycioolgtstddodlblxuzxcrtc/SourcePackages/checkouts/CBORCoding/build/CBORCoding.build/Release/CBORCoding.build/Objects-normal/x86_64/CBORCoding.swiftmodule -Xcc -I/Users/toadzky/Library/Developer/Xcode/DerivedData/File_Buddy_11-avyycioolgtstddodlblxuzxcrtc/SourcePackages/checkouts/Half/Sources/CHalf/include -Xcc -I/Users/toadzky/Library/Developer/Xcode/DerivedData/File_Buddy_11-avyycioolgtstddodlblxuzxcrtc/SourcePackages/checkouts/CBORCoding/build/Release/include -Xcc -I/Users/toadzky/Library/Developer/Xcode/DerivedData/File_Buddy_11-avyycioolgtstddodlblxuzxcrtc/SourcePackages/checkouts/CBORCoding/build/CBORCoding.build/Release/CBORCoding.build/DerivedSources-normal/x86_64 -Xcc -I/Users/toadzky/Library/Developer/Xcode/DerivedData/File_Buddy_11-avyycioolgtstddodlblxuzxcrtc/SourcePackages/checkouts/CBORCoding/build/CBORCoding.build/Release/CBORCoding.build/DerivedSources/x86_64 -Xcc -I/Users/toadzky/Library/Developer/Xcode/DerivedData/File_Buddy_11-avyycioolgtstddodlblxuzxcrtc/SourcePackages/checkouts/CBORCoding/build/CBORCoding.build/Release/CBORCoding.build/DerivedSources -Xcc -DSWIFT_PACKAGE -working-directory /Users/toadzky/Library/Developer/Xcode/DerivedData/File_Buddy_11-avyycioolgtstddodlblxuzxcrtc/SourcePackages/checkouts/CBORCoding
    
    CompileSwift normal x86_64 (in target 'CBORCoding' from project 'CBORCoding')
        cd /Users/toadzky/Library/Developer/Xcode/DerivedData/File_Buddy_11-avyycioolgtstddodlblxuzxcrtc/SourcePackages/checkouts/CBORCoding
        /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift -frontend -c /Users/toadzky/Library/Developer/Xcode/DerivedData/File_Buddy_11-avyycioolgtstddodlblxuzxcrtc/SourcePackages/checkouts/CBORCoding/Sources/CBORCoding/CBOR+Codable.swift /Users/toadzky/Library/Developer/Xcode/DerivedData/File_Buddy_11-avyycioolgtstddodlblxuzxcrtc/SourcePackages/checkouts/CBORCoding/Sources/CBORCoding/CBOR+Equatable.swift /Users/toadzky/Library/Developer/Xcode/DerivedData/File_Buddy_11-avyycioolgtstddodlblxuzxcrtc/SourcePackages/checkouts/CBORCoding/Sources/CBORCoding/CBOR.swift /Users/toadzky/Library/Developer/Xcode/DerivedData/File_Buddy_11-avyycioolgtstddodlblxuzxcrtc/SourcePackages/checkouts/CBORCoding/Sources/CBORCoding/CBORDecoder.swift /Users/toadzky/Library/Developer/Xcode/DerivedData/File_Buddy_11-avyycioolgtstddodlblxuzxcrtc/SourcePackages/checkouts/CBORCoding/Sources/CBORCoding/CBOREncoder.swift /Users/toadzky/Library/Developer/Xcode/DerivedData/File_Buddy_11-avyycioolgtstddodlblxuzxcrtc/SourcePackages/checkouts/CBORCoding/Sources/CBORCoding/CBORParser.swift /Users/toadzky/Library/Developer/Xcode/DerivedData/File_Buddy_11-avyycioolgtstddodlblxuzxcrtc/SourcePackages/checkouts/CBORCoding/Sources/CBORCoding/Containers.swift -supplementary-output-file-map /var/folders/dc/pmsq9v796hzdjyj07cgn0fhw0000gn/T/supplementaryOutputs-d43348 -target x86_64-apple-macos10.10 -enable-objc-interop -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk -I /Users/toadzky/Library/Developer/Xcode/DerivedData/File_Buddy_11-avyycioolgtstddodlblxuzxcrtc/SourcePackages/checkouts/CBORCoding/build/Release -F /Users/toadzky/Library/Developer/Xcode/DerivedData/File_Buddy_11-avyycioolgtstddodlblxuzxcrtc/SourcePackages/checkouts/CBORCoding/build/Release -F /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks -g -swift-version 5 -enforce-exclusivity=checked -O -D SWIFT_PACKAGE -D Xcode -serialize-debugging-options -Xcc -working-directory -Xcc /Users/toadzky/Library/Developer/Xcode/DerivedData/File_Buddy_11-avyycioolgtstddodlblxuzxcrtc/SourcePackages/checkouts/CBORCoding -Xcc -I/Users/toadzky/Library/Developer/Xcode/DerivedData/File_Buddy_11-avyycioolgtstddodlblxuzxcrtc/SourcePackages/checkouts/Half/Sources/CHalf/include -Xcc -I/Users/toadzky/Library/Developer/Xcode/DerivedData/File_Buddy_11-avyycioolgtstddodlblxuzxcrtc/SourcePackages/checkouts/CBORCoding/build/Release/include -Xcc -I/Users/toadzky/Library/Developer/Xcode/DerivedData/File_Buddy_11-avyycioolgtstddodlblxuzxcrtc/SourcePackages/checkouts/CBORCoding/build/CBORCoding.build/Release/CBORCoding.build/DerivedSources-normal/x86_64 -Xcc -I/Users/toadzky/Library/Developer/Xcode/DerivedData/File_Buddy_11-avyycioolgtstddodlblxuzxcrtc/SourcePackages/checkouts/CBORCoding/build/CBORCoding.build/Release/CBORCoding.build/DerivedSources/x86_64 -Xcc -I/Users/toadzky/Library/Developer/Xcode/DerivedData/File_Buddy_11-avyycioolgtstddodlblxuzxcrtc/SourcePackages/checkouts/CBORCoding/build/CBORCoding.build/Release/CBORCoding.build/DerivedSources -Xcc -DSWIFT_PACKAGE -module-name CBORCoding -num-threads 8 -o /Users/toadzky/Library/Developer/Xcode/DerivedData/File_Buddy_11-avyycioolgtstddodlblxuzxcrtc/SourcePackages/checkouts/CBORCoding/build/CBORCoding.build/Release/CBORCoding.build/Objects-normal/x86_64/CBOR+Codable.o -o /Users/toadzky/Library/Developer/Xcode/DerivedData/File_Buddy_11-avyycioolgtstddodlblxuzxcrtc/SourcePackages/checkouts/CBORCoding/build/CBORCoding.build/Release/CBORCoding.build/Objects-normal/x86_64/CBOR+Equatable.o -o /Users/toadzky/Library/Developer/Xcode/DerivedData/File_Buddy_11-avyycioolgtstddodlblxuzxcrtc/SourcePackages/checkouts/CBORCoding/build/CBORCoding.build/Release/CBORCoding.build/Objects-normal/x86_64/CBOR.o -o /Users/toadzky/Library/Developer/Xcode/DerivedData/File_Buddy_11-avyycioolgtstddodlblxuzxcrtc/SourcePackages/checkouts/CBORCoding/build/CBORCoding.build/Release/CBORCoding.build/Objects-normal/x86_64/CBORDecoder.o -o /Users/toadzky/Library/Developer/Xcode/DerivedData/File_Buddy_11-avyycioolgtstddodlblxuzxcrtc/SourcePackages/checkouts/CBORCoding/build/CBORCoding.build/Release/CBORCoding.build/Objects-normal/x86_64/CBOREncoder.o -o /Users/toadzky/Library/Developer/Xcode/DerivedData/File_Buddy_11-avyycioolgtstddodlblxuzxcrtc/SourcePackages/checkouts/CBORCoding/build/CBORCoding.build/Release/CBORCoding.build/Objects-normal/x86_64/CBORParser.o -o /Users/toadzky/Library/Developer/Xcode/DerivedData/File_Buddy_11-avyycioolgtstddodlblxuzxcrtc/SourcePackages/checkouts/CBORCoding/build/CBORCoding.build/Release/CBORCoding.build/Objects-normal/x86_64/Containers.o
    /Users/toadzky/Library/Developer/Xcode/DerivedData/File_Buddy_11-avyycioolgtstddodlblxuzxcrtc/SourcePackages/checkouts/CBORCoding/Sources/CBORCoding/CBORDecoder.swift:10:8: error: no such module 'Half'
    import Half
           ^
    
    ** BUILD FAILED **
    
    
    The following build commands failed:
    	CompileSwift normal x86_64
    	CompileSwiftSources normal x86_64 com.apple.xcode.tools.swift.compiler
    (2 failures)
    

    I added it to my xcode project using xcode's gui for adding swift packages.

    opened by toadzky 10
  • Fix exception when parsing with sliced Data

    Fix exception when parsing with sliced Data

    When a Data input is used that has a slice started beyond 0, an exception was thrown because the parser tried to access index 0.

    See for example https://stackoverflow.com/questions/54698947/swift-data-subdata-fails-with-exc-bad-instruction-code-exc-i386-invop-subcode for an explanation.

    All that was needed to fix it was to start at data.startIndex and finishes before data.endIndex. I also added a testcase that failed before my change and succeeds after my changes. The rest of the cases still run.

    opened by joskuijpers 2
  • String table

    String table

    Hi,

    I have used this package to encode (essentially) a lis of 500 structs that have the same keys. It seems that in the binary encoding, the keys are now repeated 500 times, taking up up to 50% of the total encoding size.

    Is this according to spec? Have there been considerations to have a string table?

    I have not looked too deeply into the CBOR details. If it is truly like json, and you can simply embed one CBOR object into another CBOR object, then I guess a header is not possible.

    Does anyone know how to build a string table with Codable? Then I can add it to a fork.

    opened by joskuijpers 2
  • Added back erroneously removed Linux run from Swift Package workflow

    Added back erroneously removed Linux run from Swift Package workflow

    Checklist

    • [X] Code compilation.
    • [X] All tests passing.
    • [X] No new SwiftLint issues.
    • [X] Added new unit tests, if applicable.
    • [X] Extended the documentation (including README), if applicable.
    • [X] Updated version in CBORCoding.podspec following semver guidelines.
    • [X] Ran workflowtests.sh and passed.
    opened by SomeRandomiOSDev 0
  • Bulk updates

    Bulk updates

    Checklist

    • [X] Code compilation.
    • [X] All tests passing.
    • [X] No new SwiftLint issues.
    • [X] Added new unit tests, if applicable.
    • [X] Extended the documentation (including README), if applicable.
    • [X] Updated version in CBORCoding.podspec following semver guidelines.
    • [X] Ran workflowtests.sh and passed.

    Proposed changes

    Regenerated project using the latest from ProjectTemplate. Added DocC documentation and fixed up/added code documentation where needed. Added pull request and issue form templates. Added code of conduct, contributing, and contributors documents. Updated all workflows to run using macOS-11 and added filters to avoid unnecessary workflow runs. Added log artifacts to relevant workflows. Added and updated scripts. Fixed up Codacy issues.

    opened by SomeRandomiOSDev 0
  • Fixed Code Coverage Reports

    Fixed Code Coverage Reports

    Travis-CI seems to not want to upload to codecov.io, or codecov.io isn't accepting uploads from Travis-CI, not sure either way. Attempting Code Coverage uploads from Github Workflow

    opened by SomeRandomiOSDev 0
Releases(1.3.2)
Owner
Joe Newton
I'm a developer who loves writing open-source libraries. I'm proficient in a variety of languages, IBNLT Swift, Objective-C, C++ and C.
Joe Newton
A cross-platform library of Swift utils to ease your iOS | macOS | watchOS | tvOS and Linux development.

Mechanica A library of Swift utils to ease your iOS, macOS, watchOS, tvOS and Linux development. Requirements Documentation Installation License Contr

Alessandro 28 Aug 28, 2022
Swift Package for Decoding RSS Feeds.

SyndiKit Swift Package built on top of XMLCoder for Decoding RSS Feeds. Check out the DocC-Built Site! Table of Contents Introduction Features Install

BrightDigit 34 Dec 27, 2022
Super powerful remote config utility written in Swift (iOS, watchOS, tvOS, OSX)

Mission Control Super powerful remote config utility written in Swift (iOS, watchOS, tvOS, OSX) Brought to you by Have you ever wished you could chang

appculture 113 Sep 9, 2022
Collection of native Swift extensions to boost your development. Support tvOS and watchOS.

SparrowKit Collection of native Swift extensions to boost your development. Support iOS, tvOS and watchOS. If you like the project, don't forget to pu

Ivan Vorobei 119 Dec 20, 2022
Versions tracker for your iOS, macOS, and tvOS app

VersionTrackerSwift VersionTrackerSwift is a versions / builds tracker to know which version has been installed by a user. Usage In your ApplicationDe

Tom Baranes 82 Oct 5, 2022
LanguageList for SwiftUI supporting iOS/macOS/tvOS

LanguageList for iOS, macOS and tvOS LanguageList is created and maintaned with ❥ by Sascha Muellner. What? This is a SwiftUI package with support for

Swift Package Repository 2 Jan 26, 2022
Easy way to detect iOS device properties, OS versions and work with screen sizes. Powered by Swift.

Easy way to detect device environment: Device model and version Screen resolution Interface orientation iOS version Battery state Environment Helps to

Anatoliy Voropay 582 Dec 25, 2022
Swift-DocC is a documentation compiler for Swift frameworks and packages aimed at making it easy to write and publish great developer documentation.

Swift-DocC is a documentation compiler for Swift frameworks and packages aimed at making it easy to write and publish great developer docum

Apple 833 Jan 3, 2023
🗃 Powerful and easy to use Swift Query Builder for Vapor 3.

⚠️ This lib is DEPRECATED ⚠️ please use SwifQL with Bridges Quick Intro struct PublicUser: Codable { var name: String var petName: String

iMike 145 Sep 10, 2022
RandomKit is a Swift framework that makes random data generation simple and easy.

RandomKit is a Swift framework that makes random data generation simple and easy. Build Status Installation Compatibility Swift Package Manager CocoaP

Nikolai Vazquez 1.5k Dec 29, 2022
CocoAttributedStringBuilder: Elegant and Easy AttributedStringBuilder in Swift

CocoAttributedStringBuilder: Elegant and Easy AttributedStringBuilder in Swift Features Requirements Installation SampleProjects Usage Contributors Li

Kiarash Vosough 10 Sep 5, 2022
SwiftRegressor - A linear regression tool that’s flexible and easy to use

SwiftRegressor - A linear regression tool that’s flexible and easy to use

null 3 Jul 10, 2022
Swift Date() made easy

Swift-Date-Extensions Swift Date() made easy! // Will return the wekkday in a st

Ricky Stone 0 Dec 28, 2021
Sovran-Swift: Small, efficient, easy. State Management for Swift

Sovran-Swift: Small, efficient, easy. State Management for Swift

Segment 5 Jan 3, 2023
Handling dimensional numbers with physical units in Swift made easy.

Dimensional arithmetics in Swift This package provides an easy and natural way of dealing with physical sizes. Performing complex arithmetics or unit

Niklas Nickel 1 May 27, 2022
Testable Combine Publishers - An easy, declarative way to unit test Combine Publishers in Swift

Testable Combine Publishers An easy, declarative way to unit test Combine Publishers in Swift About Combine Publishers are notoriously verbose to unit

Albert Bori 6 Sep 26, 2022
An application focused on managing men's haircuts. It has never been so easy to keep the cut on time

An application focused on managing men's haircuts. It has never been so easy to keep the cut on time

Yago Marques 2 Oct 13, 2022
SwiftyUpdateKit is a framework for iOS and macOS.

SwiftyUpdateKit is a framework for iOS and macOS. This framework supports for a user to update your app when new app version is released on the App Store.

Hituzi Ando 4 Aug 24, 2022
A macOS application displaying the thermal, voltage and current sensor values.

Sensors About A macOS application displaying the thermal, voltage and current sensor values. License Project is released under the terms of the MIT Li

Jean-David Gadina 82 Jan 3, 2023