UBJSON Serialization in native Swift 4
Installation & Compatibility
The recommended (and only tested) way to install and use UBJSONSerialization is
SwiftPM, using at least Swift 4.
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.0")], targets: [.target(name: "myawesomeproject", dependencies: ["UBJSONSerialization"])] )
UBJSONSerialization has the same basic interface than
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.
JSONSerialization, when a full valid object has been parsed
from a SimpleStream, you can unserialized
another object from the same stream. This is useful to parsed 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.
I am not aware of any other implementation of UBJSON serialization/deserialization at the moment in Swift.
I used the UBJSON specification from http://ubjson.org
- At some point in the future, but maybe in a separate project, add support for Swift’s Encoder protocol.
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
MIT (see License.txt file)