Skip to main content

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)

Installation

# Install from source
git clone https://github.com/kristofmulier/triple-json5.git
cd triple-json5/python_tjson5
pip install -e .

Usage

import tjson5parser

# Parse a TJSON5 string
data = tjson5parser.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 open("config.tjson5", "r") as f:
    config = tjson5parser.load(f)

# Write to JSON (standard JSON format)
with open("output.json", "w") as f:
    tjson5parser.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:

  1. Preprocesses triple-quoted strings, converting them to standard JSON strings
  2. Converts hex and binary numbers to decimal
  3. Removes comments
  4. Handles trailing commas
  5. 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.

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

tjson5-0.1.2.tar.gz (93.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tjson5-0.1.2-cp312-cp312-win_amd64.whl (46.4 kB view details)

Uploaded CPython 3.12Windows x86-64

File details

Details for the file tjson5-0.1.2.tar.gz.

File metadata

  • Download URL: tjson5-0.1.2.tar.gz
  • Upload date:
  • Size: 93.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for tjson5-0.1.2.tar.gz
Algorithm Hash digest
SHA256 10e98c568b9103c101e91d64a1446d6a064f13898eaefd4d9e28fc4b4d9121d8
MD5 0bdc8fef316a7f5ca3bdd31294bb2163
BLAKE2b-256 fe7ef7e57198e8974773f89c610c132631cfda58a2180eeadada431752f6bedb

See more details on using hashes here.

File details

Details for the file tjson5-0.1.2-cp312-cp312-win_amd64.whl.

File metadata

  • Download URL: tjson5-0.1.2-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 46.4 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

Hashes for tjson5-0.1.2-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 b0912e2792a3353047bc1d7107a8ab94dc0a44d4051e58739096effcfb78e9eb
MD5 b7faf891fb434cc4440cae17f65b8dbc
BLAKE2b-256 d1d4b4a54e08c69334d215a7f5879500b1c02f1ac95e798c9cb95317d0aeb3be

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page