Skip to content

Instantly share code, notes, and snippets.

@dotandimet
Created October 25, 2012 15:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dotandimet/3953299 to your computer and use it in GitHub Desktop.
Save dotandimet/3953299 to your computer and use it in GitHub Desktop.
tests to compare how JSON and Mojo::JSON generate true and false values in JSON
#!/usr/bin/env perl
use JSON;
use Mojo::JSON;
use Test::More tests => 6;
my $j = JSON->new;
my $mj = Mojo::JSON->new;
# JSON::true and JSON::false stringify to 'true' and 'false'
# OK:
is(attempt_encode($j, {a=>JSON::true,b=>JSON::false}), '{"a":true,"b":false}', 'JSON with JSON constants');
# Fail:
is(attempt_encode($mj, {a=>JSON::true,b=>JSON::false}), '{"a":true,"b":false}', 'Mojo::JSON with JSON constants');
# Mojo::JSON->true and Mojo::JSON->false stringify to 1 and 0:
# Fail - with exception!
is(attempt_encode($j, {a=>Mojo::JSON->true,b=>Mojo::JSON->false}), '{"a":true,"b":false}', 'JSON with Mojo::JSON constants');
# OK:
is(attempt_encode($mj, {a=>Mojo::JSON->true,b=>Mojo::JSON->false}), '{"a":true,"b":false}', 'Mojo::JSON with Mojo::JSON constants');
# \1 and \0 are converted internally to JSON::true and JSON::false by the JSON module:
# OK:
is(attempt_encode($j, {a=>\1,b=>\0}), '{"a":true,"b":false}', 'JSON with JSON constant aliases (\1 and \0)');
# Fail - I would like this one to pass.
is(attempt_encode($mj, {a=>\1,b=>\0}), '{"a":true,"b":false}', 'Mojo::JSON with JSON constant aliases (\1 and \0)');
# because JSON throws an exception in one of the tests:
sub attempt_encode {
my ($obj, $hashref) = @_;
my $out = '';
eval {
$out = $obj->encode($hashref);
};
if ($@) {
$out = "EXCEPTION: " . $@;
}
return $out;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment