Skip to main content

An overpowered JSON-with-comments parser with inplace edition

Project description

jsoncgx: An overpowered JSON-with-comments parser with inplace edition

Cool, a JSONC parser for Python. How do I load my file into a dict?

I'll stop you right here. If you're only looking at simply deserialising JSONC files, this library is not for you.

jsoncgx is developped with 3 intentions:

  1. Supporting C-style // and C++-style /**/ comments. This part is typical, there are lots of other libraries offering this functionality.
  2. Offering more flexibility with the JSON syntax that your usual straight-to-Python-types parser. Namely, the JSON format allows to have multiple name:value pairs with the same name in the same object, but that is not supported by Python's dicts.
  3. Inplace edition, that is, being able to replace a chunk of a given JSONC file without messing up its indentation or comments. Typically, libraries only offer indentation imitation, that is, deserialisation makes guesses about the indentation, and serialisation reproduces it.

Installation

This library is available on PyPI:

pip install --user jsoncgx

Comparison / Quickstart / Demo

Standard Python module

jsoncgx

Loading JSON data
>>> import json
>>> data = json.loads('{ "answer": 42 }')
>>> data["answer"]
42
>>> import jsoncgx
>>> editor = jsoncgx.loads('{ "answer": 42 }')
>>> editor.root["answer"][0]
42
Editing JSON data
>>> data["answer"] = "spam"
>>> json.dumps(data)
'{"answer": "spam"}'
>>> editor.root.editreplacevalue(0, "spam")
>>> editor.dumps()
'{ "answer": "spam" }'
Working with JSON files
>>> with open("in.json") as f:
...   data = json.load(f)
...
>>> with open("out.json", "w") as f:
...   json.dump(data, f)
...
>>> editor = json.loadf("in.json")
>>> editor.dumpf("out.json")

Or use jsoncgx.load() and jsoncgx.JSONEditor.dump() with a file handle, like the standard module.

Non unique name
>>> data = json.loads(
...     '{ "answer": 42, "answer": "spam" }')
>>> data["answer"]
'spam'
>>> editor = jsoncgx.loads(
...     '{ "answer": 42, "answer": "spam" }')
>>> editor.root["answer"][0]
42
>>> editor.root["answer"][1]
'spam'
Comments
>>> data = json.loads(
...     '{ "answer": /* eggs and */ 42 }')
Traceback (most recent call last):
...
json.decoder.JSONDecodeError: ...
>>> editor = jsoncgx.loads(
...     '{ "answer": /* eggs and */ 42 }')
>>> editor.root.editreplacevalue(0, "spam")
>>> editor.dumps()
'{ "answer": /* eggs and */ "spam" }'

What syntax does this parse exactly?

ECMA-404 with the following additional lexical elements:

  • regex //[^\n]*\n, that is, the Unicode scalar sequence:
    • U+002F,
    • U+002F,
    • any amount of any scalars except U+000A,
    • U+000A.
  • lazy regex /\*.*\*/, that is, the Unicode scalar sequence:
    • U+002F,
    • U+002A,
    • any amount of any scalars except the sequence U+002A U+002F
    • U+002A,
    • U+002F.

Those elements are considered like whitespace.

Architecture

jsoncgx architecture

Changelog

V1.1:

  • JSONNumber, JSONString, JSONBool, and JSONNull have been replaced with JSONBuiltin, for easier integration.
  • edit*() methods that accepted JSONValue now also accept int, float, str, bool, and None. They are automatically turned into JSONBuiltin.

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

jsoncgx-1.1.tar.gz (35.0 kB view details)

Uploaded Source

Built Distribution

jsoncgx-1.1-py3-none-any.whl (35.9 kB view details)

Uploaded Python 3

File details

Details for the file jsoncgx-1.1.tar.gz.

File metadata

  • Download URL: jsoncgx-1.1.tar.gz
  • Upload date:
  • Size: 35.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.0.0 CPython/3.12.3

File hashes

Hashes for jsoncgx-1.1.tar.gz
Algorithm Hash digest
SHA256 1b3b736e6f7c4092dd5b8db2547cc47c78e2b577a436ddcff34214dc551f94a6
MD5 6a4d858583799987214020449c9b2667
BLAKE2b-256 9d74fd5c51ff08120e8ec8b55cf4ce57bef8774e04746524314308582f9046ab

See more details on using hashes here.

File details

Details for the file jsoncgx-1.1-py3-none-any.whl.

File metadata

  • Download URL: jsoncgx-1.1-py3-none-any.whl
  • Upload date:
  • Size: 35.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.0.0 CPython/3.12.3

File hashes

Hashes for jsoncgx-1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 6e14449a67d538411936cf08a726790c3d934f69f7ecc48b0a6a01a0b2cfca26
MD5 bf291a4c5df461ffa5a625bb2dde7eec
BLAKE2b-256 74cf7b4915e4ce24811c4be32a6c425bfb1a4d6d70adae4aa621544c91db0453

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