-
Notifications
You must be signed in to change notification settings - Fork 8
Description
Hello!
In some cases it is needed to distinguish in YAML between number stored as string literals (number in single or double quotes) and number stored as number (without quotes).
Similar thing is needed also in JSON and e.g. Cpanel::JSON::XS encoder/decoder provides additional argument for passing types (when encoding) or getting types (when decoding). Documentation is written at: https://metacpan.org/pod/Cpanel::JSON::XS::Type
JSON example of usage:
use Cpanel::JSON::XS;
use Cpanel::JSON::XS::Type;
my $json_string = '{"key1":{"key2":[10,"10",10.6]},"key3":"10.5"}';
my $perl_struct = decode_json($json_string, 0, my $type_spec);
# $perl_struct is { key1 => { key2 => [ 10, 10, 10.6 ] }, key3 => 10.5 }
# $type_spec is { key1 => { key2 => [ JSON_TYPE_INT, JSON_TYPE_STRING, JSON_TYPE_FLOAT ] }, key3 => JSON_TYPE_STRING }
use Cpanel::JSON::XS;
use Cpanel::JSON::XS::Type;
encode_json([10, 10, 10.25], [JSON_TYPE_STRING, JSON_TYPE_INT, JSON_TYPE_STRING]);
# '["10",10,"10.25"]'
Via YAML::PP I can load e.g. following example
use YAML::PP qw(Load);
my $perl_struct = Load(<<EOF);
array:
- "10000000000000000000"
- 10000000000000000000
EOF
And I would like to get information what are types of the key array (like in above JSON example). So e.g. that first element is string and second element is a number.
It is possible to provide some kind of support for types when loading and dumping YAMLs via YAML::PP?