Commit 065f9c0c authored by Ximin Luo's avatar Ximin Luo

Fix json presenter and handle recursion properly, it was completely wrong

parent 829ed378
......@@ -29,26 +29,37 @@ JSON_FORMAT_MAGIC = "diffoscope-json-version"
class JSONPresenter(Presenter):
def __init__(self, print_func):
self.stack = []
self.current = self.stack
self.print_func = print_func
super().__init__()
def start(self, difference):
root = []
self.stack = [root]
super().start(difference)
self.stack[0][JSON_FORMAT_MAGIC] = JSON_FORMAT_MAGIC
self.stack[0].move_to_end(JSON_FORMAT_MAGIC, last=False)
self.print_func(json.dumps(self.stack[0], indent=2))
root[0][JSON_FORMAT_MAGIC] = JSON_FORMAT_VERSION
root[0].move_to_end(JSON_FORMAT_MAGIC, last=False)
self.print_func(json.dumps(root[0], indent=2))
def visit_difference(self, difference):
child_differences = []
self.current.append(OrderedDict([
while self.depth + 1 < len(self.stack):
self.stack.pop()
elements = [
('source1', difference.source1),
('source2', difference.source2),
('comments', [x for x in difference.comments]),
('differences', child_differences),
('unified_diff', difference.unified_diff),
]))
('source2', difference.source2)
]
if difference.comments:
elements += [('comments', [x for x in difference.comments])]
if difference.has_internal_linenos:
elements += [('has_internal_linenos', True)]
elements += [('unified_diff', difference.unified_diff)]
child_differences = []
if difference.details:
elements += [('details', child_differences)]
self.current = child_differences
self.stack[-1].append(OrderedDict(elements))
if difference.details:
self.stack.append(child_differences)
{
"comments": [],
"differences": [
"diffoscope-json-version": 1,
"source1": "test1.tar",
"source2": "test2.tar",
"unified_diff": null,
"details": [
{
"comments": [],
"differences": [
{
"comments": [],
"differences": [
{
"comments": [
"symlink"
],
"differences": [],
"source1": "dir/link",
"source2": "dir/link",
"unified_diff": "@@ -1 +1 @@\n-destination: broken\n+destination: really-broken\n"
}
],
"source1": "dir/text",
"source2": "dir/text",
"unified_diff": "@@ -1,6 +1,12 @@\n+A common form of lorem ipsum reads:\n+\n Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor\n incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis\n nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.\n Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu\n fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in\n culpa qui officia deserunt mollit anim id est laborum.\n+\n+\"Lorem ipsum\" text is derived from sections 1.10.32--3 of Cicero's De finibus\n+bonorum et malorum (On the Ends of Goods and Evils, or alternatively [About]\n+The Purposes of Good and Evil).\n"
}
],
"source1": "file list",
"source2": "file list",
"unified_diff": "@@ -1,4 +1,4 @@\n-drwxr-xr-x 0 lunar (1000) lunar (1000) 0 2015-06-29 15:49:09.000000 dir/\n--rw-r--r-- 0 lunar (1000) lunar (1000) 446 2015-06-29 15:49:09.000000 dir/text\n-crw-r--r-- 0 root (0) root (0) 1, 3 2015-06-29 15:49:09.000000 dir/null\n-lrwxrwxrwx 0 lunar (1000) lunar (1000) 0 2015-06-29 15:49:09.000000 dir/link -> broken\n+drwxr-xr-x 0 lunar (1000) lunar (1000) 0 2015-06-29 15:49:41.000000 dir/\n+-rw-r--r-- 0 lunar (1000) lunar (1000) 671 2015-06-29 15:49:41.000000 dir/text\n+crw-r--r-- 0 root (0) root (0) 1, 3 2015-06-29 15:49:41.000000 dir/null\n+lrwxrwxrwx 0 lunar (1000) lunar (1000) 0 2015-06-29 15:49:41.000000 dir/link -> really-broken\n"
},
{
"source1": "dir/text",
"source2": "dir/text",
"unified_diff": "@@ -1,6 +1,12 @@\n+A common form of lorem ipsum reads:\n+\n Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor\n incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis\n nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.\n Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu\n fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in\n culpa qui officia deserunt mollit anim id est laborum.\n+\n+\"Lorem ipsum\" text is derived from sections 1.10.32--3 of Cicero's De finibus\n+bonorum et malorum (On the Ends of Goods and Evils, or alternatively [About]\n+The Purposes of Good and Evil).\n"
},
{
"source1": "dir/link",
"source2": "dir/link",
"comments": [
"symlink"
],
"unified_diff": "@@ -1 +1 @@\n-destination: broken\n+destination: really-broken\n"
}
],
"source1": "test1.tar",
"source2": "test2.tar",
"unified_diff": null
]
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment