Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
The Future of encoding/json
Search
Shunta Komatsu
December 01, 2023
Technology
2
830
The Future of encoding/json
https://kyotogo.connpass.com/event/285351/
Shunta Komatsu
December 01, 2023
Tweet
Share
More Decks by Shunta Komatsu
See All by Shunta Komatsu
Recap: Automatically Instrument Your Go Source Code with Orchestrion
iamshunta
0
520
Recap: The Future of JSON in Go
iamshunta
0
1.1k
Other Decks in Technology
See All in Technology
財務データを題材に、 ETLとは何であるかを考える
shoe116
5
1.9k
dbtを中心にして組織のアジリティとガバナンスのトレードオンを考えてみた
gappy50
2
410
DevSecOps入門:Security Development Lifecycleによる開発プロセスのセキュリティ強化
yuriemori
0
210
信頼性を支えるテレメトリーパイプラインの構築 / Building Telemetry Pipeline with OpenTelemetry
ymotongpoo
9
4.4k
RevOpsへ至る道 データ活用による事業革新への挑戦 / path-to-revops
pei0804
2
570
サーバーレス環境における生成AI活用の可能性
mikanbox
1
170
srekaigi2025-hajimete-ippo-aws
masakichieng
0
180
ドメイン駆動設計によるdodaダイレクトのリビルド実践 / Rebuild practice of doda direct with domain-driven design
techtekt
0
470
ObservabilityCON on the Road Tokyoの見どころ
hamadakoji
0
130
大学教員が押さえておくべき生成 AI の基礎と活用例〜より効率的な教育のために〜
soh9834
1
170
Microsoft Ignite 2024 最新情報!Microsoft 365 Agents SDK 概要 / Microsoft Ignite 2024 latest news Microsoft 365 Agents SDK overview
karamem0
0
170
LLM活用の現在とこれから:LayerXにおける事例とともに 2025/1 ver. / layerx-llm-202501
yuya4
3
250
Featured
See All Featured
Side Projects
sachag
452
42k
For a Future-Friendly Web
brad_frost
176
9.5k
Measuring & Analyzing Core Web Vitals
bluesmoon
5
210
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
3
260
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
39
1.9k
Java REST API Framework Comparison - PWX 2021
mraible
28
8.3k
The Language of Interfaces
destraynor
156
24k
Reflections from 52 weeks, 52 projects
jeffersonlam
348
20k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
PRO
11
900
ReactJS: Keep Simple. Everything can be a component!
pedronauck
666
120k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
7
590
Principles of Awesome APIs and How to Build Them.
keavy
126
17k
Transcript
1 The Future of encoding/json Go Conference mini 2023 Winter
IN KYOTO Shunta Komatsu
2 About me • Shunta Komatsu • Backend Engineer at
Merpay ◦ Developing the payment platform • Likes ◦ Dogs ◦ Neovim
3 このセッションの目的 • 現在提案されている encoding/json/v2 について理解してもらう • 興味を持った方が discussion に参加することで
Go コミュニティに貢献する きっかけを作る
4 Agenda 1. encoding/json/v1 package の問題点 2. encoding/json/v2 package の紹介
3. パフォーマンスとマイグレーション 4. 議論の動向
5 encoding/json package 使ったことありますか?✋
6 encoding/json package encoding/json は 5 番目 に多く import されている
(らしい) Marshal (encode) Unmarshal (decode)
7 encoding/json package json パッケージができたのは 2008 年
8 encoding/json/v1 package の問題点
9 encoding/json/v1 package の問題点 1. 機能不足 2. API の欠陥 3.
パフォーマンスの制限 4. 動作上の欠陥
10 機能不足 • 92 個の encoding/json 関連の proposal や feature
request
11 機能不足 • 92 個の encoding/json 関連の proposal や feature
request
12 機能不足
13 機能不足 • 92 個の encoding/json 関連の proposal や feature
request
14 機能不足
15 API の欠陥 • io.Reader から unmarshal できない ◦ json.Unmarshal
は []byte を引数に取る ◦ io.Reader を入力するためには中間デコーダが必要 ▪ json.NewDecoder(r).Decode(v) ▪ 末尾の文字列を許容するなどの直感に反する動作 (#36225)
16 API の欠陥 • Marshal, Unmarshal や MarshalJSON, UnmarshalJSON は動作を設定
するオプションを受け付けない ◦ 動作を設定するためにはやはり Encoder, Decoder が必要
17 • MarshalJSON は []byte を返すので、その分のメモリをアロケートする必要 がある • MarshalJSON と
UnmarshalJSON は内部で 2 回テキストの解析を行うため 効率が悪い ◦ オブジェクトの型情報の収集 -> 値のパース • ストリーミングの欠如 ◦ Encoder.Encode と Decoder.Decode は io.Writer と io.Reader を引数に取るにも関わらず、JSON 値全体をメモリにバッファ する パフォーマンスの制限
18 動作上の欠陥 • RFC (RFC 8259) に定義されている JSON の仕様とのズレ ◦
invalid な UTF-8 が許容されている ◦ JSON object の key の重複が許容されている
19 動作上の欠陥 • MarshalJSON と UnmarshalJSON は reflection を使っているため、アド レスが取得できないと定義していても呼び出されない
• unmarshal は case-insensitive に行われる • 返されるエラーの型に一貫性がない ◦ 構造化されたエラーを返したり、構造化されていないエラーを返す
20 encoding/json/v2 package の紹介
21 encoding/json/v2 の思想と目標 • (ほぼ) 後方互換を保つ • より正確に • よりパフォーマンス良く
• より柔軟に • 使いやすく、誤用しにくい • unsafe package は利用しない
22 Overview https://github.com/golang/go/discussions/63397
23 encoding/json/jsontext package https://github.com/golang/go/discussions/63397
24 encoding/json/jsontext package • Encoder と Decoder は JSON token
と value の読み書きをする ◦ JSON テキストを純粋なストリーミング形式で処理する • NewEncoder と NewDecoder はオプションを受け付ける
25 • 可変長の Options 型によって動作の設定が可能 • Options interface は jsonopts
package に定義されている Options type https://github.com/go-json-experiment/json/blob/master/jsontext/options.go
26 SyntacticError type • JSON の文法に準拠していないことに起因するエラーは SyntacticError と して返される
27 encoding/json/v2 package https://github.com/golang/go/discussions/63397
28 encoding/json/v2 package • Marshal, Unmarshal ◦ v1 の Marshal
と Unmarshal とほぼ同じ • MarshalWrite, UnmarshalRead ◦ []byte の代わりに io.Writer と io.Reader を受け付ける • MarshalEncode, UnmarshalDecode ◦ *jsontext.Encoder と *jsontext.Decoder を受け付ける
29 https://github.com/golang/go/discussions/63397 デフォルトの挙動の変更
30 • key を case-sensitive に比較する デフォルトの挙動の変更
31 • invalid な UTF-8 を許容しない デフォルトの挙動の変更
32 • key の重複を許容しない デフォルトの挙動の変更
33 Struct tag options • v2 は以下の struct tag をサポート
◦ omitzero <- new! ◦ omitempty ◦ string ◦ nocase <- new! ◦ inline <- new! ◦ unknown <- new! ◦ format <- new!
34 • omitzero ◦ 値がゼロ値のときは marshal 時に省略される Struct tag options
35 • format ◦ serialize するときの format を指定できる Struct tag
options
36 Options type • 可変長の Options 型によって動作の設定が可能 • Options interface
は jsonopts package に定義されている https://github.com/go-json-experiment/json/blob/master/options.go
37 SemanticError type • Go と JSON 間で値を関連付けるときに発生するエラーは SemanticError として返される
38 encoding/json/v2 package https://github.com/golang/go/discussions/63397
39 パフォーマンスとマイグレーション
40 パフォーマンス https://github.com/go-json-experiment/jsonbench/blob/master/images/benchmark-marshal-concrete.png
41 パフォーマンス https://github.com/go-json-experiment/jsonbench/blob/master/images/benchmark-unmarshal-concrete.png
42 マイグレーション
43 議論の動向
44 golang/go で議論中
45 今すぐ参加できる discussion marshal 時に nil slice と nil map
を空の JSON array や object として扱う か、JSON null として扱うか
46 今すぐ参加できる discussion 新しい Options を提案することも可能
47 Summary
48 個人的な所感 • パフォーマンスや API の柔軟性、RFC への準拠による正確性に加えて、後方 互換性やマイグレーションにつても考慮されており、提案は全体的によく考えら れている •
これ以上パフォーマンスを上げる場合は 3rd party の JSON ライブラリによう にコード生成やアセンブリレベルでの最適化が必要 ◦ 標準パッケージがやるべきではない? • JSON の文法を処理する jsontext pacakge と Go の値と作用する json package に分けるアイデアは綺麗な設計
49 まとめ • encoding/json/v2 は v1 が持っている機能不足、API の欠陥、パフォーマ ンスの制限、動作の欠陥といった問題点を解決・向上する提案 •
jsontext と json の 2 つのパッケージに分けることで、”JSON の文法的な 処理” と “Go の値との相互作用を行う意味的な処理” を効率的に実装している • repository: https://github.com/go-json-experiment/json • discussion : https://github.com/golang/go/discussions/63397
50 References • “The Future of JSON in Go” by
Joe Tsai at GopherCon 2023 (link) • https://github.com/go-json-experiment/json • https://github.com/golang/go/discussions/63397
51 よい JSON ライフを!👋