Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Perl5 VS JSON

373 views

Published on

The Perl Conference 2019 in Pittsburgh
Lightning Talks

Published in: Engineering
  • Be the first to comment

Perl5 VS JSON

  1. 1. Perl5 VS JSON The Perl Conference 2019 in Pittsburgh - Lightning Talks Kenta SATO (a.k.a. @karupanerura)
  2. 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. 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
  4. 4. Background
  5. 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. 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. 7. JSON is a de facto standard serialization format for IPC today
  8. 8. Perl5 can encode/decode JSON! JSON::(PP|XS) Cpanel::JSON::(PP|XS)
  9. 9. No problem!
  10. 10. Really?
  11. 11. JSON encoding
  12. 12. JSON value has a type
  13. 13. Perl5 value can be 3 container types: Scalar(SV), Array(AV), Hash(HV)
  14. 14. JSON value can be 6 data types: null, boolean, number, string, array, object
  15. 15. How to translate to JSON's data type from Perl's container type?
  16. 16. Perl5 to JSON • Hash -> object • Array -> array • Scalar(undef) -> null • Scalar(special boolean blessed object) -> boolean • Scalar -> number OR string
  17. 17. Perl5 to JSON • Hash -> object • Array -> array • Scalar(undef) -> null • Scalar(special boolean blessed object) -> boolean • Scalar -> number OR string What’s going on?
  18. 18. Scalar to JSON (string|number) • Looking internal SV flags • Basic examples: • PV(Literal: ‘a’) -> “a” (string) • IV(Literal: 1) -> 1 (number) • PVIV(Literal: ‘1’) -> “1” (string)
  19. 19. Looks like good for almost cases!
  20. 20. Really…?
  21. 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. 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.
  23. 23. It’s too messy…
  24. 24. Solutions
  25. 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"]
  26. 26. Good: JSON::Types • Very very simple implementation • Easy to use it :)
  27. 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. 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"]
  29. 29. Good: Cpanel::JSON::XS::Type • Never implicitly change the JSON data type • Don’t look SV flags • Explicitly type control
  30. 30. Bad: Cpanel::JSON::XS::Type • No converter from JSON Schema / OpenAPI (Swagger) / etc • Or exists...? (Maybe I don't know yet?)
  31. 31. Conclution
  32. 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. 33. Thank you Executable example codes in the slide https://gist.github.com/karupanerura/223b2d862f0c0c966e706e7062cb8cc9

×