[diffoscope] 01/01: Add a JSON comparator.

Chris Lamb chris at chris-lamb.co.uk
Sat Aug 6 13:09:13 CEST 2016


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

lamby pushed a commit to branch master
in repository diffoscope.

commit d72c1f48f668269480176bc7585bcca8c49bb6ef
Author: Chris Lamb <lamby at debian.org>
Date:   Sat Aug 6 12:09:04 2016 +0100

    Add a JSON comparator.
    
    Signed-off-by: Chris Lamb <lamby at debian.org>
---
 diffoscope/comparators/__init__.py |  2 ++
 diffoscope/comparators/json.py     | 44 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 46 insertions(+)

diff --git a/diffoscope/comparators/__init__.py b/diffoscope/comparators/__init__.py
index f02da1a..4fb31d3 100644
--- a/diffoscope/comparators/__init__.py
+++ b/diffoscope/comparators/__init__.py
@@ -58,6 +58,7 @@ from diffoscope.comparators.icc import IccFile
 from diffoscope.comparators.image import ImageFile
 from diffoscope.comparators.ipk import IpkFile
 from diffoscope.comparators.iso9660 import Iso9660File
+from diffoscope.comparators.json import JSONFile
 from diffoscope.comparators.macho import MachoFile
 from diffoscope.comparators.mono import MonoExeFile
 from diffoscope.comparators.pdf import PdfFile
@@ -136,6 +137,7 @@ FILE_CLASSES = (
     DebDataTarFile,
     ElfSection,
     PsFile,
+    JSONFile,
     TextFile,
     Bzip2File,
     CpioFile,
diff --git a/diffoscope/comparators/json.py b/diffoscope/comparators/json.py
new file mode 100644
index 0000000..055a2a7
--- /dev/null
+++ b/diffoscope/comparators/json.py
@@ -0,0 +1,44 @@
+# -*- coding: utf-8 -*-
+#
+# diffoscope: in-depth comparison of files, archives, and directories
+#
+# Copyright © 2016 Chris Lamb <lamby at debian.org>
+#
+# diffoscope is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# diffoscope is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with diffoscope.  If not, see <http://www.gnu.org/licenses/>.
+
+import re
+import json
+
+from diffoscope.difference import Difference
+from diffoscope.comparators.binary import File
+
+
+class JSONFile(File):
+    RE_FILE_EXTENSION = re.compile(r'\.json$')
+
+    @staticmethod
+    def recognizes(file):
+        return JSONFile.RE_FILE_EXTENSION.search(file.name)
+
+    def compare_details(self, other, source=None):
+        def dumps(path):
+            with open(path) as f:
+                return json.dumps(json.load(f), indent=4, sort_keys=True)
+
+        return [Difference.from_text(
+            dumps(self.path),
+            dumps(other.path),
+            self,
+            other,
+        )]

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


More information about the diffoscope mailing list