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
#7
Look at jsonpickle.
A quick example and i have removed getter/setter which in not popular or not needed for simple simple attribute access.
For more advance attribute access that return a value can use @property.
import jsonpickle

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 __str__(self):
    """Return string representation of the entry."""
    return f'({self.name}, {self.score})' # e.g., '(Bob, 98)'

obj = GameEntry('Bob', 98)
frozen = jsonpickle.encode(obj)

# To disk
with open('score.json', 'w') as j_out:
    print(frozen, file=j_out)

# From disk
with open('score.json') as j:
    frozen_obj = jsonpickle.decode(j.read())
Test frozen_obj:
>>> frozen_obj.name
'Bob'
>>> frozen_obj.score
98

>>> print(frozen_obj)
(Bob, 98)
Reply


Messages In This Thread
RE: Object of type Scoreboard is not JSON serializable - by snippsat - Jul-31-2019, 02:39 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  declaring object parameters with type JonWayn 2 934 Dec-13-2022, 07:46 PM
Last Post: JonWayn
  Help to make a shuffleboard scoreboard Slett1 1 1,165 Apr-28-2022, 06:18 PM
Last Post: deanhystad
  Deserialize Complex Json to object using Marshmallow tlopezdh 2 2,168 Dec-09-2021, 06:44 PM
Last Post: tlopezdh
Star Type Error: 'in' object is not callable nman52 3 3,458 May-01-2021, 11:03 PM
Last Post: nman52
  finding and deleting json object GrahamL 1 4,876 Dec-10-2020, 04:11 PM
Last Post: bowlofred
  Serializable JarredAwesome 4 2,298 Nov-19-2020, 11:50 PM
Last Post: JarredAwesome
  TypeError: 'type' object is not subscriptable Stef 1 4,592 Aug-28-2020, 03:01 PM
Last Post: Gribouillis
  isinstance() always return true for object type check Yoki91 2 2,604 Jul-22-2020, 06:52 PM
Last Post: Yoki91
  AttributeError: type object 'FunctionNode' has no attribute '_TestValidateFuncLabel__ binhduonggttn 0 2,282 Feb-19-2020, 11:29 AM
Last Post: binhduonggttn
  Type hinting - return type based on parameter micseydel 2 2,530 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