test-fullautomation / python-jsonpreprocessor

A preprocessor for json files
Apache License 2.0
2 stars 2 forks source link

Write method wanted for JsonPreprocessor #150

Closed HolQue closed 5 months ago

HolQue commented 9 months ago

The JsonPreprocessor can be used to read content from JSON files with:

jsonLoad(file)

A user might have the need to write back data to a JSON file (e.g. after the content has been modified). But this has to be done manually with something like

with open(outfile,"w", encoding='utf-8') as jsonfile:
   json.dump(new_content,jsonfile,ensure_ascii=False)
   jsonfile.close()

This is because of a corresponding method is missing in JsonPreprocessor. Long winded. Why not using the already existing JsonPreprocessor object in Python code?

Therefore wanted is the possibility to do this:

jsonWrite(new_content,jsonfile)

or maybe

jsonDump(new_content,jsonfile)

to be more aligned with the original function name.

namsonx commented 7 months ago

Hello Holger, Hello Thomas,

I pushed new commit 2ef6cedb139 to stabi branch to add new jsonDump() method to write a json file from configuration object.

Thank you, Son

HolQue commented 7 months ago

Saving the json file with jsonDump() in general works. But some problems are left and need to be fixed:

(1) History and PDF documentation are not updated. But jsonDump() is a new feature. This requires a minor number update of the JsonPreprocessor, an update of the history and a rebuild of the PDF documentation.

(2) This does not work properly:

outFile = CString.NormalizePath(outFile, sReferencePathAbs=os.path.dirname(sys.argv[0]))

Reason: In case of sys.argv[0] is relative, also sReferencePathAbs will be relative. With os.path.dirname() you only get the part of the path that is not the file name. But you need to get the absolute path to the file, because sReferencePathAbs has to be absolute! This is what you have to code explicitly. Otherwise jsonDump() returns a relative path (and this would be also in opposite to the interface description; there it is explained that an absolute path is returned).

Solution:

outFile = CString.NormalizePath(outFile, sReferencePathAbs=os.path.dirname(os.path.abspath(sys.argv[0])))

(3)

The interface description of jsonDump() needs to be adapted.

Not:

This function is used to write a json file from dictionary object.
It loads the dict object, write a new json file, and returns the path of new json file.

But: This function writes the content of a Python dictionary to a file in JSON format and returns a normalized path to this JSON file.

Not: Relative/absolute path to main json file.

But: Path and name of the JSON output file. The path can be absolute or relative and is also allowed to contain environment variables.

Not: Absolute path to main json file.

But: Normalized path and name of the JSON output file.

test-fullautomation commented 7 months ago

Hi Son, please consider the findings from Holger. Thank you, Thomas

namsonx commented 7 months ago

Hello Holger,

Thank you for your review, I will update this new feature based on your comments above.

Thank you, Son

namsonx commented 7 months ago

Updated on the stabi branch

HolQue commented 5 months ago

Proposals are implemented. Fine now. Issue can be closed.

namsonx commented 5 months ago

merged to develop branch

test-fullautomation commented 5 months ago

solved with version 0.10.0