Permalink
Browse files

fixed JSON encoding detection

  • Loading branch information...
1 parent 667682f commit e9eaf5b7880d4aeab07525ed778b5602679d739d @rsp committed Nov 19, 2009
Showing with 15 additions and 1 deletion.
  1. +15 −1 lib/Mojo/JSON.pm
View
@@ -85,6 +85,14 @@ my $BOM_RE;
$BOM_RE = qr/^($bom)/;
}
+# Unicode encoding detection
+my $UTF_PATTERNS = {
+ "\0\0\0[^\0]" => 'UTF-32BE',
+ "\0[^\0]\0[^\0]" => 'UTF-16BE',
+ "[^\0]\0\0\0" => 'UTF-32LE',
+ "[^\0]\0[^\0]\0" => 'UTF-16LE'
+};
+
# Hey...That's not the wallet inspector...
sub decode {
my ($self, $string) = @_;
@@ -97,7 +105,13 @@ sub decode {
# Detect and decode unicode
my $encoding = 'UTF-8';
- if ($string =~ s/$BOM_RE//) { $encoding = $BOM->{$1} }
+ $string =~ s/$BOM_RE//g;
+ for my $pattern (keys %$UTF_PATTERNS) {
+ if ($string =~ /^$pattern/){
+ $encoding = $UTF_PATTERNS->{$pattern};
+ last;
+ }
+ }
$string = b($string)->decode($encoding)->to_string;
# Decode

0 comments on commit e9eaf5b

Please sign in to comment.