Commit 2a758d3d authored by Chris Lamb's avatar Chris Lamb 👀
Browse files

Do some cheap fuzzy parsing to detect JSON files not named .json. (re. #888112)

parent 9bcbb517
Loading
Loading
Loading
Loading
+7 −5
Original line number Diff line number Diff line
@@ -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):
        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)

        with open(file.path) as f:
            try:
                file.parsed = json.load(
                    f,