|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# SPDX-FileCopyrightText: 2012-2023 MOD Audio UG |
| 3 | +# SPDX-License-Identifier: AGPL-3.0-or-later |
| 4 | +import json |
| 5 | + |
| 6 | +from tornado.util import unicode_type |
| 7 | + |
| 8 | +from mod.controller.handler.timeless_request_handler import TimelessRequestHandler |
| 9 | + |
| 10 | + |
| 11 | +class JsonRequestHandler(TimelessRequestHandler): |
| 12 | + def write(self, data): |
| 13 | + # FIXME: something is sending strings out, need to investigate what later.. |
| 14 | + # it's likely something using write(json.dumps(...)) |
| 15 | + # we want to prevent that as it causes issues under Mac OS |
| 16 | + |
| 17 | + if isinstance(data, (bytes, unicode_type, dict)): |
| 18 | + TimelessRequestHandler.write(self, data) |
| 19 | + self.finish() |
| 20 | + return |
| 21 | + |
| 22 | + elif data is True: |
| 23 | + data = "true" |
| 24 | + self.set_header("Content-Type", "application/json; charset=UTF-8") |
| 25 | + |
| 26 | + elif data is False: |
| 27 | + data = "false" |
| 28 | + self.set_header("Content-Type", "application/json; charset=UTF-8") |
| 29 | + |
| 30 | + # TESTING for data types, remove this later |
| 31 | + #elif not isinstance(data, list): |
| 32 | + #print("=== TESTING: Got new data type for RequestHandler.write():", type(data), "msg:", data) |
| 33 | + #data = json.dumps(data) |
| 34 | + #self.set_header('Content-type', 'application/json') |
| 35 | + |
| 36 | + else: |
| 37 | + data = json.dumps(data) |
| 38 | + self.set_header("Content-Type", "application/json; charset=UTF-8") |
| 39 | + |
| 40 | + TimelessRequestHandler.write(self, data) |
| 41 | + self.finish() |
0 commit comments