1. Perl5 VS JSON
The Perl Conference 2019 in Pittsburgh - Lightning Talks
Kenta SATO (a.k.a. @karupanerura)
2. Kenta SATO a.k.a. @karupanerura
• PAUSE ID: KARUPA
• TOML::Parser, Time::Strptime, DBD::BlackHole, etc..
• Japan Perl Association - 3rd Representative Director
• Shibuya.pm - Leader
• DeNA, Co,. LTD. - Software Engineer
3. tl;dr
• JSON is de facto standard serialization format for IPC in today
• JSON data type is important for almost other languages
• Typed data schema is great solution for JSON encoding
• JSON::Types is match for simple cases
• Cpanel::JSON::XS::Type is match for almost cases
5. Modern Web Application Development
• JavaScript side DOM rendering
• e.g.) React.js, Vue.js, etc..
• Minimam HTML rendering on Server-side application
• Almost data is provided by JSON Web API.
6. JSON is used for many many cases
• Authentication/Authorization
• JWT(JWS), OIDC, etc..
• Mobile (Android/iOS) applications
• (Internal/External) Web APIs
• microservices, customer’s application, etc..
7. JSON is a de facto standard
serialization format for IPC today
21. Problem: SV flags is changed implicitly
• Too easy to change SV flags implicitly
# Example
my $x = 1; # IV (JSON encoding as number)
print "DEBUG: x = $xn"; # <-- Creates PV slot implicitly!
$x; # PVIV (JSON encoding as string)
22. Problem: JSON typing is important
// Java Script Example
const response = JSON.parse(`{"expiresTime":"3600"}`);
const expiresTime = response.expiresTime;
const nowEpoch = Math.floor(Date.now() / 1000);
const expiresAt = expiresTime + nowEpoch; // concat
console.log(expiresAt); // 3600XXXXXXXX
Strictly typing is serious problem for almost other languages.
Almost static typing languages throw error if type mismatch.
Almost dynamic typing languages change some behaviours
by the data type.
25. JSON::Types
• Type annotation likes SV converter for JSON::(PP|XS)
use JSON::XS qw/encode_json/;
use JSON::Types;
my $json = encode_json([bool 0, number 1, string 2]);
say $json; # => [false,1,"2"]
27. Bad: JSON::Types
• SHOULD use it ONLY just before encoding
• Because, the converted SVs can be re-changed
• By after processes
• No converter from JSON Schema / OpenAPI (Swagger) / etc
28. Cpanel::JSON::XS::Type
• Typed data structure schema for Cpanel::JSON::XS
use Cpanel::JSON::XS;
use Cpanel::JSON::XS::Type;
my $schema =
[JSON_TYPE_BOOL, JSON_TYPE_INT, JSON_TYPE_STRING];
my $json = encode_json([0, 1, 2], $schema);
say $json; # => [false,1,"2"]
32. Conclution
• JSON is de facto standard serialization format for IPC today
• JSON data type is important for almost other languages
• Typed data schema is great solution for JSON encoding
• JSON::Types is match for simple cases
• Cpanel::JSON::XS::Type is match for almost cases
33. Thank you
Executable example codes in the slide
https://gist.github.com/karupanerura/223b2d862f0c0c966e706e7062cb8cc9