Swiftpack.co - Package - SomeRandomiOSDev/CBORCoding

CBORCoding

License MIT CocoaPods Compatible Carthage Compatible Platform Linux Build Code Coverage Codacy Swift

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

TODO

  • Add additional options to CBOREncoder and CBORDecoder.

Contributing

If you have need for a specific feature or you encounter a bug, please open an issue. If you extend the functionality of CBORCoding yourself or you feel like fixing a bug yourself, please submit a pull request.

Author

Joseph Newton, somerandomiosdev@gmail.com

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.

Github

link
Stars: 8

Dependencies

Used By

Total: 0

Releases

1.2.0 - 2020-03-06 13:04:42

  • Added support for Linux
  • Light project restructuring
  • Dropped Carthage requirement for development

1.1.0 - 2020-03-06 13:03:23

  • Added support to encoding and decoding native half-precision floating point numbers

1.0.5 - 2020-03-06 13:02:05

  • Added TopLevelEncoder conformance to CBOREncoder
  • Added TopLevelDecoder conformance to CBORDecoder

1.0.4 - 2020-03-06 13:00:56

  • Updated visibility of CBOREncoded fields

1.0.3 - 2020-03-06 12:59:29

  • Made CBOREncoded structure public
  • Added unit testing to cover the case of CBOREncoded being encoded with a non-CBOR encoder

1.0.2 - 2020-03-06 12:56:46

  • Added support for Travis-CI and Codecov
  • Added support for Swift Package Manager

1.0.1 - 2020-03-06 12:55:43

  • Minor project tweaks and updated README

1.0.0 - 2020-03-06 12:54:40

Initial Release