0

I am making a chess league to track elo and I am storing all the data in json files. However when I use:

    json.dumps((objectToSerialize, default=lambda o: o.__dict__,
                          sort_keys=True, indent=4, separators=(',', ':'))

The object I am trying to serialize:

    class Player:
        def __init__(self, rating, name):
            self.rating = rating
            self.name = name

instead of it making a string like this

    {
                "name": "jack",
                "rating": 1500
    },

it is like this:


    [
                "jack",
                1500
    ],

It is important that the values are given identifiers as when I read the data in the program, it cannot be understood.

Is there something I am doing wrong or is there a fix for this?

vvvvv
  • 25,404
  • 19
  • 49
  • 81
Haxor
  • 19
  • 3
  • does this answer your question? https://stackoverflow.com/questions/3768895/how-to-make-a-class-json-serializable – QWERTYL Feb 21 '22 at 22:21
  • @QWERTYL I’ll look into it thanks. – Haxor Feb 21 '22 at 22:26
  • The way you have it setup I have no problems getting this to work. Are you actually creating the player class, and not just sending a tuple of the name/rating to be serialized? – Robert Criqui Feb 21 '22 at 22:28
  • @RobertCriqui I have a class called league which in turn has a players value and I append to that list with a new player object, then I write to the json file. – Haxor Feb 21 '22 at 22:49
  • @Haxor Can you provide a little more of the code to show exactly what the object you're serializing is? – Robert Criqui Feb 23 '22 at 14:19

0 Answers0