-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemo.py
More file actions
109 lines (89 loc) · 3.29 KB
/
demo.py
File metadata and controls
109 lines (89 loc) · 3.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/usr/bin/env python3
"""
Example usage of the Python JSON Diff library.
This demonstrates functionality similar to the Go json-diff repository.
"""
import json
from pprint import pprint
from jsondiff import JSONDiff, JSONReconstructor
def main():
# Example JSON objects
json1 = {
"name": "John",
"age": 30,
"city": "New York",
"hobbies": [["reading", "swimming"]],
"address": {
"street": "123 Main St",
"zip": "10001"
}
}
json2 = {
"name": "John",
"age": 31, # Modified
"city": "Boston", # Modified
"hobbies": [["reading", "swimming", "cycling"]], # Array modified
"address": {
"street": "456 Oak Ave", # Modified
"zip": "10001",
"country": "USA" # Added
},
"email": "john@example.com", # Added
"obj":{ # object added
"a":1
}
}
print("=== JSON Diff Example ===")
print("\nOriginal JSON:")
print(json.dumps(json1, indent=2))
print("\nModified JSON:")
print(json.dumps(json2, indent=2))
# Create diff
differ = JSONDiff()
diffs = differ.compare(json1, json2)
print("\n=== Differences Found ===")
for i, diff in enumerate(diffs, 1):
print(f"\nDiff {i}:")
print(f" Field: {diff['field_name']}")
print(f" Change Type: {diff['change_type']}")
print(f" From: {diff['from_value']}")
print(f" To: {diff['to_value']}")
print(f" Full Path: {diff['full_path']}")
print(f" Value Type: {diff['value_type']}")
print(f"\nTotal diffs: {len(diffs)}")
print("Diff paths:", [d['full_path'] for d in diffs])
# Test reconstruction
print("\n=== JSON Reconstruction Example ===")
reconstructor = JSONReconstructor()
original_reconstructed = reconstructor.reverse_diff(json2, diffs)
print("\nReconstructed Original JSON:")
print(json.dumps(original_reconstructed, indent=2))
print("\nReconstruction Test:")
if original_reconstructed == json1:
print("✅ SUCCESS: Reconstructed JSON matches original!")
else:
print("❌ FAILED: Reconstructed JSON does not match original")
print("Expected:")
print(json.dumps(json1, indent=2))
print("Got:")
print(json.dumps(original_reconstructed, indent=2))
# Test with JSON strings
print("\n=== Testing with JSON Strings ===")
json1_str = json.dumps(json1)
json2_str = json.dumps(json2)
diffs_from_strings = differ.compare(json1_str, json2_str)
print(f"Found {len(diffs_from_strings)} differences when comparing JSON strings")
# Array modification example
print("\n=== Array Modification Example ===")
array1 = {"fruits": ["apple", "banana", "orange"]}
array2 = {"fruits": ["apple", "orange", "grape"]} # banana removed, grape added
array_diffs = differ.compare(array1, array2)
print("Array 1:", json.dumps(array1))
print("Array 2:", json.dumps(array2))
print("\nArray Differences:")
for diff in array_diffs:
print(f" {diff['field_name']}: {diff['change_type']}")
print(f" From: {diff['from_value']}")
print(f" To: {diff['to_value']}")
if __name__ == "__main__":
main()