betcode-org / betfair

betfairlightweight - Betfair API-NG python wrapper (with streaming)
MIT License
418 stars 146 forks source link

Serializing and Deserializing MarketBook to/from JSON loses market_definition #517

Open vincentmele opened 1 year ago

vincentmele commented 1 year ago

I am trying to serialize and deserialize MarketBook objects. I am 1) going from MarketBook to JSON, then 2) JSON back to MarketBook. It appears that when converting JSON to MarketBook, the marketDefinition does not set itself. With a bit of trial and error, I discovered that I can get a correct MarketBook by setting the key "market_definition" instead of the "marketDefinition" in the JSON outputted from first step.

Assuming an existing, populated MarketBook object as "mb1"

Doesn't work, market_definition is not set:

import json
mb_json = mb1.json()
mb_dict = json.loads(mb_json)
new_mb = MarketBook(**mb_dict)
# new_mb.market_definition is None at this point.

Works, market_definition is set:

import json
mb_json = mb1.json()
mb_dict = json.loads(mb_json)
mb_dict['market_definition'] = mb_dict['marketDefinition']
new_mb = MarketBook(**mb_dict)
# new_mb.market_definition has correct output

If this isn't how I should be storing MarketBooks, I'd be more than happy to be corrected :-)