7
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ZOZOAdvent Calendar 2024

Day 10

KSONについて

Last updated at Posted at 2024-12-09

KSONはKeyless Schemæ Object Notationの略でJSONからキー情報を省くことでコンパクトにすることを目的としたフォーマットです。
キー情報が存在しないため、スキーマと別途渡す必要があります。

まず、以下の方法でスキーマを定義しKSONに渡します。

var movie_schemas = [
  {
    "id": "role",
    "fields": ["name", "character"],
    "meta": [0, 0]
  },{
    "id": "movie",
    "fields": ["title", "year", "rating", "cover", "actors"],
    "meta": [
      0,
      0,
      0,
      "prefix:http\://movies.db/covers/|suffix:.jpg",
      "[]role"
    ]
  }
];
KSON.addSchema(movie_schemas);

そして事前に定義したスキーマに合致するオブジェクトを用意し、KSON文字列化をします。

var movies = [
  {
   "title": "Forrest Gump",
   "year": 1994,
   "rating": 8.7,
   "cover": "http://movies.db/covers/8.jpg",
   "actors": [
    {"name": "Tom Hanks", "character": "Forest Gump"},
    {"name": "Robin Wright", "character": "Jenny Curran"},
    {"name": "Gary Sinise", "character": "Lieutenant Dan Taylor"}
   ]
  },
  {
    "title": "Toy Story",
    ...
  }
  ...
];

KSON.stringify(movies, "[]movie")

KSON化した結果は以下のようになります。
JSON化した場合と比較するとオブジェクトのキー情報がなくコンパクトな表現になっていることが分かります。

[
  "[]movie",
  "Forest Gump",
  1994,
  8.7,
  "8",
  [
    ["Tom Hanks", "Forest Gump"],
    ["Robin Wright", "Jenny Curran"],
    ["Gary Sinise", "Lieutenant Dan Taylor"]
  ],
  "Toy Story",
  ...
]

出展:

7
0
0

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
7
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?