keep comment(round-trip) when read & write jsonc/hjson, sdict with extra context(parent/keypath/merge) like benedict/deepdiff/deepmerge
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.
Install
pip install "jsonc-sdict[full]"
# pip install "jsonc-sdict[full] @ git+https://github.com/AClon314/jsonc-sdict.git"
For local development:
git clone https://github.com/AClon314/jsonc-sdict.git
cd jsonc-sdict
pip install -e ".[dev]"
Usage
jsonc
from jsonc_sdict import jsoncDict, CommentIn, NONE
jc = jsoncDict(
{
CommentIn(NONE, "a"): "// before a",
"a": 1,
CommentIn("a", "b"): "// between a and b",
"b": 2,
}
)
jc[CommentIn("b")] = {CommentIn(":", "v"): "/* before value */"}
jc["b"] = 3
print(jc.full)
Use CommentIn to mark comment positions. See test_jsonc.py.
CommentIn(left, right)means a comment between two logical items.CommentIn(key)means comments attached to one pair's internalk:/:v/v,slots.
If you start from JSONC/HJSON text instead of a mapping, use jsoncDict(raw, loads=hjson.loads, dumps=hjson.dumps).
Invalid JSONC examples
// /* this is still single-line comment
so this line is illegal */
/* // this is block comment */ trailing-text-is-illegal
sdict
See test_sdict.py.
Common operations
from jsonc_sdict import sdict
data = sdict({"node": {"items": [0, {"value": 1}]}, "a": 1, "b": 2, "c": 2})
print(data["node", "items", 1, "value"])
data["node", "items", 1, "value"] = 2
node = data["node"]
print(node.keypath)
print(node.parent is data)
data.insert({"x": 9}, key="a", after=True)
data.rename_key("x", "y")
data.sort(reverse=True)
print(list(data.items()))
print(data.unref())
merge()
from functools import partial
from jsonc_sdict import get1, merge
old = {"children": [{"id": 1, "name": "1", "old": None}]}
new = {"children": [{"id": 1, "name": "2", "new": ""}, {"id": 2, "name": "3"}]}
result = merge(
(old, new),
dictDict={"value_of_idKey": partial(get1.item, keys="id")},
unMergeable="new",
)()
print(result)
deep-Merge
merge((old, new))() is the shortest form. For list[dict], pass dictDict=... so items can be matched by an id-like key before diff/merge.
from functools import partial
from jsonc_sdict import get1, merge
old = {"items": [{"id": 1, "name": "old", "keep": True}]}
new = {"items": [{"id": 1, "name": "new"}, {"id": 2, "name": "add"}]}
merged = merge(
(old, new),
dictDict={"value_of_idKey": partial(get1.item, keys="id")},
unMergeable="new",
)()
print(merged)
CLI merge
deep-merge -i '{a:{b:1}}' '{a:{b:2}}' -fo json -m new
# {"a": {"b": 2}}
GetSetDel
See test_get_set_del.py.
from jsonc_sdict import get1, set1
from jsonc_sdict.GetSetDel import del1
obj = {"a": {"b": 1}}
print(get1.item(obj, ("a", "b")))
print(set1.item(obj, ("a", "b"), 2))
print(set1.item(obj, ("a", "c"), 3))
del1.item(obj, ("a", "b"))
print(obj)
Develop
env
LOG=DEBUG enables debug-level logging.
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 | comment |
|---|---|---|---|---|
| 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.20260517.tar.gz.
File metadata
- Download URL: jsonc_sdict-0.2.20260517.tar.gz
- Upload date:
- Size: 69.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c210fce9e548cdcb4f3c888f7831231a479a124a17404248bd9c2c4b79c98795
|
|
| MD5 |
6ab49f7cdde51b0bd0c3591f437c638e
|
|
| BLAKE2b-256 |
ac4362d4e4c69b5b411c0a99a1119d9d31e6992b8dd03d523acf152bcf023c3b
|
Provenance
The following attestation bundles were made for jsonc_sdict-0.2.20260517.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.20260517.tar.gz -
Subject digest:
c210fce9e548cdcb4f3c888f7831231a479a124a17404248bd9c2c4b79c98795 - Sigstore transparency entry: 1560911148
- Sigstore integration time:
-
Permalink:
AClon314/jsonc-sdict@06359ac8560281217f3770c580b81e27ae11709d -
Branch / Tag:
refs/tags/v0.2.20260517 - Owner: https://github.com/AClon314
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@06359ac8560281217f3770c580b81e27ae11709d -
Trigger Event:
release
-
Statement type:
File details
Details for the file jsonc_sdict-0.2.20260517-py3-none-any.whl.
File metadata
- Download URL: jsonc_sdict-0.2.20260517-py3-none-any.whl
- Upload date:
- Size: 57.1 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 |
3ac714d2965e2837b9def1d610504fea43d5147b2f414912a60f465fca38a38b
|
|
| MD5 |
00a22bdc48dce6cab0109b277212a300
|
|
| BLAKE2b-256 |
ec3fd3224c4edf0a0299f9b2b3af828f939c9942216c4b29d323db1a99e5f778
|
Provenance
The following attestation bundles were made for jsonc_sdict-0.2.20260517-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.20260517-py3-none-any.whl -
Subject digest:
3ac714d2965e2837b9def1d610504fea43d5147b2f414912a60f465fca38a38b - Sigstore transparency entry: 1560911247
- Sigstore integration time:
-
Permalink:
AClon314/jsonc-sdict@06359ac8560281217f3770c580b81e27ae11709d -
Branch / Tag:
refs/tags/v0.2.20260517 - Owner: https://github.com/AClon314
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@06359ac8560281217f3770c580b81e27ae11709d -
Trigger Event:
release
-
Statement type: