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
840
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
540
Recap: The Future of JSON in Go
iamshunta
0
1.1k
Other Decks in Technology
See All in Technology
OPENLOGI Company Profile for engineer
hr01
1
20k
EMConf JP 2025 懇親会LT / EMConf JP 2025 social gathering
sugamasao
2
180
ウォンテッドリーのデータパイプラインを支える ETL のための analytics, rds-exporter / analytics, rds-exporter for ETL to support Wantedly's data pipeline
unblee
0
110
IAMポリシーのAllow/Denyについて、改めて理解する
smt7174
2
190
Exadata Database Service on Cloud@Customer セキュリティ、ネットワーク、および管理について
oracle4engineer
PRO
2
1.5k
ESXi で仮想化した ARM 環境で LLM を動作させてみるぞ
unnowataru
0
160
Snowflakeの開発・運用コストをApache Icebergで効率化しよう!~機能と活用例のご紹介~
sagara
1
360
AWSアカウントのセキュリティ自動化、どこまで進める? 最適な設計と実践ポイント
yuobayashi
5
220
Helm , Kustomize に代わる !? 次世代 k8s パッケージマネージャー Glasskube 入門 / glasskube-entry
parupappa2929
0
290
2025-02-21 ゆるSRE勉強会 Enhancing SRE Using AI
yoshiiryo1
1
470
AIエージェント元年
shukob
0
150
What's new in Go 1.24?
ciarana
1
100
Featured
See All Featured
Code Reviewing Like a Champion
maltzj
521
39k
Testing 201, or: Great Expectations
jmmastey
42
7.2k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
233
17k
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
4
430
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
33
2.1k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
33
2.8k
The MySQL Ecosystem @ GitHub 2015
samlambert
250
12k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
356
29k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
45
9.4k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
100
18k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
28
9.3k
StorybookのUI Testing Handbookを読んだ
zakiyama
28
5.5k
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 ライフを!👋