diff options
Diffstat (limited to 'Master/tlpkg/tlperl/site/lib/Cpanel/JSON/XS/Type.pm')
-rwxr-xr-x | Master/tlpkg/tlperl/site/lib/Cpanel/JSON/XS/Type.pm | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/Master/tlpkg/tlperl/site/lib/Cpanel/JSON/XS/Type.pm b/Master/tlpkg/tlperl/site/lib/Cpanel/JSON/XS/Type.pm index 9367f1b4fa8..4d70bfd9704 100755 --- a/Master/tlpkg/tlperl/site/lib/Cpanel/JSON/XS/Type.pm +++ b/Master/tlpkg/tlperl/site/lib/Cpanel/JSON/XS/Type.pm @@ -83,6 +83,10 @@ does not work reliable for dual vars and scalars which were used in both numeric and string operations. See L<simple scalars|Cpanel::JSON::XS/simple scalars>. +To enforce that specification is always provided use C<require_types>. +In this case when C<encode> is called without second argument (or is +undef) then it croaks. It applies recursively for all sub-structures. + =head2 JSON type specification for scalars: =over 4 @@ -170,6 +174,12 @@ JSON encoder chooses one that matches. Like L<C<json_type_anyof>|/json_type_anyof>, but scalar can be only perl's C<undef>. +=back + +=head2 Recursive specifications + +=over 4 + =item json_type_weaken This function can be used as an argument for L</json_type_arrayof>, @@ -187,6 +197,25 @@ See following example: json_type_arrayof(JSON_TYPE_STRING), ); +If you want to encode all perl scalars to JSON string types despite +how complicated is input perl structure you can define JSON type +specification for alternatives recursively. It could be defined as: + + my $type = json_type_anyof(); + $type->[0] = JSON_TYPE_STRING_OR_NULL; + $type->[1] = json_type_arrayof(json_type_weaken($type)); + $type->[2] = json_type_hashof(json_type_weaken($type)); + + print encode_json([ 10, "10", { key => 10 } ], $type); + # ["10","10",{"key":"10"}] + +An alternative solution for encoding all scalars to JSON strings is to +use C<type_all_string> method of L<Cpanel::JSON::XS> itself: + + my $json = Cpanel::JSON::XS->new->type_all_string; + print $json->encode([ 10, "10", { key => 10 } ]); + # ["10","10",{"key":"10"}] + =back =head1 AUTHOR |