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
890
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
Cursor Meetup Tokyo
iamshunta
5
1.5k
Recap: Automatically Instrument Your Go Source Code with Orchestrion
iamshunta
0
600
Recap: The Future of JSON in Go
iamshunta
0
1.2k
Other Decks in Technology
See All in Technology
AIエージェントの継続的改善のためオブザーバビリティ
pharma_x_tech
6
1.4k
初めてのAzure FunctionsをClaude Codeで作ってみた / My first Azure Functions using Claude Code
hideakiaoyagi
1
180
TerraformをSaaSで使うとAzureの運用がこんなに楽ちん!HCP Terraformって何?
mnakabayashi
0
300
25分で解説する「最小権限の原則」を実現するための AWS「ポリシー」大全 / 20250625-aws-summit-aws-policy
opelab
6
680
Perk アプリの技術選定とリリースから1年弱経ってのふりかえり
stomk
0
120
Snowflake Summit 2025 データエンジニアリング関連新機能紹介 / Snowflake Summit 2025 What's New about Data Engineering
tiltmax3
0
220
キャディでのApache Iceberg, Trino採用事例 -Apache Iceberg and Trino Usecase in CADDi--
caddi_eng
0
170
新卒3年目の後悔〜機械学習モデルジョブの運用を頑張った話〜
kameitomohiro
0
370
原則から考える保守しやすいComposable関数設計
moriatsushi
3
500
SFTPコンテナからファイルをダウンロードする
dip_tech
PRO
0
580
ローカルLLMでファインチューニング
knishioka
0
120
AWS CDK 実践的アプローチ N選 / aws-cdk-practical-approaches
gotok365
4
460
Featured
See All Featured
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
PRO
20
1.3k
It's Worth the Effort
3n
184
28k
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
15
1.5k
A Tale of Four Properties
chriscoyier
159
23k
The Language of Interfaces
destraynor
158
25k
VelocityConf: Rendering Performance Case Studies
addyosmani
330
24k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
31
2.4k
No one is an island. Learnings from fostering a developers community.
thoeni
21
3.3k
GitHub's CSS Performance
jonrohan
1031
460k
How GitHub (no longer) Works
holman
314
140k
Raft: Consensus for Rubyists
vanstee
140
7k
Documentation Writing (for coders)
carmenintech
71
4.9k
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 ライフを!👋