The jsoncons
package is designed to provide a basic command-line interface for handling JSON data. This can be useful for simple scripting or interoperability tasks (e.g., having a COBOL program generate a text file that this tool converts to JSON, or vice versa).
COBOL-to-JSON parsing features were added in v1.0.0 and will be extended in future versions of jsoncons
.
pip install jsoncons
-
Create Input File If Necessary: In your project directory, verify there is a file named input.json with the following content:
{"key":"value", "items":[1,2]}
-
Validate & Pretty-print JSON: Read from stdin, write to stdout. (Linux Command)
echo '{"key":"value", "items":[1,2]}' | jsoncons encode
Windows Powershell Command: Read from stdin, write to stdout.
echo {"\"key\"":"\"value\"", "\"items\"":[1,2]} | jsoncons encode
-
Validate & Pretty-print JSON from file to file: (Tested on Windows 10)
jsoncons encode input.json output_pretty.json
-
(The
decode
command might be an alias or offer slightly different formatting if needed)
- Jupyter Notebook Tutorial
- COBOL-to-JSON function tested in 3.11.1,3.11.2, 3.12.1
- Compatibility expected with Python v3.8>=
- Improved FP COBOL-to-JSON functionality
- Template Python Function using
jsoncons
- Integration with IBM zOS
Contributions are welcome! If you find errors, have suggestions for improvements, or want to add more examples, please feel free to:
- Open an issue to discuss the change.
- Fork the repository.
- Create a new branch (
git checkout -b feature/your-feature-name
). - Make your changes and commit them (
git commit -m 'Add some feature'
). - Push to the branch (
git push origin feature/your-feature-name
). - Open a Pull Request.
This project is licensed under the MIT License - see the LICENSE file for details.
- Imports: Imports necessary modules like
unittest
,sys
(for patching argv/streams),io
(for capturing streams),os
,json
,tempfile
,shutil
, andunittest.mock.patch
. It also imports thecli
module from the package. TestJsonConsCLI
Class: Inherits fromunittest.TestCase
.setUp
:- Creates a temporary directory using
tempfile.mkdtemp()
to isolate test files. - Defines paths for input, output, and invalid files within the temp directory.
- Creates sample valid and invalid JSON strings and data structures.
- Writes the sample valid and invalid JSON to the respective temporary files.
- Creates a temporary directory using
tearDown
: Cleans up by removing the temporary directory and all its contents usingshutil.rmtree()
.run_cli
Helper:- Takes a list of arguments (
args_list
) and optionalstdin_data
. - Prepends the script name (
'serial-json'
) to the arguments list assys.argv[0]
. - Uses
unittest.mock.patch
as a context manager to temporarily replacesys.argv
,sys.stdout
, andsys.stderr
with test-controlled objects (io.StringIO
for streams). - If
stdin_data
is provided,sys.stdin
is also patched. - Calls the actual
cli.main()
function within the patched context. - Catches
SystemExit
(whichsys.exit()
raises) to get the exit code. - Returns the captured stdout string, stderr string, and the exit code.
- Takes a list of arguments (
- Test Methods (
test_...
):- Each method tests a specific scenario (stdin/stdout, file I/O, options, errors).
- They call
run_cli
with appropriate arguments and/or stdin data. - They use
self.assertEqual
,self.assertNotEqual
,self.assertTrue
,self.assertIn
, etc., to verify:- The exit code (0 for success, non-zero for errors).
- The content of captured
stderr
(should be empty on success, contain error messages on failure). - The content of captured
stdout
(when output is expected there). - The existence and content of output files (when file output is expected).
if __name__ == '__main__':
: Allows running the tests directly usingpython -m unittest tests.test_cli
orpython tests/test_cli.py
.
How COBOL could interact:
A COBOL program could:
- Write data to a temporary text file (e.g.,
input.txt
). - Use
CALL 'SYSTEM'
(or equivalent OS call) to execute the Python script:CALL 'SYSTEM' USING 'jsoncons input.txt output.json'.
- Read the resulting
output.json
file from COBOL.
Alternatively:
- COBOL generates simple key-value pairs or a structured text format.
- A more sophisticated
jsoncons
encode
command could be written to parse this specific text format and produce JSON. - A
jsoncons
decode
command could parse JSON and output a simple text format readable by COBOL.
The provided CLI keeps things simple and standard, relying on JSON as the interchange format, which COBOL would interact with via file I/O and system calls.