Skip to main content

read/write json as a object | 像读写对象属性一样读写 json

Project description

dnode

$ pip install dnode

from dnode import *


print('============== load data ===============')

data = {
    'a': 1,
    'b': {'b1': 3},
    'c': {'c1': 1, 'c2': {'c22': 22}},
    'd': ['d1', 'd2', 'd3'],
    'e': [{'ee': 1}, {'ee': 2}, {'ee': 3}],
    'f': [['f11', 'f12'], ['f21', 'f22']],
    'g': [[{'gg': 11}, {'gg': 12}], [{'gg': 21}, {'gg': 22}]],
}

obj = DNode(data)

print(obj.to_dict())
assert obj.to_dict() == data

print('=========== print object ===============')

print(obj)
obj.pprint()

print('============= print json ===============')

print(obj.json)  
# or print(obj.dumps(indent=4))

print('=========== test getattr ===============')

assert obj.a == 1
assert obj.b.b1 == 3
assert obj.c.c2 == {'c22': 22} == DNode({'c22': 22})
assert obj.d[1] == 'd2'
assert obj.e[1].ee == 2
assert obj.f[0][0] == 'f11'
assert obj.g[0][0].gg == 11
assert obj.h == None

print('=========== test setattr ===============')

obj.a = 'change_a'
obj.b.b1 = 'change_b'
obj.c.c2.c22 = 'change_c'
obj.d[1] = 'change_d'
obj.e[1].ee = 'change_e'
obj.f[0][0] = 'change_f'
obj.g[0][0].gg = 'change_g'

data = obj.to_dict()
assert data['a'] == 'change_a'
assert data['b']['b1'] == 'change_b'
assert data['c']['c2']['c22'] == 'change_c'
assert data['d'][1] == 'change_d'
assert data['e'][1]['ee'] == 'change_e'
assert data['f'][0][0] == 'change_f'
assert data['g'][0][0]['gg'] == 'change_g'

print('======== test set non-json type =========')

obj.a = {1, 2, 3}
data = obj.to_dict()
assert data['a'] == None

print('============== test clear ===============')

obj.clear()
obj.pprint()

print('============== list init ================')

data = '[ {"a": 1}, [{"b": 2}, {"c": 3}] ]'
rs = DNode(data)
print(rs)
assert isinstance(rs, list)
assert rs[0].a == 1
assert rs[1][0].b == 2
assert rs[1][1].c == 3

print('=========================================')

print('\n\n-------------- test SMNode ---------------')

print('========= test STRUCT_FIELDS ============')

obj = SMNode({})
SMNode.STRUCT_FIELDS = set(['aaa', 'bbb'])
obj.aaa = 123
obj.ccc = 456
print(obj.json)  
print('`ccc` should not in the output')

print('=========================================')

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

dnode-0.2.6.tar.gz (4.4 kB view details)

Uploaded Source

File details

Details for the file dnode-0.2.6.tar.gz.

File metadata

  • Download URL: dnode-0.2.6.tar.gz
  • Upload date:
  • Size: 4.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.4

File hashes

Hashes for dnode-0.2.6.tar.gz
Algorithm Hash digest
SHA256 2a39da6fc727c189973e51688a1212e3d534458b7d7547c1ed460238ad085d7e
MD5 82086a15080c8823be311398e95c6b2c
BLAKE2b-256 bbbe5c7542f6884d81052ba4081fab8ea8d73734db4237bb9fe1927db6bf3fe1

See more details on using hashes here.

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