Skip to content

Commit

Permalink
Adding support for raw JSON objects
Browse files Browse the repository at this point in the history
  • Loading branch information
monsur committed Sep 9, 2011
1 parent c5ec908 commit c81da7c
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions jsoncompare.py
@@ -1,6 +1,7 @@
import httplib2
import json
import sys
import types

TYPE = 'TYPE'
PATH = 'PATH'
Expand All @@ -14,7 +15,7 @@ def __init__(self, first, second, with_values=False):
self.seen = []
not_with_values = not with_values
self.check(first, second, with_values=with_values)

def check(self, first, second, path='', with_values=False):
if with_values and second != None:
if not isinstance(first, type(second)):
Expand Down Expand Up @@ -44,9 +45,9 @@ def check(self, first, second, path='', with_values=False):
self.check(first[key], sec, path=new_path, with_values=with_values)
else:
# second is not dict. every key from first goes to the difference
self.save_diff(new_path, PATH)
self.save_diff(new_path, PATH)
self.check(first[key], second, path=new_path, with_values=with_values)

# if object is list, loop over it and check.
elif isinstance(first, list):
for (index, item) in enumerate(first):
Expand All @@ -68,8 +69,8 @@ def check(self, first, second, path='', with_values=False):
if with_values and second != None:
if first != second:
self.save_diff('%s - %s | %s' % (path, first, second), VALUE)
return
return

def save_diff(self, diff_message, type_):
if diff_message not in self.difference:
self.seen.append(diff_message)
Expand All @@ -79,12 +80,14 @@ def getContentFromUri(uri):
h = httplib2.Http()
resp, content = h.request(uri, "GET")
return content

def getContentFromFile(filePath):
return open(filePath, 'r').read()

def getContent(location):
content = None
if type(location) is types.DictType:
return location
if location.startswith("http"):
content = getContentFromUri(location)
else:
Expand Down Expand Up @@ -117,5 +120,4 @@ def compare(location1, location2):
if len(diffs) > 0:
print '\r\nFound differences comparing ' + location1 + ' and ' + location2
for diff in diffs:
print diff['type'] + ': ' + diff['message']

print diff['type'] + ': ' + diff['message']

0 comments on commit c81da7c

Please sign in to comment.