JSON Helpers
Project description
jsonlib
The jsonlib
package provides utility functions for reading and writing JSON data. It supports the following features:
- Easy Read/Write
- Works Just Like The
json
Library - Supports
datetime/date
Objects - Supports
dataclasses
(Python 3.7+) - Preserve Ordering
Installation
pip install jsonlib
Easy Read/Write
Read
import jsonlib
data = jsonlib.read('/foo/bar.json')
Write
import jsonlib
jsonlib.write({'foo': 'bar'}, '/foo/bar.json')
Works Just Like The json
Library
Load
import jsonlib
with open('/foo/bar.json', 'r') as jsonfile:
data = jsonlib.load(jsonfile)
Loads
import jsonlib
data = jsonlib.loads('{"foo": "bar"}')
Dump
import jsonlib
with open('/foo/bar.json', 'w') as jsonfile:
jsonlib.dump({'foo': 'bar'}, jsonfile)
Dumps
import jsonlib
data = {
'apple': 'crumble',
'banana': 'split',
}
# Use standard options.
text = jsonlib.dumps(data, jsonfile, indent=2, sort_keys=True)
Supports date/datetime
Objects
import datetime
import jsonlib
before = {
'date': datetime.date.today(),
'timestamp': datetime.datetime.now(),
}
jsonlib.write(before, '/foo/bar.json')
after = jsonlib.read('/foo/bar.json')
assert before == after # True
Supports dataclasses
(Python 3.7+)
from dataclasses import dataclass
import jsonlib
@dataclass
class Fruit:
apple: str
banana: str
before = Fruit(apple='Fuji', banana='Lady Finger')
jsonlib.write(before, '/foo/bar.json')
after = jsonlib.read('/foo/bar.json')
assert before == after # True
Preserve Ordering - i.e. OrderedDict
Objects
import collections
import jsonlib
before = collections.OrderedDict([('banana', 'split'), ('apple', 'crumble')])
text = jsonlib.dumps(before, indent=2)
after = jsonlib.loads(text, ordered=True)
assert before == after # True
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
jsonio-0.1.2.tar.gz
(5.1 kB
view hashes)
Built Distribution
Close
Hashes for jsonio-0.1.2-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0b0caf7dc5088035dfb2a3c5c0cbbfac3c9b71af12d70e8bb80b928b30dd81a5 |
|
MD5 | 884e7d02a5cb3f67b4df6261ac941030 |
|
BLAKE2b-256 | 72f8ff9328f0fe1a6a6dc36935dcc3d46800a45a4e97c09e42bb26bd82135202 |