From 985bf2f1151ef8b8814fc4798ac5d60acf998eab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20W=C3=BCllhorst?= Date: Tue, 19 Jul 2016 17:02:58 +0200 Subject: [PATCH] Fixed json parsing of local files. --- parser/JsonParser.py | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/parser/JsonParser.py b/parser/JsonParser.py index 8ba4db2..96f914f 100644 --- a/parser/JsonParser.py +++ b/parser/JsonParser.py @@ -11,15 +11,20 @@ class JsonParser(object): if self.printStatus: print('Download', fileName.rsplit('/', 1)[1] , 'from URL:', 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: if self.printStatus: print('Open', fileName.rsplit('/', 1)[1] , 'from file:', fileName) - resource = open(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() + with open(fileName) as data_file: + try: + data = json.load(data_file) + except: + raise HieraException('Error while parsing a json file (perhapes misformed file): ' + fileName) + return data