honey-team / ufpy

Ufpy (Useful Python) - package for Python with some useful features
https://honey-team.github.io/ufpy-website
MIT License
3 stars 3 forks source link

`JsonFileUpdater` class #48

Open bleudev opened 2 months ago

bleudev commented 2 months ago

Add JsonFileUpdater class for simplifying worfking with json files.

file.json:

{
'hello': 'world'
}
f = JsonFileUpdater('file.json')
f['hello'] # 'world'
f['hi'] = 12

file.json after this code:

{
'hello': 'world',
'hi': 12
}

Also i suggest add support for with operator (json.loads -> change keys with [] operator -> json.dumps and write):

with f:
    f['hi'] = 12
    f['hello'] = 34.2

Also i suggest add support for custom loads() and dumps() functions and for indent (if custom loads() or dumps() are provided, they must support indent argument).

f = JsonFileUpdater('file.json', loads=ujson.loads, dumps=ujson.dumps)
f2 = JsonFileUpdater('file.json', indent=4)