JSON::Types(3pm)
π¦ NAME π¦
JSON::Types β it's the variable type utility for JSON encoding, bestie π
β¨ SYNOPSIS β¨
okay so first way: the type functions just show up by default, no ask, they're in the group chat already π«Ά
# Export type functions by default
use JSON;
use JSON::Types;
print encode_json({
number => number "123",
string => string 123,
bool => bool "True value",
});
# => {"number":123,"string":"123","bool":true}
and if you're the kind of girl who wants nothing auto-imported, we love a boundaries queen π you can just go through the full package name instead:
# Non export interface
use JSON::Types ();
print encode_json({
number => JSON::Types::number "123",
string => JSON::Types::string 123,
bool => JSON::Types::bool "True value",
});
π DESCRIPTION π
so the type mappings between JSON and Perl? annoying. not like cute-annoying. like actually annoying π
here's the situation:
use JSON;
my $number = 123;
warn "[DEBUG] number:$number\n" if $ENV{DEBUG};
print encode_json([ $number ]);
the output of this code depends on whether the DEBUG environment variable is set or not. if it is set, the result is "[123]". if it is not set, the result is "["123"]". so you've got one variable, two different types, and your JSON is out here living a double life π
that's normal behaviour on Perl though β no shade to Perl, it's just how she is β but sometimes it causes unexpected JSON results. and unexpected JSON results are NOT it.
there is a solution for this:
print encode_json([ $number + 0 ]);
this code always outputs "[123]". but like... the code is a bit ugly and not readable at all. it's giving cryptic. it's giving what does the + 0 mean, mom.
so this module provides some functions to fix this variable types issue:
number $foo; # is always number
string $foo; # is always string
bool $foo; # is always bool
and then you can fix the above code by using this module like this, slay:
use JSON;
use JSON::Types;
my $number = 123;
warn "[DEBUG] number:$number\n" if $ENV{DEBUG};
print encode_json([ number $number ]);
πΈ FUNCTIONS πΈ
there is three functions and all functions is exported by default. three of them. they come as a set. iconic behaviour honestly π
if you don't want this exported functions, pass empty list to use line:
use JSON::Types ();
you should specify full function name when this case, like JSON::Types::number $foo or etc.
β string
β number
β bool
π BEHAVIOURS ON UNEXPECTED ARGS π
π string(undef), number(undef) returns undef, bool(undef) returns false.
passing undefined variable to string and number function is returns undef. if you doesn't prefer this, have to treat this like following:
number $undef_possible_value // 0
this code returns 0 if variable is undef. problem solved, no cap π«Ά
π number($string)
passing not numeric variable to number function is returns 0, but a warning will be occurred. so it's not silent about it β it will tell on you. consider yourself warned, bestie π
π AUTHOR π
Daisuke Murase <typester@cpan.org>
π COPYRIGHT AND LICENSE π
Copyright (c) 2012 Daisuke Murase. All rights reserved.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
The full text of the license can be found in the LICENSE file included with this module. read it, it's good for you π
okay that's the whole page, you're all caught up, and this has been JSON::Types from the User Contributed Perl Documentation for perl v5.40.1, dated 2025-08-04 β mwah, go encode something cute π¦π
β¨
π¦ where this page comes from
- ubuntu resolute: libjson-types-perl 0.05-5 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libjson-types-perl 0.05-5 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms