Skip to main content

keep comment(round-trip) when read & write jsonc/json5, signal dict with recording extra context(parent/keypath)

Project description

jsonc_sdict 即典

maybe is the only solution support round-trip(keep comments both for read&write) for jsonc/hjson, with easy api design.

bundled sdict = deepmerge + deepdiff + benedict

bundled weakList & OrderedWeakList because cpython has not implemented.

[!WARNING] some code was written by AI, and I'm re-writing/cleanup code in the future.

Usage

jc = jsonc(my_dict)
# or jsonc(json.loads(my_str), my_str)
# jc.update(hjson.loads(jc.body)) # if my_str
jc["//unique-keyname"] = "my comment but at end of body"
jc.insert_comment({
    "/*\\nunique-keyname-1": "my multi-\\nline comments\\n"
    "//\\nunique-keyname-2": "my line-above comments"
    "//your data key overlap with comment-keyname rule?" + DATA: ["treat as data, not comment"] # rare edge case
    }, "existedKey"
)
jc.body = hjson.dumps(jc)
f.write(jc.body)

comment keyname rule

TODO: english version

{
    '//0<SEED>': ' "": null, # dict内的顺序与真实的注释出现位置一致
    '0': 0,
    '//1<SEED>': ' 0', # 行内注释,在dict内的排序需要在真实数据的后面。
    '//2<SEED>': ' "1": 1,/* 1 */\n', # 行上注释,则末尾应该有个\n
    '/*,3<SEED>': ' 2 ', # 不应为`/*`,而应为`/*,`,其中prefix[0:1]=="/*",而prefix[2]==","代表这个多行注释应该`放到一个逗号前面`的意思; 多行注释按`向下查找的栈`规则,所以需要放到真实数据 "2": 2的前面。
    '2': 2,
    '/*\n4<SEED>': ' 👻 ', # `/*\n`代表多行注释的末尾是一个换行符,也相当于多行版本的行上注释
    '/*,5<SEED>': ' 3\n  4\n  * 5\n  ', # 不应为`/*`,而应为`/*,`
    '3': 3,
    '/*v6<SEED>': ' 6 ', # `/*v` 代表多行注释应该出现在实际的value槽位之前
    '6//': 6,
    '/*k7<SEED>': ' 7 ', # `/*k` 代表多行注释应该出现在实际的key槽位之前
    '7': 7,
    '/*\n8<SEED>': ' 8 ', # `/*\n`代表多行注释应该在**下方**kv数据的末尾,然后是换行符(这一点不要与 "//...": "...\n"行上注释末尾必须是\n 的规则混淆了)
    '8': 8,
    '//9<SEED>': ' "9": 9',
    '/-node<SEED>': jsonc({'just ignore these': 'i am in node comment'}),
    'node': jsonc({'do not ignore me': 'i am real data!'}),
}

同理,/*:代表多行注释应该出现在冒号前面,key槽位的后面。

json loads() substitute

pypi commits issues about lack
spyoungtech/json-five ⭐ 🕒 LAST🕒 🎯 🎯close 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 ⭐ 🕒 LAST🕒 🎯 🎯close A Roundtrip parser and CST for JSON, JSONC and JSON5.
dpranke/pyjson5 ⭐ 🕒 LAST🕒 🎯 🎯close A Python implementation of the JSON5 data format
austinyu/ujson5 ⭐ 🕒 LAST🕒 🎯 🎯close A fast JSON5 encoder/decoder for Python
qvecs/qjson5 ⭐ 🕒 LAST🕒 🎯 🎯close 📎 A quick JSON5 implementation written in C, with Python bindings.

other format that support round-trip

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

jsonc_sdict-0.1.20260302.tar.gz (38.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

jsonc_sdict-0.1.20260302-py3-none-any.whl (30.1 kB view details)

Uploaded Python 3

File details

Details for the file jsonc_sdict-0.1.20260302.tar.gz.

File metadata

  • Download URL: jsonc_sdict-0.1.20260302.tar.gz
  • Upload date:
  • Size: 38.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for jsonc_sdict-0.1.20260302.tar.gz
Algorithm Hash digest
SHA256 6e6de4d898ebe81976a33852f43cb6e6f6fb727153415aa36c541f21d0b49bd1
MD5 6db2438dc205843e01a9a1bb7c77fee6
BLAKE2b-256 d7a59d5cdb1a710193f94809a25559521baecb9c9f6b988d064cf20d48036168

See more details on using hashes here.

Provenance

The following attestation bundles were made for jsonc_sdict-0.1.20260302.tar.gz:

Publisher: python-publish.yml on AClon314/jsonc-sdict

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file jsonc_sdict-0.1.20260302-py3-none-any.whl.

File metadata

File hashes

Hashes for jsonc_sdict-0.1.20260302-py3-none-any.whl
Algorithm Hash digest
SHA256 b31ed6b98e2b9045afaa749d91d86c5445a1d5df66b12cdacfc63556b754a983
MD5 b1b92bace809218fdf5f4d8198c2de03
BLAKE2b-256 254ef72b93e5df1023659f27823175e7d8f2e7a7eca15e80f3708875fbe0c83e

See more details on using hashes here.

Provenance

The following attestation bundles were made for jsonc_sdict-0.1.20260302-py3-none-any.whl:

Publisher: python-publish.yml on AClon314/jsonc-sdict

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page