Skip to content

Provide type support when decoding JSON string #115

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 19, 2018
Merged

Conversation

pali
Copy link
Contributor

@pali pali commented Aug 13, 2018

JSON decoder fills optional third argument of Cpanel::JSON::XS::decode_json function and optional second argument of ->decode method with original JSON types from input string.

Examples:

my $value = decode_json('false', 1, my $type);
# $value is 0 and $type is JSON_TYPE_BOOL

my $value = decode_json('0', 1, my $type);
# $value is 0 and $type is JSON_TYPE_INT

my $value = decode_json('"0"', 1, my $type);
# $value is 0 and $type is JSON_TYPE_STRING

my $struct = Cpanel::JSON::XS->new->decode('[null,1,1.1,"1",[0],true]', my $type);
# $struct is [undef, 1, 1.1, '1', [0], 1]
# $value is [JSON_TYPE_NULL, JSON_TYPE_INT, JSON_TYPE_FLOAT, JSON_TYPE_STRING, [JSON_TYPE_INT], JSON_TYPE_BOOL]

This allows users to correctly distinguish between booleans, integers, floats and strings in Perl language where perl interpreter can coerce between these types at any time.

@rurban
Copy link
Owner

rurban commented Aug 13, 2018

Can you also the missing JSON_TYPE_NULL pod? pod_coverage complains
Other than that looks good.

@rurban rurban self-assigned this Aug 13, 2018
@rurban rurban self-requested a review August 13, 2018 15:43
Copy link
Owner

@rurban rurban left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JSON_TYPE_NULL missing pod

JSON decoder fills optional third argument of Cpanel::JSON::XS::decode_json
function and optional second argument of ->decode method with original JSON
types from input string.

Examples:

my $value = decode_json('false', 1, my $type);
 $value is 0 and $type is JSON_TYPE_BOOL

my $value = decode_json('0', 1, my $type);
 $value is 0 and $type is JSON_TYPE_INT

my $value = decode_json('"0"', 1, my $type);
 $value is 0 and $type is JSON_TYPE_STRING

my $struct = Cpanel::JSON::XS->new->decode('[null,1,1.1,"1",[0],true]', my $type);
 $struct is [undef, 1, 1.1, '1', [0], 1]
 $value is [JSON_TYPE_NULL, JSON_TYPE_INT, JSON_TYPE_FLOAT, JSON_TYPE_STRING, [JSON_TYPE_INT], JSON_TYPE_BOOL]

This allows users to correctly distinguish between booleans, integers,
floats and strings in Perl language where perl interpreter can coerce
between these types at any time.
@pali
Copy link
Contributor Author

pali commented Aug 13, 2018

Now I extended POD documentation.

@rurban
Copy link
Owner

rurban commented Aug 13, 2018

Great. But I cannot release this before my holidays, starting tommorow. I believe I'll have no wifi in the French Alps

@rurban
Copy link
Owner

rurban commented Aug 19, 2018

Released 4.05 without WiFi, only Phone hotspot. Thanks for the Glasgow talk!

@rurban rurban merged commit 2019473 into rurban:master Aug 19, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants