Skip to content

Instantly share code, notes, and snippets.

/json.diff Secret

Created October 4, 2014 01:32
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 anonymous/003f2ce5f0b089b74107 to your computer and use it in GitHub Desktop.
Save anonymous/003f2ce5f0b089b74107 to your computer and use it in GitHub Desktop.
diff --git a/lib/Mojo/JSON.pm b/lib/Mojo/JSON.pm
index 8bd4373..278ec3f 100644
--- a/lib/Mojo/JSON.pm
+++ b/lib/Mojo/JSON.pm
@@ -9,7 +9,7 @@ use Scalar::Util 'blessed';
has 'error';
-our @EXPORT_OK = qw(decode_json encode_json j);
+our @EXPORT_OK = qw(decode_json encode_json encode_json_text j);
# Literal names
my $FALSE = bless \(my $false = 0), 'Mojo::JSON::_Bool';
@@ -48,6 +48,8 @@ sub encode { encode_json($_[1]) }
sub encode_json { Mojo::Util::encode 'UTF-8', _encode_value(shift) }
+sub encode_json_text { _encode_value(shift) }
+
sub false {$FALSE}
sub j {
@@ -64,12 +66,9 @@ sub _decode {
# Missing input
die "Missing or empty input\n" unless length(my $bytes = shift);
- # Wide characters
- die "Wide character in input\n" unless utf8::downgrade($bytes, 1);
-
# UTF-8
- die "Input is not UTF-8 encoded\n"
- unless defined(local $_ = Mojo::Util::decode('UTF-8', $bytes));
+ $bytes = Mojo::Util::decode 'UTF-8', $bytes if utf8::downgrade $bytes, 1;
+ die "Input is not UTF-8 encoded\n" unless defined(local $_ = $bytes);
# Value
my $value = _decode_value();
@@ -359,6 +358,12 @@ Decode JSON to Perl value and die if decoding fails.
Encode Perl value to JSON.
+=head2 encode_json_text
+
+ my $chars = encode_json_text({foo => 'bar'});
+
+Encode Perl value to JSON characters.
+
=head2 j
my $bytes = j([1, 2, 3]);
diff --git a/t/mojo/json.t b/t/mojo/json.t
index 2ae25ca..abea752 100644
--- a/t/mojo/json.t
+++ b/t/mojo/json.t
@@ -10,7 +10,7 @@ use Mojo::Base -strict;
use Test::More;
use Mojo::ByteStream 'b';
-use Mojo::JSON qw(decode_json encode_json j);
+use Mojo::JSON qw(decode_json encode_json encode_json_text j);
use Mojo::Util 'encode';
use Scalar::Util 'dualvar';
@@ -250,6 +250,10 @@ is index($bytes, b("\x{2029}")->encode), -1, 'properly esca
is_deeply decode_json($bytes), ["\x{2028}test\x{2029}123"],
'successful roundtrip';
+# Wide characters
+is_deeply decode_json('["♥"]'), ['♥'], 'wide characters decoded';
+is encode_json_text(['♥']), '["♥"]', 'wide characters encoded';
+
# Blessed reference
$bytes = encode_json [b('test')];
is_deeply decode_json($bytes), ['test'], 'successful roundtrip';
@@ -326,8 +330,6 @@ is j('null'), undef, 'decode null';
is $json->decode('test'), undef, 'syntax error';
is $json->error, 'Malformed JSON: Expected string, array, object, number,'
. ' boolean or null at line 0, offset 0', 'right error';
-is $json->decode('["♥"]'), undef, 'wide character in input';
-is $json->error, 'Wide character in input', 'right error';
is $json->decode(b('["\\ud800"]')->encode), undef, 'syntax error';
is $json->error, 'Malformed JSON: Missing low-surrogate at line 1, offset 8',
'right error';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment