diff options
| author | Kenneth Reitz <me@kennethreitz.com> | 2010-09-25 17:18:51 -0400 |
|---|---|---|
| committer | Kenneth Reitz <me@kennethreitz.com> | 2010-09-25 17:18:51 -0400 |
| commit | 9f26c23eb50f6d00e04b4fe4bd5e5e98b512cfbe (patch) | |
| tree | 043f062e3eeb93939ec368dcd8c40b0e63b418a8 /tablib/formats/_json.py | |
| parent | dc21825f346e2c422c7a3ebc180cce62726aaaa7 (diff) | |
| parent | 8136f4b09eac5ed160fd49ec70f2c1338df6ec8d (diff) | |
| download | tablib-0.8.0.tar.gz | |
Merge branch 'release/0.8.0'v0.8.0
Diffstat (limited to 'tablib/formats/_json.py')
| -rw-r--r-- | tablib/formats/_json.py | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/tablib/formats/_json.py b/tablib/formats/_json.py new file mode 100644 index 0000000..1f92b58 --- /dev/null +++ b/tablib/formats/_json.py @@ -0,0 +1,38 @@ +# -*- coding: utf-8 -*- + +""" Tablib - JSON Support +""" + +import simplejson as json +import tablib.core + +title = 'json' +extentions = ('json', 'jsn') + + +def export_set(dataset): + """Returns JSON representation of Dataset.""" + return json.dumps(dataset.dict) + + +def export_book(databook): + """Returns JSON representation of Databook.""" + return json.dumps(databook._package()) + + +def import_set(dset, in_stream): + """Returns dataset from JSON stream.""" + + dset.wipe() + dset.dict = json.loads(in_stream) + + +def import_book(dbook, in_stream): + """Returns databook from JSON stream.""" + + dbook.wipe() + for sheet in json.loads(in_stream): + data = tablib.core.Dataset() + data.title = sheet['title'] + data.dict = sheet['data'] + dbook.add_sheet(data) |
