Fixed json parsing of local files.

This commit is contained in:
Simon Wüllhorst 2016-07-19 17:02:58 +02:00
parent b1303a76d9
commit 985bf2f115

View file

@ -11,15 +11,20 @@ class JsonParser(object):
if self.printStatus: if self.printStatus:
print('Download', fileName.rsplit('/', 1)[1] , 'from URL:', fileName) print('Download', fileName.rsplit('/', 1)[1] , 'from URL:', fileName)
resource = urllib.request.urlopen(fileName) resource = urllib.request.urlopen(fileName)
try:
data = json.loads(resource.read().decode('utf-8'))
except:
raise HieraException('Error while parsing a json file (perhapes misformed file): ' + fileName)
finally:
resource.close()
else: else:
if self.printStatus: if self.printStatus:
print('Open', fileName.rsplit('/', 1)[1] , 'from file:', fileName) print('Open', fileName.rsplit('/', 1)[1] , 'from file:', fileName)
resource = open(fileName) with open(fileName) as data_file:
try: try:
data = json.loads(resource.read().decode('utf-8')) data = json.load(data_file)
except: except:
raise HieraException('Error while parsing a json file (perhapes misformed file): ' + fileName) raise HieraException('Error while parsing a json file (perhapes misformed file): ' + fileName)
finally:
resource.close()
return data return data