JSON serialisation
Posted: Sat Jul 02, 2022 2:35 pm
Good day
I have a basic Python class ToDoItem:
class ToDoItem:
def __init__(self, name, description, isComplete):
self.Name = name
self.Description = description
self.IsComplete = isComplete
If I instantiate an object of this type:
item = ToDoItem('cleanRoom', 'Clean my room', False)
'serialise' it to JSON:
itemJson = ujson.dumps(item)
and 'deserialise' it again:
item = ujson.loads(itemJson)
I get the following:
ValueError: syntax error in JSON
Is this the correct way to serialise an object to JSON?
I have a basic Python class ToDoItem:
class ToDoItem:
def __init__(self, name, description, isComplete):
self.Name = name
self.Description = description
self.IsComplete = isComplete
If I instantiate an object of this type:
item = ToDoItem('cleanRoom', 'Clean my room', False)
'serialise' it to JSON:
itemJson = ujson.dumps(item)
and 'deserialise' it again:
item = ujson.loads(itemJson)
I get the following:
ValueError: syntax error in JSON
Is this the correct way to serialise an object to JSON?