keep comment(round-trip) when read & write jsonc/hjson, sdict with extra context(parent/keypath)
Project description
Round-trip comments for JSONC/HJSON, with dict-like APIs for config editing.
- Keep comments on read and write (
loads→ edit →body/full). - Work with nested structures via
sdict≈ deepmerge + deepdiff + benedict
Comments are data too, just like codes are data too by John von Neumann
[!WARNING] This project is currently
0.2.xand still evolving.
Usage
Install
pip install "jsonc-sdict[full]"
For local development:
pip install -e ".[dev]"
Quick usage
import hjson
from jsonc_sdict import jsoncDict, CommentIn, NONE
raw = """
{
"a": 1,
"b": 2
}
""".strip()
jc = jsoncDict(raw, loads=hjson.loads, dumps=hjson.dumps)
jc["b"] = 3
jc[CommentIn(NONE, "a")] = "// inserted before a"
print(jc.full)
Advanced usage
import hjson
from jsonc_sdict import jsoncDict, CommentIn, NONE
raw = """
// header
{
"a": 1, // inline
"b": 2
}
// footer
""".strip()
jc = jsoncDict(raw, loads=hjson.loads, dumps=hjson.dumps)
jc[CommentIn(NONE, "a")] = "// before a"
jc[CommentIn("a")] = {
CommentIn("k", ":"): "/* key slot */",
CommentIn(":", "v"): "/* value slot */",
CommentIn("v", ","): "/* tail slot */",
}
print(jc.full)
Comment model
jsoncDict.comments stores comment positions with CommentIn(...) keys.
CommentIn(left, right)means a comment between two logical items.CommentIn(key)means comments attached to one pair's internal slots.- Slot comments use a dict with
CommentIn("k", ":"),CommentIn(":", "v"),CommentIn("v", ","). CommentIn(NONE, first_key)andCommentIn(last_key, NONE)handle boundary comments.
Examples:
jc.comments[CommentIn("a", "b")] = "// between a and b"
jc.comments[CommentIn("b")] = {
CommentIn("k", ":"): "/* before colon */",
CommentIn(":", "v"): "/* before value */",
}
Edge cases
Invalid JSONC examples:
// /* this is still single-line comment
so this line is illegal */
/* // this is block comment */ trailing-text-is-illegal
Develop
env
LOG=DEBUG enables debug-level logging in project loggers.
Common setup:
python -m venv .venv
source .venv/bin/activate
pip install -e ".[dev]"
LOG=DEBUG pytest -q
Internal design
jsonc
jsoncDict.loads()parses comments with tree-sitter and stores them intocomments.jsoncDict.bodyrenders the current value with comments restored.jsoncDict.fullreturnsheader + body + footer.CommentIn(key)comments are stored as slot maps, not raw strings, so key/colon/value/comma placement stays explicit.
sdict (common pitfalls)
sdictwraps both mapping and iterable nodes; nested access may returnsdictviews, not raw dict/list.jsoncDictoutput depends on comment/data mutation paths; bypassing public APIs can leave internal state inconsistent.dfs()warns against mutating yielded data during iteration.insert(update, key=...|index=...)is ordering-oriented: it inserts by reordering keys after update.
weakList (common pitfalls)
- Items must support both
__hash__and weak references (__weakref__); built-inint/str/list/dictdo not qualify. - Weak references can disappear when no strong references exist; list length can shrink unexpectedly.
WeakList(noRepeat=True)is not identical toOrderedWeakSet: repeated append/insert can move item position.
Related projects
json loads()
| pypi | commits | issues | about | lack |
|---|---|---|---|---|
| spyoungtech/json-five |
Python JSON5 parser with round-trip preservation of comments | can keep comment in another API style (e.g: BlockComment/wsc_before) |
||
| tusharsadhwani/json5kit |
A Roundtrip parser and CST for JSON, JSONC and JSON5. | |||
| dpranke/pyjson5 |
A Python implementation of the JSON5 data format | |||
| austinyu/ujson5 |
A fast JSON5 encoder/decoder for Python | |||
| qvecs/qjson5 |
📎 A quick JSON5 implementation written in C, with Python bindings. |
other format that support round-trip
Project details
Release history Release notifications | RSS feed
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 jsonc_sdict-0.2.20260516.tar.gz.
File metadata
- Download URL: jsonc_sdict-0.2.20260516.tar.gz
- Upload date:
- Size: 66.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
08a2726ee9a0f30a554a64f9ee0cfc97d228f035998af63b0d28c711f39b6fb6
|
|
| MD5 |
67546ceb4df199554122ad30d568e010
|
|
| BLAKE2b-256 |
2bcc582bc95c3c26c20988640c68a9d14cafa7f2c3a9198ac2845b7c47d8d2fa
|
Provenance
The following attestation bundles were made for jsonc_sdict-0.2.20260516.tar.gz:
Publisher:
python-publish.yml on AClon314/jsonc-sdict
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
jsonc_sdict-0.2.20260516.tar.gz -
Subject digest:
08a2726ee9a0f30a554a64f9ee0cfc97d228f035998af63b0d28c711f39b6fb6 - Sigstore transparency entry: 1553629230
- Sigstore integration time:
-
Permalink:
AClon314/jsonc-sdict@97bc5675959954eef068aa4a8730ba14aad1a3ba -
Branch / Tag:
refs/tags/v0.2.20260516 - Owner: https://github.com/AClon314
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@97bc5675959954eef068aa4a8730ba14aad1a3ba -
Trigger Event:
release
-
Statement type:
File details
Details for the file jsonc_sdict-0.2.20260516-py3-none-any.whl.
File metadata
- Download URL: jsonc_sdict-0.2.20260516-py3-none-any.whl
- Upload date:
- Size: 55.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a5ca0ebf9d75c14e92164032d1835b12d4f7daa1075a54c9d8cc4d41089d80b8
|
|
| MD5 |
325b24ad446b727e3404ca43bf1823a2
|
|
| BLAKE2b-256 |
08c6ef17f44b9bf5aaeaa39a0c15a6646b9c6dc84e22c25ac028cb706ca78f0c
|
Provenance
The following attestation bundles were made for jsonc_sdict-0.2.20260516-py3-none-any.whl:
Publisher:
python-publish.yml on AClon314/jsonc-sdict
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
jsonc_sdict-0.2.20260516-py3-none-any.whl -
Subject digest:
a5ca0ebf9d75c14e92164032d1835b12d4f7daa1075a54c9d8cc4d41089d80b8 - Sigstore transparency entry: 1553629266
- Sigstore integration time:
-
Permalink:
AClon314/jsonc-sdict@97bc5675959954eef068aa4a8730ba14aad1a3ba -
Branch / Tag:
refs/tags/v0.2.20260516 - Owner: https://github.com/AClon314
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@97bc5675959954eef068aa4a8730ba14aad1a3ba -
Trigger Event:
release
-
Statement type: