LoginSignup
171
98

More than 5 years have passed since last update.

SION, a data serialization format a little more expressive than JSON

Last updated at Posted at 2018-07-09

SIONというシリアライゼーションフォーマットを提案します。Swiftによるレファランス実装はこちら。

SIONという名前は Swift Interchangeable Object Notation からとりました。名前の通りSwiftのリテラルが元になっています。以下はSIONで表現されたデータの一例です。

[
    "array" : [
        nil,
        true,
        1,    // Int in decimal
        1.0,  // Double in decimal
        "one",
        [1],
        ["one" : 1.0]
    ],
    "bool" : true,
    "data" : .Data("R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7"),
    "date" : .Date(0x0p+0),
    "dictionary" : [
        "array" : [],
        "bool" : false,
        "double" : 0x0p+0,
        "int" : 0,
        "nil" : nil,
        "object" : [:],
        "string" : ""
    ],
    "double" : 0x1.518f5c28f5c29p+5, // Double in hexadecimal
    "int" : -0x2a, // Int in hexadecimal
    "nil" : nil,
    "string" : "漢字、カタカナ、ひらがなの入ったstring😇",
    "url" : "https://github.com/dankogai/",
    nil   : "Unlike JSON and Property Lists,",
    true  : "Yes, SION",
    1     : "does accept",
    1.0   : "non-String keys.",
    []    : "like",
    [:]   : "Map of ECMAScript."
]

見ての通り、

  • (Dictionary|Object|Map)は{}ではなく[]で囲っている
  • Data.Data("base64encoded")と表現。ここでbase64encodedBase64でエンコードされたバイト列。
  • Date.Date(timeIntervalSince1970)と表現。ここでtimeIntervalSince1970は1970年01月01日00:00:00GMTからの経過時間をDoubleで表現したもの(SwiftのDate型の内部表現)。
  • // から改行まではコメント

としています。

動機

要はJSONは痒くても掻けなかったところを掻けるようにしたかったということです。痒いところというのは以下のとおり。

  • 数値がNumber、実質上のDoubleしかなく、数値データがlossyになりがち
    • IntDoubleをきちんと分けたい
  • String以外のキーが使えない
  • DataDateをサポートしていない
  • コメントが書けない

MessagePackはこれらをすベて解決していますが、しかしMessagePackはバイナリー。データ交換には最適でもデータ視覚化には何らかの形でそれをテキスト化する必要がある。しかしJSONでは漏れが出る。というわけでたどり着いたのが Swift のリテラルだったわけです。

参考に各種データシリアライゼーションフォーマットの各種データのサポート状況を表にまとめました。

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

レファランス実装

その最初のレファランス実装であるswift-sionは、本記事執筆現在

をサポートしています。

TODO

さいごに

ご意見をお待ちしております。

Dan the Safe, Fast and Expressive

171
98
13

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
171
98