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
3
910
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.6k
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
United™️ Airlines®️ Customer®️ USA Contact Numbers: Complete 2025 Support Guide
flyunitedguide
0
720
60以上のプロダクトを持つ組織における開発者体験向上への取り組み - チームAPIとBackstageで構築する組織の可視化基盤 - / sre next 2025 Efforts to Improve Developer Experience in an Organization with Over 60 Products
vtryo
2
630
【LT会登壇資料】TROCCO新コネクタ「スマレジ」を活用した直営店データの分析
kazari0425
1
140
助けて! XからWaylandに移行しないと新しいGNOMEが使えなくなっちゃう 2025-07-12
nobutomurata
2
130
VGGT: Visual Geometry Grounded Transformer
peisuke
1
550
スタートアップに選択肢を 〜生成AIを活用したセカンダリー事業への挑戦〜
nstock
0
270
データ基盤からデータベースまで?広がるユースケースのDatabricksについて教えるよ!
akuwano
3
150
[SRE NEXT] ARR150億円_エンジニア140名_27チーム_17プロダクトから始めるSLO.pdf
satos
3
1.7k
DBのスキルで生き残る技術 - AI時代におけるテーブル設計の勘所
soudai
PRO
54
22k
[ JAWS-UG千葉支部 x 彩の国埼玉支部 ]ムダ遣い卒業!FinOpsで始めるAWSコスト最適化の第一歩
sh_fk2
2
150
ソフトウェアテストのAI活用_ver1.25
fumisuke
1
510
関数型プログラミングで 「脳がバグる」を乗り越える
manabeai
2
220
Featured
See All Featured
Building Applications with DynamoDB
mza
95
6.5k
A designer walks into a library…
pauljervisheath
207
24k
How to train your dragon (web standard)
notwaldorf
96
6.1k
Java REST API Framework Comparison - PWX 2021
mraible
31
8.7k
Fashionably flexible responsive web design (full day workshop)
malarkey
407
66k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
10
970
The Art of Programming - Codeland 2020
erikaheidi
54
13k
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
8
700
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
331
22k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
34
3.1k
Statistics for Hackers
jakevdp
799
220k
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
18
980
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 ライフを!👋