diff --git a/src/sonic-config-engine/portconfig.py b/src/sonic-config-engine/portconfig.py index b2d28b5150..c41878f567 100644 --- a/src/sonic-config-engine/portconfig.py +++ b/src/sonic-config-engine/portconfig.py @@ -45,16 +45,25 @@ BRKOUT_PATTERN = r'(\d{1,3})x(\d{1,3}G)(\[\d{1,3}G\])?(\((\d{1,3})\))?' # # Helper Functions # + +# For python2 compatibility +def py2JsonStrHook(j): + if isinstance(j, unicode): + return j.encode('utf-8', 'backslashreplace') + if isinstance(j, list): + return [py2JsonStrHook(item) for item in j] + if isinstance(j, dict): + return {py2JsonStrHook(key): py2JsonStrHook(value) + for key, value in j.iteritems()} + return j + def readJson(filename): # Read 'platform.json' or 'hwsku.json' file try: with open(filename) as fp: - try: - data = json.load(fp) - except json.JSONDecodeError: - print("Json file does not exist") - data_dict = ast.literal_eval(json.dumps(data)) - return data_dict + if sys.version_info.major == 2: + return json.load(fp, object_hook=py2JsonStrHook) + return json.load(fp) except Exception as e: print("error occurred while parsing json: {}".format(sys.exc_info()[1])) return None diff --git a/src/sonic-config-engine/tests/sample_platform.json b/src/sonic-config-engine/tests/sample_platform.json index fd11a49ed0..461e01e371 100644 --- a/src/sonic-config-engine/tests/sample_platform.json +++ b/src/sonic-config-engine/tests/sample_platform.json @@ -1,4 +1,16 @@ { + "chassis": { + "psus": [ + { + "name": "PSU 1", + "temperature": false + }, + { + "name": "PSU 2", + "temperature": false + } + ] + }, "interfaces": { "Ethernet0": { "index": "1,1,1,1",