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 hashes)

Uploaded Source

Built Distribution

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

Uploaded Python 3

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