Browse Source

Attempt to handle mis-encoded data

George S. Baugh 4 years ago
parent
commit
9497b1b2e1
1 changed files with 7 additions and 2 deletions
  1. 7 2
      lib/Trog/Data/FlatFile.pm

+ 7 - 2
lib/Trog/Data/FlatFile.pm

@@ -54,8 +54,13 @@ sub read ($self, $query={}) {
         }
         }
         my $parsed  = eval { $parser->decode($slurped) };
         my $parsed  = eval { $parser->decode($slurped) };
         if (!$parsed) {
         if (!$parsed) {
-            print "JSON Decode error on $item:\n$@\n";
-            next;
+            # Try and read it in binary in case it was encoded incorrectly the first time
+	    $slurped = eval { File::Slurper::read_binary($item) };
+	    $parsed  = eval { $parser->decode($slurped) };
+	    if (!$parsed) {
+                print "JSON Decode error on $item:\n$@\n";
+                next;
+            }
         }
         }
 
 
         #XXX this imposes an inefficiency in itself, get() will filter uselessly again here
         #XXX this imposes an inefficiency in itself, get() will filter uselessly again here