mirror of
https://github.com/EDCD/EDMarketConnector.git
synced 2025-04-17 17:42:20 +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:
parent
f12b8be74c
commit
2e27a2ba00
10
companion.py
10
companion.py
@ -125,6 +125,14 @@ class CAPIData(UserDict):
|
|||||||
self.data['lastStarport']['ships'] = {'shipyard_list': {}, 'unavailable_list': []}
|
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:
|
class CAPIDataRawEndpoint:
|
||||||
"""Last received CAPI response for a specific endpoint."""
|
"""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())
|
timestamp = time.strftime('%Y-%m-%dT%H.%M.%S', time.localtime())
|
||||||
with open(f'dump/{system}{station}.{timestamp}.json', 'wb') as h:
|
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,
|
ensure_ascii=False,
|
||||||
indent=2,
|
indent=2,
|
||||||
sort_keys=True,
|
sort_keys=True,
|
||||||
|
@ -13,7 +13,10 @@ import util_ships
|
|||||||
|
|
||||||
def export(data, filename=None):
|
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:
|
if filename:
|
||||||
with open(filename, 'wt') as h:
|
with open(filename, 'wt') as h:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user