SION handler in Swift.
import SION
var sion:SION = [
"nil": nil,
"bool": true,
"int": -42,
"double": 42.195,
"string": "漢字、カタカナ、ひらがなの入ったstring😇",
"array": [nil, true, 1, 1.0, "one", [1], ["one":1.0]],
"dictionary": [
"nil":nil, "bool":false, "int":0, "double":0.0, "string":"","array":[], "object":[:]
],
"url":"https://github.com/dankogai/"
]
sion["data"] = .Data("R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7")
sion["date"] = .Date(0x0p+0)
sion["ext"] = .Ext("1NTU") // 0xd4,0xd4,0xd4This module is both an introduction and a reference implementation of SION, a data serialization formatlike JSON but more capable and expressive . As JSON is originated from a {ECMA,Java}Script literal, SION is originated from a Swift literal. So like JSON was named after JavaScript Object Notation, SION was named after Swift Interchangeable Object Notation. But as its name suggests, SION is language independent like JSON.
SION can serialize anything JSON can plus:
- support
Data - support
Date - non-
Stringkeys inDictionary IntandDoubledistinctively, notNumber. Therefore you can exchange 64-bit integers losslessly.- // comment support!
SIONEncoderandSIONDecoder— encode and decode your ownCodabletypes, likeJSONEncoderandJSONDecoder.- Roughly equvalent to MsgPack in terms of capability.
- MsgPack is a binary serialization while
SIONis a text serialization.
- MsgPack is a binary serialization while
| Type | SION | MsgPack | JSON | Property List | Comment |
|---|---|---|---|---|---|
Nil |
✔︎ | ✔︎ | ✔︎ | ❌ | plist: .binary only |
Bool |
✔︎ | ✔︎ | ✔︎ | ✔︎ | |
Int |
✔︎ | ✔︎ | ❌ | ✔︎ | 64bit |
Double |
✔︎ | ✔︎ | ✔︎ | ✔︎ | JSON's Number |
String |
✔︎ | ✔︎ | ✔︎ | ✔︎ | utf-8 encoded |
Data |
✔︎ | ✔︎ | ❌ | ✔︎ | binary blob |
Date |
✔︎ | ✔︎ | ❌ | ✔︎ | .timeIntervalSince1970 in Double |
[Self] |
✔︎ | ✔︎ | ✔︎ | ✔︎ | aka Array |
[String:Self] |
✔︎ | ✔︎ | ✔︎ | ✔︎ | aka Object, Map… |
[Self:Self] |
✔︎ | ✔︎ | ❌ | ❌ | non-String keys |
Ext |
✔︎ | ✔︎ | ❌ | ❌ | msgpack extension |
- As you see
SIONis upper-compatible with JSON and Property List. As a matter of fact,SIONcan {,de}serialize JSON and Property List.
As for the format details, see the main page of SION.
Your own Codable types {en,de}code just like they do with JSONEncoder and JSONDecoder — except Date and Data are stored natively, so no encoding strategies are needed:
struct Person : Codable, Equatable {
let name:String
let birthday:Date
let avatar:Data
let tags:[String]
}
let dan = Person(
name: "dankogai",
birthday: Date(timeIntervalSince1970: 0x1p30),
avatar: Data([0xde, 0xad, 0xbe, 0xef]),
tags: ["swift", "perl"]
)
let text = try SIONEncoder().encode(toString:dan, space:2) // SION text
let back = try SIONDecoder().decode(Person.self, from:text) // == danSee DESCRIPTION.md for details.
Is now at DESCRIPTION.md.
$ git clone https://github.com/dankogai/swift-sion.git
$ cd swift-sion # the following assumes your $PWD is here
$ swift buildSimply
$ swift run --replNote for Swift 6.4 or later: SwiftPM switched its default build system to swiftbuild, whose REPL support is currently broken — the REPL launches but cannot find the module symbols (swiftlang/swift-package-manager#10553). Until that is fixed upstream, ask for the previous build system explicitly:
$ swift run --repl --build-system nativeand in your repl,
1> import SION
2> let sion:SION = ["swift":["safe","fast","expressive"]]
sion: SION.SION = Object {
Object = 1 key/value pair {
[0] = {
key = "swift"
value = Array {
Array = 3 values {
[0] = String {
String = "safe"
}
[1] = String {
String = "fast"
}
[2] = String {
String = "expressive"
}
}
}
}
}
}Just open ./Package.swift
The bundled macOS.playground is self-contained: it carries its own copy of SION.swift in its Sources, so just open it in Xcode and run any page. It no longer relies on Xcode building the package scheme first (which stopped working as of Xcode 26.4 / Swift 6.4).
Unfortunately Swift Package Manager does not work well with Swift Playgrounds even though it claims to support it (too many error=22 :-). But don't worry. This module is so compact all you need is copy SION.swift.
In case of Swift Playgrounds just add it to one of the sources there. In which case import SION is not necessary.
Just add:
https://github.com/dankogai/swift-sion.git
via "Add Package Dependencies..." or "Add Package..." menu.
Add the following to the dependencies section:
.package(
url: "https://github.com/dankogai/swift-sion.git", from: "0.0.0"
)and the following to the .target argument:
.target(
name: "YourSwiftyPackage",
dependencies: ["SION"])Now all you have to do is:
import SIONin your code. Enjoy!
Swift 5.7 or better.