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
880
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
2
550
Recap: Automatically Instrument Your Go Source Code with Orchestrion
iamshunta
0
590
Recap: The Future of JSON in Go
iamshunta
0
1.2k
Other Decks in Technology
See All in Technology
Java 30周年記念! Javaの30年をふりかえる
skrb
1
400
NW運用の工夫と発明
recuraki
1
790
ソフトウェアは捨てやすく作ろう/Let's make software easy to discard
sanogemaru
10
5.8k
セキュリティSaaS企業が実践するCursor運用ルールと知見 / How a Security SaaS Company Runs Cursor: Rules & Insights
tetsuzawa
0
420
名刺メーカーDevグループ 紹介資料
sansan33
PRO
0
740
Oracle Base Database Service 技術詳細
oracle4engineer
PRO
8
65k
GigaViewerにおけるMackerel APM導入の裏側
7474
0
460
Contract One Dev Group 紹介資料
sansan33
PRO
0
6k
“⾞が通れるほど⼤きな”セキュリティーホールを抑えながらログインしたい
taiseiue
0
160
Java で学ぶ 代数的データ型
ysknsid25
1
360
MCP Clientを活用するための設計と実装上の工夫
yudai00
1
810
Azure Developer CLI と Azure Deployment Environment / Azure Developer CLI and Azure Deployment Environment
nnstt1
1
130
Featured
See All Featured
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
280
13k
What's in a price? How to price your products and services
michaelherold
245
12k
Music & Morning Musume
bryan
47
6.6k
Building an army of robots
kneath
306
45k
The Language of Interfaces
destraynor
158
25k
The Straight Up "How To Draw Better" Workshop
denniskardys
233
140k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
194
16k
The Art of Programming - Codeland 2020
erikaheidi
54
13k
A Tale of Four Properties
chriscoyier
159
23k
Raft: Consensus for Rubyists
vanstee
137
7k
Reflections from 52 weeks, 52 projects
jeffersonlam
349
20k
Fantastic passwords and where to find them - at NoRuKo
philnash
51
3.2k
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 ライフを!👋