🐍 The jsoncons Package 🐛❇️🐉
🚙🦖 COBOL-to-JSON CLI Utility in Python 🦕🐍
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.
Installation:
pip install jsoncons
Basic Usage for Pretty-Print JSON:
-
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
decodecommand might be an alias or offer slightly different formatting if needed)
Latest Release: jsoncons v1.1.0 ✨
New Features: Fibonacci Hashing Integration
- Fibonacci Hashing Function:
fibonacci_hash_to_index()for efficient hash table indexing - New CLI Commands:
process_json_fib- Fibonacci variant of JSON processingcobol_to_json_fib- Fibonacci variant of COBOL-to-JSON conversion
- Comprehensive Jupyter Notebook:
Fibonacci_Hashing_Demo.ipynbdemonstrating:- Performance benchmarks (Fibonacci vs. Modulo vs. Bitwise AND hashing)
- Distribution analysis with visualizations
- Step-by-step algorithm visualization
- Educational content on hashing techniques
- Full Backward Compatibility: All existing commands work unchanged
- Tested on Python: 3.8+, 3.11.1, 3.11.2, 3.12.1
What is Fibonacci Hashing?
Fibonacci hashing is a multiplicative hashing technique that uses the golden ratio to distribute hash values uniformly across power-of-2 sized hash tables. It's faster than modulo hashing and provides better distribution than simple bitwise AND operations.
Key Benefits:
- ⚡ Performance: Uses only multiplication and bit shift operations
- 📊 Distribution: Uniform distribution across hash table indices
- 🎓 Educational: Learn about advanced hashing techniques
- 🔧 Extensible: Foundation for future performance optimizations
Using Fibonacci Hashing
# Process JSON with Fibonacci variant
jsoncons process_json_fib input.json output.json
# Convert COBOL to JSON with Fibonacci variant
jsoncons cobol_to_json_fib --layout-file layout.json input.cobol output.json
# Use the Fibonacci hashing function in Python
from jsoncons.cli import fibonacci_hash_to_index
index = fibonacci_hash_to_index(hash_value=12345, table_size_power_of_2=1024)
print(f"Hash index: {index}") # Output: 0-1023
Jupyter Notebook Demo
Run the included Fibonacci_Hashing_Demo.ipynb to see:
- Performance comparisons with 100,000 hash operations
- Distribution analysis with histograms
- Visual step-by-step demonstration of the algorithm
- Practical applications and conclusions
Previous Release: jsoncons v1.0.4
Bug Fixed: f-string Issue in COBOL-to-JSON function
- COBOL-to-JSON function tested in 3.11.1, 3.11.2, 3.12.1
- Compatibility with Python v3.8+
Roadmap to v2.0.0
- Integration with IBM zOS
- Performance optimizations using Fibonacci hashing in internal data structures
- Enhanced COBOL field lookup with hash table acceleration
🤝 Contributing 🖥️
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.
📝 License ⚖️
This project is licensed under the MIT License - see the LICENSE file for details.
🧪 Unit Test Explanation For jsoncons Package ✅
- 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 theclimodule from the package. TestJsonConsCLIClass: 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_cliHelper:- 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.patchas a context manager to temporarily replacesys.argv,sys.stdout, andsys.stderrwith test-controlled objects (io.StringIOfor streams). - If
stdin_datais provided,sys.stdinis 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_cliwith 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_cliorpython tests/test_cli.py.
⛰️ Extending jsoncons to COBOL 👀
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.jsonfile from COBOL.
Alternatively:
- COBOL generates simple key-value pairs or a structured text format.
- A more sophisticated
jsonconsencodecommand could be written to parse this specific text format and produce JSON. - A
jsonconsdecodecommand 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.
Metadata
Release files for jsoncons 1.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| jsoncons-1.1.0.tar.gz | 55.6 kB | Without provenance | Details for jsoncons-1.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| jsoncons-1.1.0-py3-none-any.whl | Python 3 | none | any | Details for jsoncons-1.1.0-py3-none-any.whl |
Total release size: 71.1 kB
Release files / jsoncons-1.1.0.tar.gz
| Download URL | jsoncons-1.1.0.tar.gz |
|---|---|
| Size | 55.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
16e1858a497984fa8ff7ed6689e1a72571b28d4da6fc7c9709cbfa7f14c196bd
|
|
BLAKE2b-256 checksum How to use checksums |
10ce3385d6ba4523ac7260c306d1baef63826dff2134ecaa7a33a3ac42b0fba9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.13.7
|
Release files / jsoncons-1.1.0-py3-none-any.whl
| Download URL | jsoncons-1.1.0-py3-none-any.whl |
|---|---|
| Size | 15.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
c6d573ffb08fe78bafca3dd837794ace550a9b100e553bf888d95a9c0206ef49
|
|
BLAKE2b-256 checksum How to use checksums |
4a434debd2884ee5c9461aa329c3d9101d994e06e3c972b3d8f4f9620f2b2ef5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.13.7
|
Security for jsoncons 1.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.