Skip to content

Commit

Permalink
Enhancement 193 - Added jsonLoads method
Browse files Browse the repository at this point in the history
  • Loading branch information
namsonx committed Jun 4, 2024
1 parent b8d6f39 commit 862548e
Showing 1 changed file with 54 additions and 26 deletions.
80 changes: 54 additions & 26 deletions JsonPreprocessor/CJsonPreprocessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -1200,6 +1200,41 @@ def jsonLoad(self, jFile : str, masterFile : bool = True):
/ *Type*: dict /
Preprocessed JSON file(s) as Python dictionary
"""
jFile = CString.NormalizePath(jFile, sReferencePathAbs=os.path.dirname(os.path.abspath(sys.argv[0])))
if not(os.path.isfile(jFile)):
self.__reset()
raise Exception(f"File '{jFile}' is not existing!")

self.lImportedFiles.append(jFile)
self.jsonPath = os.path.dirname(jFile)
try:
sJsonData= self.__load_and_removeComments(jFile)
except Exception as reason:
self.__reset()
raise Exception(f"Could not read json file '{jFile}' due to: '{reason}'!")

return self.jsonLoads(sJsonData, jFile, masterFile)

def jsonLoads(self, sJsonpContent : str, jFile : str = '', masterFile : bool = True):
"""
``jsonLoads`` loads the JSONP content, preprocesses it and returns the preprocessed result as Python dictionary.
**Arguments:**
* ``sJsonpContent``
/ *Condition*: required / *Type*: str /
The JSONP content.
**Returns:**
* ``oJson``
/ *Type*: dict /
Preprocessed JSON content as Python dictionary
"""
def __handleDuplicatedKey(dInput : dict) -> dict:
listKeys = list(dInput.keys())
Expand Down Expand Up @@ -1269,21 +1304,8 @@ def __handleLastElement(sInput : str) -> str:
sInput = sInput.replace(sParam, '"' + sParam + '"')
return sInput

jFile = CString.NormalizePath(jFile, sReferencePathAbs=os.path.dirname(os.path.abspath(sys.argv[0])))
if not(os.path.isfile(jFile)):
self.__reset()
raise Exception(f"File '{jFile}' is not existing!")

self.lImportedFiles.append(jFile)
self.jsonPath = os.path.dirname(jFile)
try:
sJsonData= self.__load_and_removeComments(jFile)
except Exception as reason:
self.__reset()
raise Exception(f"Could not read json file '{jFile}' due to: '{reason}'!")

sJsonDataUpdated = ""
for line in sJsonData.splitlines():
for line in sJsonpContent.splitlines():
if line == '' or line.isspace():
continue
try:
Expand Down Expand Up @@ -1414,13 +1436,16 @@ def __handleLastElement(sInput : str) -> str:
object_pairs_hook=self.__processImportFiles)
except Exception as error:
self.__reset()
failedJsonDoc = self.__getFailedJsonDoc(error)
jsonException = "not defined"
if failedJsonDoc is None:
jsonException = f"{error}\nIn file: '{jFile}'"
if jFile != '':
failedJsonDoc = self.__getFailedJsonDoc(error)
jsonException = "not defined"
if failedJsonDoc is None:
jsonException = f"{error}\nIn file: '{jFile}'"
else:
jsonException = f"{error}\nNearby: '{failedJsonDoc}'\nIn file: '{jFile}'"
raise Exception(jsonException)
else:
jsonException = f"{error}\nNearby: '{failedJsonDoc}'\nIn file: '{jFile}'"
raise Exception(jsonException)
raise Exception(error)
self.bDuplicatedKeys = True

# Load Json object with checking duplicated keys feature is enabled.
Expand All @@ -1431,13 +1456,16 @@ def __handleLastElement(sInput : str) -> str:
object_pairs_hook=self.__processImportFiles)
except Exception as error:
self.__reset()
failedJsonDoc = self.__getFailedJsonDoc(error)
jsonException = "not defined"
if failedJsonDoc is None:
jsonException = f"{error}\nIn file: '{jFile}'"
if jFile != '':
failedJsonDoc = self.__getFailedJsonDoc(error)
jsonException = "not defined"
if failedJsonDoc is None:
jsonException = f"{error}\nIn file: '{jFile}'"
else:
jsonException = f"{error}\nNearby: '{failedJsonDoc}'\nIn file: '{jFile}'"
raise Exception(jsonException)
else:
jsonException = f"{error}\nNearby: '{failedJsonDoc}'\nIn file: '{jFile}'"
raise Exception(jsonException)
raise Exception(error)
self.__checkDotInParamName(oJson)
__checkKeynameFormat(oJson)

Expand Down

0 comments on commit 862548e

Please sign in to comment.