Python Forum
Object of type Scoreboard is not JSON serializable
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Object of type Scoreboard is not JSON serializable
#1
Object of type Scoreboard is not JSON serializable

IS ANY OTHER WAY TO serialize this OBJECT ... ? trying a scoreboard for a game...

>>> %Run snake-points-pause-sounds-highscores2.py
pygame 1.9.6
Hello from the pygame community. https://www.pygame.org/contribute.html
Traceback (most recent call last):
File "F:\OneDrive\OneDrive\macOS_MBP2016\2019mbp\Clients\Snake - PyGame\snake-points-pause-sounds-highscores2.py", line 424, in <module>
drawGameOver(surface, data.username, data.points)
File "F:\OneDrive\OneDrive\macOS_MBP2016\2019mbp\Clients\Snake - PyGame\snake-points-pause-sounds-highscores2.py", line 115, in drawGameOver
board = scoreRecord(username, points)
File "F:\OneDrive\OneDrive\macOS_MBP2016\2019mbp\Clients\Snake - PyGame\snake-points-pause-sounds-highscores2.py", line 355, in scoreRecord
serialize("hightscores.txt", board)
File "F:\OneDrive\OneDrive\macOS_MBP2016\2019mbp\Clients\Snake - PyGame\snake-points-pause-sounds-highscores2.py", line 285, in serialize
json.dump(players, f)
File "C:\Users\lwdls\Thonny\lib\json\__init__.py", line 179, in dump
for chunk in iterable:
File "C:\Users\lwdls\Thonny\lib\json\encoder.py", line 438, in _iterencode
o = _default(o)
File "C:\Users\lwdls\Thonny\lib\json\encoder.py", line 179, in default
raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type Scoreboard is not JSON serializable


# hghscores - start

def deserialize(fileName):
    exists = os.path.isfile(fileName)
    if exists:
        # Store configuration file values
        f = open(fileName, 'r')
        board = json.load(f)
        f.close()    
    else:
        # Keep presets
        board = Scoreboard()
        
    return board


def serialize(fileName, players):
    f = open(fileName, 'w')
    json.dump(players, f)
    f.close()


class GameEntry:
  """Represents one entry of a list of high scores."""

  def __init__(self, name, score):
    """Create an entry with given name and score."""
    self._name = name
    self._score = score

  def get_name(self):
    """Return the name of the person for this entry."""
    return self._name
    
  def get_score(self):
    """Return the score of this entry."""
    return self._score

  def __str__(self):
    """Return string representation of the entry."""
    return '({0}, {1})'.format(self._name, self._score) # e.g., '(Bob, 98)'


class Scoreboard:
  """Fixed-length sequence of high scores in nondecreasing order."""

  def __init__(self, capacity=10):
    """Initialize scoreboard with given maximum capacity.

    All entries are initially None.
    """
    self._board = [None] * capacity        # reserve space for future scores
    self._n = 0                            # number of actual entries

  def __getitem__(self, k):
    """Return entry at index k."""
    return self._board[k]

  def __str__(self):
    """Return string representation of the high score list."""
    return '\n'.join(str(self._board[j]) for j in range(self._n))

  def add(self, entry):
    """Consider adding entry to high scores."""
    score = entry.get_score()

    # Does new entry qualify as a high score?
    # answer is yes if board not full or score is higher than last entry
    good = self._n < len(self._board) or score > self._board[-1].get_score()

    if good:
      if self._n < len(self._board):        # no score drops from list
        self._n += 1                        # so overall number increases

      # shift lower scores rightward to make room for new entry
      j = self._n - 1
      while j > 0 and self._board[j-1].get_score() < score:
        self._board[j] = self._board[j-1]   # shift entry from j-1 to j
        j -= 1                              # and decrement j
      self._board[j] = entry                # when done, add new entry


def scoreRecord(username, points):
    board = deserialize("hightscores.txt")

    entry = GameEntry(username, points)
    board.add(entry)
            
    serialize("hightscores.txt", board)
        
    return board
       
Reply


Messages In This Thread
Object of type Scoreboard is not JSON serializable - by lsepolis123 - Jul-30-2019, 09:01 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  declaring object parameters with type JonWayn 2 912 Dec-13-2022, 07:46 PM
Last Post: JonWayn
  Help to make a shuffleboard scoreboard Slett1 1 1,140 Apr-28-2022, 06:18 PM
Last Post: deanhystad
  Deserialize Complex Json to object using Marshmallow tlopezdh 2 2,133 Dec-09-2021, 06:44 PM
Last Post: tlopezdh
Star Type Error: 'in' object is not callable nman52 3 3,421 May-01-2021, 11:03 PM
Last Post: nman52
  finding and deleting json object GrahamL 1 4,854 Dec-10-2020, 04:11 PM
Last Post: bowlofred
  Serializable JarredAwesome 4 2,247 Nov-19-2020, 11:50 PM
Last Post: JarredAwesome
  TypeError: 'type' object is not subscriptable Stef 1 4,544 Aug-28-2020, 03:01 PM
Last Post: Gribouillis
  isinstance() always return true for object type check Yoki91 2 2,581 Jul-22-2020, 06:52 PM
Last Post: Yoki91
  AttributeError: type object 'FunctionNode' has no attribute '_TestValidateFuncLabel__ binhduonggttn 0 2,256 Feb-19-2020, 11:29 AM
Last Post: binhduonggttn
  Type hinting - return type based on parameter micseydel 2 2,513 Jan-14-2020, 01:20 AM
Last Post: micseydel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020