[diffoscope] 02/02: Do some cheap fuzzy parsing to detect JSON files not named .json. (re. #888112)

Chris Lamb chris at chris-lamb.co.uk
Wed Jan 24 02:17:15 CET 2018


This is an automated email from the git hooks/post-receive script.

lamby pushed a commit to branch master
in repository diffoscope.

commit 2a758d3d0205e934ed6dffebb5d6462b00fe590d
Author: Chris Lamb <lamby at debian.org>
Date:   Wed Jan 24 12:11:20 2018 +1100

    Do some cheap fuzzy parsing to detect JSON files not named .json. (re. #888112)
---
 diffoscope/comparators/json.py | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)

diff --git a/diffoscope/comparators/json.py b/diffoscope/comparators/json.py
index 6ebe5cf..7263657 100644
--- a/diffoscope/comparators/json.py
+++ b/diffoscope/comparators/json.py
@@ -26,14 +26,16 @@ from .utils.file import File
 
 
 class JSONFile(File):
-    FILE_EXTENSION_SUFFIX = '.json'
-
     @classmethod
     def recognizes(cls, file):
-        if not super().recognizes(file):
-            return False
-
         with open(file.path) as f:
+            # Try fuzzy matching for JSON files
+            if not file.name.endswith('.json') and \
+                    file.magic_file_type.startswith('ASCII text'):
+                if '{' not in f.read(10):
+                    return False
+                f.seek(0)
+
             try:
                 file.parsed = json.load(
                     f,

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/reproducible/diffoscope.git


More information about the diffoscope mailing list