keep comment(round-trip) when read & write jsonc/hjson, sdict with extra context(parent/keypath), weakList & OrderedWeakSet
Project description
Round-trip comments for JSONC/HJSON, with dict-like APIs that stay simple & easy for app config editing.
- Keep comments on read and write (
loads→ edit →restore). - Work with nested structures via
sdict≈ deepmerge + deepdiff + benedict - Use weak-reference ordered containers via
weakList/OrderedWeakSet.
[!WARNING] This project is currently alpha (
0.1.x) and still being refactored.
Usage
Install
pip install jsonc-sdict
For local development:
pip install -e ".[dev]"
Quick start
import json
import hjson
from jsonc_sdict import jsonc, AS_DATA
raw = """
// header
{
"a": 1, // inline
"b": 2
}
// footer
"""
def loads(self, obj):
return hjson.loads(obj, ...) # pre-fill your custom args here
jc = jsoncDict(raw, loads, dumps=hjson.dumps)
jc.insert_comment(
{
"/*\\nnew-block": "multi\\nline\\n",
"//\\nnew-line-above": "line above b\\n",
"//this-is-data" + AS_DATA: ["not a comment key"],
},
key="b",
)
print(jc.full)
Comment keyname rule
jsonc stores comments as synthetic keys in the underlying mapping:
<prefix><position-marker><id><SEED>
SEEDis auto-appended to mark an internal comment key.- Add
AS_DATAsuffix to force a key starting with comment prefix to be treated as normal data.
Common forms:
| Internal key prefix | Means | Restored shape |
|---|---|---|
// |
single-line comment, inline mode | after current value/comma |
//\n |
single-line comment, line-above mode | independent line before next key/value |
/* |
block comment (default) | inline block comment |
/*\n |
block comment with trailing newline mode | rendered with line break behavior |
/*, |
block comment before comma | placed before comma of current item |
/*k |
block comment before key slot | before JSON key token |
/*: |
block comment before colon slot | between key and value |
/*v |
block comment before value slot | after colon, before value |
/- |
node comment | comments out a whole subtree (KDL-like style) |
Example mapping shape:
{
"//0<SEED>": ' "": null,',
"0": 0,
"//1<SEED>": " 0",
"//\n2<SEED>": ' "1": 1,/* 1 */',
"/*,3<SEED>": " 2 ",
"2": 2,
"/*\n4<SEED>": " 👻 ",
"/*v6<SEED>": " 6 ",
"6//": 6,
"/*k7<SEED>": " 7 ",
"7": 7,
"/-node<SEED>": {"ignored": "node comment"},
"node": {"kept": "real data"},
}
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
Comments are data too!
- Inline comment (
//...): stored as//<id><SEED>, restored near the current item. - Line-above comment (
//\n...): stored as line-above mode, restored before next item. - Block comment (
/*...*/): stored with mode markers (/*,/*,,/*k,/*:,/*v,/*\n) to preserve placement. - Node comment (
/-name): special key style that comments out a full subtree, similar to KDL config style.
sdict (common pitfalls)
sdictwraps both mapping and iterable nodes; nested access may returnsdictviews, not raw dict/list.- Cache fields (for example
body,body_restored) rely on mutation hooks; bypassing APIs can leave stale cache. 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, but in AST-tree style with lots of re-defined concepts (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.1.20260304.tar.gz.
File metadata
- Download URL: jsonc_sdict-0.1.20260304.tar.gz
- Upload date:
- Size: 41.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
026355381af1073b49e527c7f4fa0b464fcf130bb3b3468ed2baeb484d3ee214
|
|
| MD5 |
534b91924b8b07a16676d5f9e7e17011
|
|
| BLAKE2b-256 |
2142ae6fe5aed9e612c1edd2b87831e99de90fed5e55111fabeade2ad40f945c
|
Provenance
The following attestation bundles were made for jsonc_sdict-0.1.20260304.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.1.20260304.tar.gz -
Subject digest:
026355381af1073b49e527c7f4fa0b464fcf130bb3b3468ed2baeb484d3ee214 - Sigstore transparency entry: 1029684582
- Sigstore integration time:
-
Permalink:
AClon314/jsonc-sdict@e2362b3c0655739f7ddc23c59837f00c5c7b650a -
Branch / Tag:
refs/tags/v0.1.20260304 - Owner: https://github.com/AClon314
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@e2362b3c0655739f7ddc23c59837f00c5c7b650a -
Trigger Event:
release
-
Statement type:
File details
Details for the file jsonc_sdict-0.1.20260304-py3-none-any.whl.
File metadata
- Download URL: jsonc_sdict-0.1.20260304-py3-none-any.whl
- Upload date:
- Size: 31.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9fcc311831be0d2fc5bb437a43720127d19a465c8f1c3ae74af4157834835107
|
|
| MD5 |
a18e8001e5022bb3b19045ab4842d53c
|
|
| BLAKE2b-256 |
d55617f5a919b583168a8e6bf2da9c27692eea5bac6caa7de4c0f7f2c6201a12
|
Provenance
The following attestation bundles were made for jsonc_sdict-0.1.20260304-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.1.20260304-py3-none-any.whl -
Subject digest:
9fcc311831be0d2fc5bb437a43720127d19a465c8f1c3ae74af4157834835107 - Sigstore transparency entry: 1029684594
- Sigstore integration time:
-
Permalink:
AClon314/jsonc-sdict@e2362b3c0655739f7ddc23c59837f00c5c7b650a -
Branch / Tag:
refs/tags/v0.1.20260304 - Owner: https://github.com/AClon314
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@e2362b3c0655739f7ddc23c59837f00c5c7b650a -
Trigger Event:
release
-
Statement type: