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
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
papix
May 24, 2019
Technology
1.3k
1
Share
PerlとJSON / Perl and JSON
papix
May 24, 2019
More Decks by papix
See All by papix
Houtou.pm #1
papix
0
1.8k
Perl歴約10年のエンジニアがフルスタックTypeScriptに出会ってみた
papix
1
720
YAPC::Kyotoの「全て」 / All of "YAPC::Kyoto"
papix
0
1.6k
イベントの中の人 / Inside the Events
papix
0
340
2022年に始めるPerlでWebサービス開発(趣味)
papix
0
610
ワーケーションに関する考察
papix
3
2.3k
(今更)Amplifyさっくり体験
papix
0
910
はてなにおけるGitHub Actions活用事例 / GitHub Actions in Hatena
papix
0
2.7k
ミススペルを発見するmisspellのご紹介 / Introduce misspell
papix
0
1.2k
Other Decks in Technology
See All in Technology
昔話で振り返るAWSの歩み ~S3誕生から20年、クラウドはどう進化したのか~
nrinetcom
PRO
0
120
来期の評価で変えようと思っていること 〜AI時代に変わること・変わらないこと〜
estie
0
130
AI時代のシステム開発者の仕事_20260328
sengtor
0
320
「活動」は激変する。「ベース」は変わらない ~ 4つの軸で捉える_AI時代ソフトウェア開発マネジメント
sentokun
0
140
TUNA Camp 2026 京都Stage ヒューリスティックアルゴリズム入門
terryu16
0
650
Databricks Appsで実現する社内向けAIアプリ開発の効率化
r_miura
0
160
AgentCoreとLINEを使った飲食店おすすめアプリを作ってみた
yakumo
2
270
FlutterでPiP再生を実装した話
s9a17
0
240
【AWS】CloudTrail LakeとCloudWatch Logs Insightsの使い分け方針
tsurunosd
0
130
The essence of decision-making lies in primary data
kaminashi
0
190
Navigation APIと見るSvelteKitのWeb標準志向
yamanoku
2
130
Oracle AI Database@Google Cloud:サービス概要のご紹介
oracle4engineer
PRO
5
1.3k
Featured
See All Featured
技術選定の審美眼(2025年版) / Understanding the Spiral of Technologies 2025 edition
twada
PRO
118
110k
The Curious Case for Waylosing
cassininazir
0
280
Agile Actions for Facilitating Distributed Teams - ADO2019
mkilby
0
160
Tell your own story through comics
letsgokoyo
1
880
The Language of Interfaces
destraynor
162
26k
SEOcharity - Dark patterns in SEO and UX: How to avoid them and build a more ethical web
sarafernandez
0
160
What the history of the web can teach us about the future of AI
inesmontani
PRO
1
500
Effective software design: The role of men in debugging patriarchy in IT @ Voxxed Days AMS
baasie
0
270
Automating Front-end Workflow
addyosmani
1370
200k
Stop Working from a Prison Cell
hatefulcrawdad
274
21k
Why Your Marketing Sucks and What You Can Do About It - Sophie Logan
marketingsoph
0
120
Documentation Writing (for coders)
carmenintech
77
5.3k
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とうまくつきあっていきましょう