Skip to content
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

Provide type support when decoding JSON string #115

Merged
merged 1 commit into from Aug 19, 2018
Merged

Conversation

@pali
Copy link
Contributor

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

This comment has been minimized.

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 added the enhancement label Aug 13, 2018
@rurban rurban self-requested a review Aug 13, 2018
Copy link
Owner

rurban left a comment

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 pali force-pushed the pali:master branch from c0423f0 to 2019473 Aug 13, 2018
@pali

This comment has been minimized.

Copy link
Contributor Author

pali commented Aug 13, 2018

Now I extended POD documentation.

@rurban

This comment has been minimized.

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
rurban approved these changes Aug 19, 2018
@rurban

This comment has been minimized.

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
3 checks passed
3 checks passed
continuous-integration/appveyor/pr AppVeyor build succeeded
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
linthub Linthub has cleared the pull request, no code style suggestions found.
Details
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.