|
1 | 1 | """ |
2 | | - pyexcel_text.jsonr |
| 2 | + pyexcel_text.json |
3 | 3 | ~~~~~~~~~~~~~~~~~~~ |
4 | 4 |
|
5 | | - Render json input |
| 5 | + Provide json output |
6 | 6 |
|
7 | 7 | :copyright: (c) 2014-2016 by C. W. |
8 | 8 | :license: New BSD |
9 | 9 | """ |
10 | | -import pyexcel.constants as constants |
11 | | -from pyexcel.parser import AbstractParser |
12 | | -from pyexcel.plugins.sources.pydata.common import ( |
13 | | - ArrayReader, RecordsReader, DictReader) |
14 | | -from pyexcel.plugins.sources.pydata.bookdict import BookDictSource |
15 | 10 | import json |
16 | | - |
17 | | - |
18 | | -class JsonParser(AbstractParser): |
19 | | - def parse_file(self, file_name, **keywords): |
20 | | - with open(file_name, 'r') as f: |
21 | | - content = json.load(f) |
22 | | - return as_a_dict_of_2_dimensional_array(content, **keywords) |
23 | | - |
24 | | - def parse_file_stream(self, file_stream, **keywords): |
25 | | - content = json.load(file_stream) |
26 | | - return as_a_dict_of_2_dimensional_array(content, **keywords) |
27 | | - |
28 | | - def parse_file_content(self, file_content, **keywords): |
29 | | - content = json.loads(file_content) |
30 | | - return as_a_dict_of_2_dimensional_array(content, **keywords) |
31 | | - |
32 | | - |
33 | | -def as_a_dict_of_2_dimensional_array( |
34 | | - content, sheet_name=constants.DEFAULT_NAME, |
35 | | - **keywords): |
36 | | - if isinstance(content, list): |
37 | | - if isinstance(content[0], list): |
38 | | - array_reader = ArrayReader(content, **keywords) |
39 | | - return {sheet_name: array_reader.to_array()} |
40 | | - elif isinstance(content[0], dict): |
41 | | - records_reader = RecordsReader(content, **keywords) |
42 | | - return {sheet_name: records_reader.to_array()} |
43 | | - else: |
44 | | - raise ValueError("Unknow file format") |
45 | | - elif isinstance(content, dict): |
46 | | - try: |
47 | | - keys = list(content.keys()) |
48 | | - first_item = content.get(keys[0])[0] |
49 | | - except Exception as e: |
50 | | - print(e) |
51 | | - raise |
52 | | - if isinstance(first_item, list): |
53 | | - bookdict = BookDictSource(content, **keywords) |
54 | | - return bookdict.get_data() |
55 | | - else: |
56 | | - dict_reader = DictReader(content, **keywords) |
57 | | - return {sheet_name: dict_reader.to_array()} |
| 11 | +import datetime |
| 12 | +from pyexcel.renderer import Renderer |
| 13 | + |
| 14 | + |
| 15 | +class Jsonifier(Renderer): |
| 16 | + |
| 17 | + def render_sheet(self, sheet): |
| 18 | + content = jsonify(sheet, self._file_type, self._write_title) |
| 19 | + self._stream.write(content) |
| 20 | + |
| 21 | + def render_book(self, book): |
| 22 | + content = jsonify_book(book, self._file_type) |
| 23 | + self._stream.write(content) |
| 24 | + |
| 25 | + |
| 26 | +def jsonify(sheet, file_type, write_title): |
| 27 | + content = "" |
| 28 | + table = sheet.to_array() |
| 29 | + if hasattr(sheet, 'rownames'): |
| 30 | + colnames = sheet.colnames |
| 31 | + rownames = sheet.rownames |
| 32 | + # In the following, row[0] is the name of each row |
| 33 | + if colnames and rownames: |
| 34 | + table = dict((row[0], dict(zip(colnames, row[1:]))) |
| 35 | + for row in table[1:]) |
| 36 | + elif colnames: |
| 37 | + table = sheet.to_records() |
| 38 | + elif rownames: |
| 39 | + table = sheet.to_records() |
| 40 | + else: |
| 41 | + table = list(table) |
| 42 | + if write_title: |
| 43 | + content = {sheet.name: table} |
| 44 | + else: |
| 45 | + content = table |
| 46 | + return json.dumps(content, sort_keys=True, default=_serializer) |
| 47 | + |
| 48 | + |
| 49 | +def jsonify_book(book, file_type): |
| 50 | + return json.dumps(book.to_dict(), sort_keys=True, |
| 51 | + default=_serializer) |
| 52 | + |
| 53 | + |
| 54 | +def _serializer(obj): |
| 55 | + if isinstance(obj, datetime.datetime): |
| 56 | + return obj.strftime('%Y-%m-%d %H:%M:%S.%f') |
| 57 | + elif isinstance(obj, datetime.date): |
| 58 | + return obj.strftime('%Y-%m-%d') |
| 59 | + return str(obj) |
0 commit comments