1
0
mirror of https://github.com/EDCD/EDMarketConnector.git synced 2025-04-13 15:57:14 +03:00

CAPIData: Make JSON encodable, and use that elsewhere

* It's no use `dict(CAPIData)` if that contains other `CAPIData`.  So,
  rather than write something to do that recursively just implement a
  simple JSON Encoder class and specify its use.
This commit is contained in:
Athanasius 2021-08-24 13:22:13 +01:00
parent f12b8be74c
commit 2e27a2ba00
No known key found for this signature in database
GPG Key ID: AE3E527847057C7D
2 changed files with 13 additions and 2 deletions

View File

@ -125,6 +125,14 @@ class CAPIData(UserDict):
self.data['lastStarport']['ships'] = {'shipyard_list': {}, 'unavailable_list': []}
class CAPIDataEncoder(json.JSONEncoder):
"""Allow for json dumping via specified encoder."""
def default(self, o):
"""Tell JSON encoder that we're actually just a dict."""
return o.__dict__
class CAPIDataRawEndpoint:
"""Last received CAPI response for a specific endpoint."""
@ -1099,7 +1107,7 @@ class Session(object):
timestamp = time.strftime('%Y-%m-%dT%H.%M.%S', time.localtime())
with open(f'dump/{system}{station}.{timestamp}.json', 'wb') as h:
h.write(json.dumps(dict(data),
h.write(json.dumps(data, cls=CAPIDataEncoder,
ensure_ascii=False,
indent=2,
sort_keys=True,

View File

@ -13,7 +13,10 @@ import util_ships
def export(data, filename=None):
string = json.dumps(companion.ship(data), ensure_ascii=False, indent=2, sort_keys=True, separators=(',', ': ')) # pretty print
string = json.dumps(
companion.ship(data), cls=companion.CAPIDataEncoder,
ensure_ascii=False, indent=2, sort_keys=True, separators=(',', ': ')
) # pretty print
if filename:
with open(filename, 'wt') as h: