Skip to main content

A Roundtrip parser and CST for JSON, JSONC and JSON5.

Project description

json5kit

A Roundtrip parser and CST for JSON, JSONC and JSON5.

JSON5 is a superset of JSON, that allows trailing commas, comments, unquoted and single-quoted object keys, and a lot more.

Currently supports parsing most JSON5 syntax, and converting it back to source. Also supports single line // comments.

Installation

pip install json5kit

Usage

>>> source = """
... {
...   items: [1, 2, 4],  // change this to 3
... }
... """
>>> import json5kit
>>> tree = json5kit.parse(source)
>>> print(tree.to_source())

{
  items: [1, 2, 4],  // change this to 3
}

>>> print(tree.to_json())
{"items":[1,2,4]}

>>> # Let's replace the `4` with `3` now:
>>> class ReplaceFourWithThree(json5kit.Json5Transformer):
...     def visit_Number(self, node):
...         if node.value == 4:
...             return node.replace(value=3)
...         return node
...
>>> ReplaceFourWithThree().visit(tree)
>>> print(tree.to_source())

{
  items: [1, 2, 3],  // change this to 3
}
>>> print(tree.to_json())
{"items":[1,2,3]}

Development / Testing

  • Clone the project:

    git clone https://github.com/tusharsadhwani/json5kit
    cd json5kit
    
  • Setup a virtual environment:

    virtualenv venv
    . venv/bin/activate
    
  • Do an editable install of the project, that way you don't have to keep reinstalling:

    pip install -r requirements-dev.txt
    
  • Run tests:

    pytest
    
  • Run type checking:

    mypy .
    

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

json5kit-0.4.0.tar.gz (8.9 kB view details)

Uploaded Source

Built Distribution

json5kit-0.4.0-py3-none-any.whl (8.7 kB view details)

Uploaded Python 3

File details

Details for the file json5kit-0.4.0.tar.gz.

File metadata

  • Download URL: json5kit-0.4.0.tar.gz
  • Upload date:
  • Size: 8.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.2

File hashes

Hashes for json5kit-0.4.0.tar.gz
Algorithm Hash digest
SHA256 0f7978bed6aa539133873bda602aa3f130ee83cfdafa93565b55a63a5d2f3490
MD5 90e211b71951a8c9801ce90e53b4917d
BLAKE2b-256 13678d48f04daae6a794f4b847f44a8368cd48597da778edc4efca35305b61c0

See more details on using hashes here.

File details

Details for the file json5kit-0.4.0-py3-none-any.whl.

File metadata

  • Download URL: json5kit-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 8.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.2

File hashes

Hashes for json5kit-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8fac2788abd070bdffbfad9bc0c7755bb7c259fa56a163b8517ba5aa4d0e2cb7
MD5 1010e8287321d15111ee85d6f23f5811
BLAKE2b-256 1b7272cf4310a3d6cab45d48cb27e5f5e8420e1b955bb5cb0ab6e5f8adfa2e8d

See more details on using hashes here.

Supported by

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