A JSON parser that tolerates common syntax errors.
Project description
tolerantjson
tolerantjson
is a Python package that provides a robust JSON parser capable of handling non-standard JSON inputs. This parser is designed to be more forgiving than the strict JSON parsers that adhere to the RFC 7159 specification. It can handle certain forms of malformed JSON, such as trailing commas or single-quoted strings, and can provide callbacks for dealing with extra tokens.
Features
- Parses JSON with a best-effort, recovering from some common errors in JSON formatting.
- Reports the nature and position of parsing errors for easier debugging.
- Provides a mechanism to handle extra tokens or malformed structures via callbacks.
- Register custom parsers for specific cases or tokens.
Installation
To install tolerantjson
, use pip:
pip install tolerantjson
Alternatively, you can clone the repository and install it manually:
git clone https://github.com/nurettn/tolerantjson.git
cd tolerantjson
python setup.py install
Usage
Here is a basic example of how to use tolerantjson to parse a JSON string:
import tolerantjson as tjson
# Example JSON string with an error
json_str = '[1,2,{"a":"apple",}]'
# Parse the JSON string
try:
data = tjson.tolerate(json_str)
print(data)
except tjson.ParseException as e:
print(f"Failed to parse JSON: {e}")
# Output: [1, 2, {'a': 'apple'}]
Handling Extra Tokens
You can define your own callback for handling extra tokens by assigning a function to tjson.parse.on_extra_token
. Here's an example:
import tolerantjson as tjson
def handle_extra_token(text, data, reminding):
print(f"Warning: Extra tokens detected after valid JSON. Data: {data}, Extra: {reminding}")
# Assign your custom callback
tjson.tolerate.on_extra_token = handle_extra_token
# Parse a JSON string with extra tokens
json_str = '[1,2,3] extra tokens'
data = tjson.tolerate(json_str)
Custom Parsers
You can extend tolerantjson
with custom parsers for specific scenarios:
import tolerantjson as tjson
def parse_custom_date(s, e):
# Custom parsing logic
pass
# Register the custom parser for a specific token
tjson.parsers['d'] = parse_custom_date
# Use the extended parser
json_str = 'd"2024-03-22"'
data = tjson.tolerate(json_str)
Contribution
Contributions are welcome! If you would like to contribute to the project, please follow these steps:
- Fork the repository.
- Create a new branch for your feature or fix.
- Write your code and add tests if applicable.
- Submit a pull request with a clear description of your changes.
License
tolerantjson
is open source software licensed as MIT.
Credits
This project was inspired by the best-effort-json-parser.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file tolerantjson-1.0.0.tar.gz
.
File metadata
- Download URL: tolerantjson-1.0.0.tar.gz
- Upload date:
- Size: 4.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.8.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 23d03f9e0676360e3e49bb8bdeb02e2d574bfbe142af857c3a5273f1c42a94e6 |
|
MD5 | 95161ef8f3f5f181010a896ee606aa92 |
|
BLAKE2b-256 | 40bf8523eee163087f2acc492fe73d3ab0a178ecede366c6de49a7c59ff9a79f |
File details
Details for the file tolerantjson-1.0.0-py3-none-any.whl
.
File metadata
- Download URL: tolerantjson-1.0.0-py3-none-any.whl
- Upload date:
- Size: 5.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.8.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7c9a62b42981db590c65929381fb00032da533461c554be608d5541e957cd830 |
|
MD5 | 2bd80f5efa7e62b42f9861c083d0854b |
|
BLAKE2b-256 | 9ffa72b1ac68835b78a1dae589502de5438cfd2605ce4b51f1e987e36081d2b9 |