|
3 | 3 | from time import time |
4 | 4 | import typing as t |
5 | 5 |
|
| 6 | +from piccolo.columns.column_types import JSON, JSONB |
| 7 | +from piccolo.query.mixins import ColumnsDelegate |
6 | 8 | from piccolo.querystring import QueryString |
7 | 9 | from piccolo.utils.sync import run_sync |
8 | | -from piccolo.utils.encoding import dump_json |
| 10 | +from piccolo.utils.encoding import dump_json, load_json |
9 | 11 |
|
10 | 12 | if t.TYPE_CHECKING: # pragma: no cover |
11 | 13 | from piccolo.table import Table # noqa |
| 14 | + from piccolo.query.mixins import OutputDelegate |
12 | 15 |
|
13 | 16 |
|
14 | 17 | class Timer: |
@@ -51,9 +54,54 @@ async def _process_results(self, results): |
51 | 54 | if hasattr(self, "run_callback"): |
52 | 55 | self.run_callback(raw) |
53 | 56 |
|
54 | | - raw = await self.response_handler(raw) |
| 57 | + output: t.Optional[OutputDelegate] = getattr( |
| 58 | + self, "output_delegate", None |
| 59 | + ) |
| 60 | + |
| 61 | + ####################################################################### |
| 62 | + |
| 63 | + if output and output._output.load_json: |
| 64 | + columns_delegate: t.Optional[ColumnsDelegate] = getattr( |
| 65 | + self, "columns_delegate", None |
| 66 | + ) |
| 67 | + |
| 68 | + if columns_delegate is not None: |
| 69 | + json_columns = [ |
| 70 | + i |
| 71 | + for i in columns_delegate.selected_columns |
| 72 | + if isinstance(i, (JSON, JSONB)) |
| 73 | + ] |
| 74 | + else: |
| 75 | + json_columns = self.table._meta.json_columns |
| 76 | + |
| 77 | + json_column_names = [] |
| 78 | + for column in json_columns: |
| 79 | + if column.alias is not None: |
| 80 | + json_column_names.append(column.alias) |
| 81 | + elif len(column._meta.call_chain) > 0: |
| 82 | + json_column_names.append( |
| 83 | + column.get_select_string( |
| 84 | + engine_type=column._meta.engine_type |
| 85 | + ) |
| 86 | + ) |
| 87 | + else: |
| 88 | + json_column_names.append(column._meta.name) |
| 89 | + |
| 90 | + processed_raw = [] |
| 91 | + |
| 92 | + for row in raw: |
| 93 | + new_row = {**row} |
| 94 | + for json_column_name in json_column_names: |
| 95 | + value = new_row.get(json_column_name) |
| 96 | + if value is not None: |
| 97 | + new_row[json_column_name] = load_json(value) |
| 98 | + processed_raw.append(new_row) |
55 | 99 |
|
56 | | - output = getattr(self, "output_delegate", None) |
| 100 | + raw = processed_raw |
| 101 | + |
| 102 | + ####################################################################### |
| 103 | + |
| 104 | + raw = await self.response_handler(raw) |
57 | 105 |
|
58 | 106 | if output: |
59 | 107 | if output._output.as_objects: |
|
0 commit comments