Triple-JSON5 parser for Python
Project description
TJSON5 Parser
A high-performance Python parser for Triple-JSON5 (TJSON5) files implemented in Cython.
Features
- Parses TJSON5 files into Python objects
- Supports all standard JSON5 features:
- Comments (single and multi-line)
- Trailing commas in objects and arrays
- Unquoted object keys
- Triple-JSON5 extensions:
- Triple-quoted strings (
""") for multi-line text without escaping - Hexadecimal number literals (
0xFF) - Binary number literals (
0b1010)
- Triple-quoted strings (
- Automatic encoding detection and fallback
- Helpful error messages with context
- No external dependencies (pure Python/Cython implementation)
Installation
# Install from source
git clone https://github.com/kristofmulier/triple-json5.git
cd triple-json5/python_tjson5
pip install -e .
Usage
import tjson5
# Parse a TJSON5 string
data = tjson5.parse("""
{
// This is a comment
name: "TJSON5 Example",
description: """
This is a multi-line
description using triple quotes
""",
values: [1, 2, 0xFF, 0b1010,], // Trailing comma is allowed
}
""")
print(data["name"]) # "TJSON5 Example"
print(data["values"]) # [1, 2, 255, 10]
# Read from a file with automatic encoding detection
config = tjson5.load_file("config.tjson5")
# Or traditional way
with open("config.tjson5", "r", encoding="utf-8") as f:
config = tjson5.load(f)
# Write to JSON (standard JSON format)
with open("output.json", "w") as f:
tjson5.dump(data, f, indent=2)
Building the Extension
# Install development dependencies
pip install cython
# Build the extension in place
python setup.py build_ext --inplace
# Run tests
python test_tjson5.py
How it Works
The parser uses a multi-stage process without any external dependencies:
- Preprocesses triple-quoted strings, converting them to standard JSON strings
- Converts hex and binary numbers to decimal
- Removes comments (both single-line and multi-line)
- Handles unquoted keys by adding proper quotes
- Removes trailing commas in objects and arrays
- Uses multiple fallback strategies for robust parsing
- Passes the processed JSON to Python's built-in JSON parser
All error positions are mapped back to the original source for accurate error reporting with helpful context.
Performance
The Cython implementation provides near-native performance, making it suitable for parsing large TJSON5 files quickly.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Project details
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file tjson5-0.1.7.tar.gz.
File metadata
- Download URL: tjson5-0.1.7.tar.gz
- Upload date:
- Size: 118.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5cd6eb0e14aa616451eaec0df6ae6d1546519d68d3e359f6c0a00b3c35e41f9c
|
|
| MD5 |
e9e6d461589ee9a3fb3bc7b0718d24fd
|
|
| BLAKE2b-256 |
392ce125f5c1eb231b8b1e099ff82e80691de20366efc9bfc38a25054aa970be
|
File details
Details for the file tjson5-0.1.7-cp312-cp312-win_amd64.whl.
File metadata
- Download URL: tjson5-0.1.7-cp312-cp312-win_amd64.whl
- Upload date:
- Size: 49.7 kB
- Tags: CPython 3.12, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cdcc6eea605493251ab75548627cdcaf3a7a02bc1012067e0835eb48bdd67599
|
|
| MD5 |
a1123adb89148f32403154509418c88f
|
|
| BLAKE2b-256 |
f3deaed1ac396e2c5eba3f9c23be9787fd48dd3d63c23401dc6e0fd9f2a86a93
|