UBJSONSerialization

2.0.4

UBJSON Serialization in Swift (with the help of Apple’s Foundation)
Frizlab/UBJSONSerialization

What's New

2022-02-09T15:32:41Z

UBJSONSerialization

UBJSON Serialization in native Swift

Installation & Compatibility

The recommended (and only tested) way to install and use UBJSONSerialization is via SwiftPM, using at least Swift 5.1.

The content of your Package.swift should be something resembling:

import PackageDescription

let package = Package(
	name: "myawesomeproject",
	dependencies: [.package(url: "https://github.com/Frizlab/UBJSONSerialization.git", from: "1.0.1")],
	targets: [.target(name: "myawesomeproject", dependencies: [.product(name: "UBJSONSerialization", package: "UBJSONSerialization")])]
)

Usage

UBJSONSerialization has the same basic interface than JSONSerialization.

Example of use:

let myFirstUBJSONDoc = ["key": "value"]
let serializedUBJSONDoc = try UBJSONSerialization.data(withUBJSONObject: myFirstUBJSONDoc, options: [])
let unserializedUBJSONDoc = try UBJSONSerialization.ubjsonObject(with: serializedUBJSONDoc, options: [])
print(myFirstUBJSONDoc == unserializedUBJSONDoc as! [String: String])

Serializing/deserializing to/from a stream is also supported. Important: Unlike JSONSerialization, when a full valid object has been parsed from a [StreamReader](https://github.com/Frizlab/stream-reader), you can unserialized another object from the same stream. This is useful to parse a multiple separated documents coming in a single stream.

Finally, a method lets you know if a given dictionary can be serialized as an UBJSON document.

Alternatives

I am not aware of any other implementation of UBJSON serialization/deserialization at the moment in Swift.

Reference

I used the UBJSON specification from http://ubjson.org

To Do

  • ❏ Verify support for decoding multiple UBJSON docs in a single stream;

  • ❏ Support for streaming (receiving/sending data live);

  • ❏ At some point in the future, but maybe in a separate project, add support for Swift’s Encoder protocol.

  • ❏ Swift NIO support?

I’ll work seriously on the project if it gains enough attention. Feel free to open issues, I’ll do my best to answer.

Pull requests are welcome 😉

License

MIT (see License.txt file)

Description

  • Swift Tools 5.1.0
View More Packages from this Author

Dependencies

Last updated: Wed Apr 03 2024 17:16:11 GMT-0900 (Hawaii-Aleutian Daylight Time)