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
PerlとJSON / Perl and JSON
Search
papix
May 24, 2019
Technology
1
1.1k
PerlとJSON / Perl and JSON
papix
May 24, 2019
Tweet
Share
More Decks by papix
See All by papix
Houtou.pm #1
papix
0
1.2k
Perl歴約10年のエンジニアがフルスタックTypeScriptに出会ってみた
papix
1
550
YAPC::Kyotoの「全て」 / All of "YAPC::Kyoto"
papix
0
1.5k
イベントの中の人 / Inside the Events
papix
0
290
2022年に始めるPerlでWebサービス開発(趣味)
papix
0
550
ワーケーションに関する考察
papix
3
2.2k
(今更)Amplifyさっくり体験
papix
0
860
はてなにおけるGitHub Actions活用事例 / GitHub Actions in Hatena
papix
0
2.4k
ミススペルを発見するmisspellのご紹介 / Introduce misspell
papix
0
1.2k
Other Decks in Technology
See All in Technology
AI人生苦節10年で会得したAIがやること_人間がやること.pdf
shibuiwilliam
1
200
Ktor + Google Cloud Tasks/PubSub におけるOTel Messaging計装の実践
sansantech
PRO
1
330
地域コミュニティへの「感謝」と「恩返し」 / 20250726jawsug-tochigi
kasacchiful
0
100
MCPと認可まわりの話 / mcp_and_authorization
convto
2
280
OpenTelemetry の Log を使いこなそう
biwashi
5
1.1k
alecthomas/kong はいいぞ
fujiwara3
6
1.1k
Shadow DOM & Security - Exploring the boundary between light and shadow
masatokinugawa
0
750
Recoil脱却の現状と挑戦
kirik
3
460
Power Automate のパフォーマンス改善レシピ / Power Automate Performance Improvement Recipes
karamem0
0
270
Microsoft Learn MCP/Fabric データエージェント/Fabric MCP/Copilot Studio-簡単・便利なAIエージェント作ってみた
reireireijinjin6
1
120
AI駆動開発 with MixLeap Study【大阪支部 #3】
lycorptech_jp
PRO
0
270
From Live Coding to Vibe Coding with Firebase Studio
firebasethailand
1
300
Featured
See All Featured
How to Think Like a Performance Engineer
csswizardry
25
1.8k
Measuring & Analyzing Core Web Vitals
bluesmoon
7
530
Balancing Empowerment & Direction
lara
1
510
Making the Leap to Tech Lead
cromwellryan
134
9.4k
Building a Modern Day E-commerce SEO Strategy
aleyda
42
7.4k
Scaling GitHub
holman
461
140k
How to train your dragon (web standard)
notwaldorf
96
6.1k
Connecting the Dots Between Site Speed, User Experience & Your Business [WebExpo 2025]
tammyeverts
8
380
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
29
2.8k
Keith and Marios Guide to Fast Websites
keithpitt
411
22k
Statistics for Hackers
jakevdp
799
220k
Optimizing for Happiness
mojombo
379
70k
Transcript
PerlとJSON id:papix (@__papix__) 株式会社はてな
papix 株式会社はてな アプリケーションエンジニア (2017年2月~) シニアエンジニア (2019年2月~) ブログMediaチーム サービスリード兼スクラムマスター 「はてなブログ」をつくっています アカウント類
はてな: id:papix Twitter: @__papix__ GitHub: papix / CPAN: PAPIX ブログ: https://papix.hatena(blog.(com|jp)|diary.jp)/ 趣味はPerlと, (交通機関を利用した)旅行 JGC修行は完遂済み, 今年はSFC修行をしています
JSON JavaScript Object Notation JavaScript Object Notation (JSON 、ジェイソン)は軽量な データ記述言語の1
つである。構文はJavaScript におけるオブジ ェクトの表記法をベースとしているが、JSON はJavaScript 専用の データ形式では決してなく、様々なソフトウェアやプログラミング 言語間におけるデータの受け渡しに使えるよう設計されている。 by Wikipedia Perlでも, JSONを扱うライブラリが用意されている
Perl界の(代表的な)3つのJSON JSON::PP JSON::XS JSON
JSON::PP PP(Pure Perl)実装のJSONモジュール v5.13.9よりコアモジュール perl コマンド単体で動かしたいスクリプトならJSON::PPを 使っておくと良い
JSON::XS XSを使ったJSONの実装 XSを使って, C言語のコードやライブラリとPerlを繋いで実装 しているので(Pure Perlよりも)高速 cpanfile などに書いていて, JSON::XSがある環境なら明示的に JSON::XSを使えばよい
コアモジュールではない
JSON JSON::XSがあればそれを, なければJSON::PPを使ってくれる モジュールなどで, よしなに使わけて欲しい時はJSONを使って おけばよい コアモジュールではない
まだまだあるJSONファミリー Cpanel::JSON::XS Cpanelという会社が実装した, XSを使ったJSON JSON::MaybeXS Cpanel::JSON::XSとJSON::XSとJSON::PPをよしなに使って くれる
JSONことはじめ JSON::XSを例に, Perlで実際にJSONを扱う例を紹介していきます
PerlのHash/ArrayリファレンスをJSONにする use JSON::XS qw(encode_json); my $json = encode_json({ a =>
1, b => 2 }); print $json; # => {"a":1,"b":2}
JSONをPerlのデータ構造にする use JSON::XS qw(decode_json); use Data::Dumper; my $ref = decode_json('{"a":1,"b":2}');
print Dumper $ref; # $VAR1 = { # 'a' => 1, # 'b' => 2 # };
ちなみに... decode_json は, JSON::XS->new->utf8->decode と同じ encode_json は, JSON::XS->new->utf8->encode と同じ use
JSON::XS; my $json = JSON::XS->new->utf8->encode( { a => 1, b => 2, c => 3 } );
こういう時はどうする?
JSONにする時, Hash Randomizationを回避したい Perl 5.18以降, Hash Randomizationが導入された PerlのHash/ArrayリファレンスをJSONにするとき, キーの順序 が不順になることがある
use JSON::XS qw(encode_json); my $json = encode_json({ a => 1, b => 2, c => 3 }); print "$json\n"; $ perl json.pl {"c":3,"a":1,"b":2} $ perl json.pl {"a":1,"c":3,"b":2} $ perl json.pl {"b":2,"c":3,"a":1}
JSONにする時, Hash Randomizationを回避したい 解決策としては, Canonicalモードを使う キーをソートした上でJSONにしてくれる use JSON::XS; my $json
= JSON::XS->new->utf8->canonical->encode( { a => 1, b => 2, c => 3 } ); print "$json\n"; # {"a":1,"b":2,"c":3}
JSONにするとき, オブジェクトが含まれていても無視したい PerlのHash/ArrayリファレンスをJSONにするとき, 通常オブジェク トが含まれると例外になる encountered object 'Obj=HASH(0x7ff069003418)', but neither
allow_blessed, convert_blessed nor allow_tags settings are enabled (or TO_JSON/FREEZE method missing) use JSON::XS qw(encode_json); my $hash = { a => 1, b => Obj->new, }; my $json = encode_json($hash); # XXX
JSONにするとき, オブジェクトが含まれていても無視したい allow_blessed をOnにすると, オブジェクトは無視して null に してくれる use JSON::XS
qw(encode_json); my $hash = { a => 1, b => Obj->new, }; my $json = JSON::XS->new->utf8->allow_blessed->encode( $hash ); print $json; # => {"a":1,"b":null}
JSONにするとき, オブジェクトもよしなにJSONにしたい オブジェクト側に TO_JSON メソッドを用意しておけば, オブジェク トの TO_JSON の返り値でJSONを作ってくれる use
JSON::XS; my $hash = { a => 'b', c => Obj->new, }; my $json = encode_json($hash); print $json; # => {"a":"b","c":{"cc":11}} package Obj; sub new { bless {}, $_[0] } sub TO_JSON { {cc => 11} }
JSONにするとき, 型を明示したい JSON::Typesを使いましょう use JSON::XS; use JSON::Types; my $hash =
{ number => JSON::Types::number 1, string => JSON::Types::string 1, bool => JSON::Types::bool 1, }; my $json = encode_json($hash); print $json; # {"number":1,"string":"1","bool":true}
JSON::Types 実装は素朴 sub number($) { return undef unless defined $_[0];
$_[0] + 0; } sub string($) { return undef unless defined $_[0]; $_[0] . ''; } sub bool($) { $_[0] ? \1 : \0; }
ちなみに... booleanについては, (Perlに存在しない概念なので)decodeするとき にどうなるかはライブラリごとに決まっている 例えば, JSON::XSなら, true は $Types::Serialiser::true に,
false は $Types::Serialiser::false にデコードされる 更に, boolean_values を使って, JSONの true / false をデ コードするときの値を上書きすることができる
まとめ PerlにはJSONを扱うための道具がいろいろある そしてJSONをよしなに扱うテクニックがいろいろある PerlとJSONとうまくつきあっていきましょう