0

I'm trying to make a dict into a template (a schema?).

prod_item_template = {'prod_name': '',
                      'prod_quant': 0,
                      'prod_id': '',
                      'added_time': '',
                      '_template_version_': '0.0.1'}

prod_items will be saved to a json file later.

I've searched for answers and people suggest to use a class instead. However it won't work since items instantiating that class won't be able to serialized to json automatically.

I'm looking for best practice for this task so the answer doesn't have to be limited to dict.

spacegoing
  • 5,056
  • 5
  • 25
  • 42
  • 3
    What do you mean by "template"? – martineau May 26 '17 at 17:58
  • @martineau as an instance of a class / or a schema for a database? You can get some insights [here][https://stackoverflow.com/questions/19673376/is-it-possible-to-create-a-dictionary-template-in-python] but the problem with that answer is it doesn't work for `json` serialization. – spacegoing May 26 '17 at 18:36
  • In that case, you could make it a class and use (the first part, with a `to_json()` method) of my answer to the question [**_Making object JSON serializable with regular encoder_**](https://stackoverflow.com/questions/18478287/making-object-json-serializable-with-regular-encoder). – martineau May 26 '17 at 20:20

0 Answers0