I have a the following code. In essence, there are two classes - Lego Themes which acts as a container for Lego Theme objects. I have implemented a method "saveThemes()" that basically iterates through the themes - converts the individually stored Lego Objects into JSON and then adds them to a variable (resultantJson).
However, I appreciate this is a bit of a 'hack'. Should I be able to do a json.dumps() of the self__.themes directly (it currently says the object is serializable)?
import json
class LegoThemes:
def __init__(self):
self.__themes = []
def saveThemes(self):
resultantJson = ""
for theme in self.__themes:
resultantJson += json.dumps(theme.__dict__)
# TODO: Output resultantJSON to .JSON file.
return resultantJson
# TODO: implement loadThemes(self)
def addTheme(self, theme):
self.__themes.append(theme)
class LegoTheme:
def __init__(self, title, description, thumbnailImage, logoImage, url):
self.__title = title
self.__description = description
self.__thumbnailImage = thumbnailImage
self.__logoImage = logoImage
self.__url = url
def getTitle(self):
return self.__title
testThemeOne = LegoTheme(
"Test Theme One Title",
"Test Theme One Description.",
"Test Theme One Thumbnail Image",
"Test Theme One Logo Image",
"Test Theme One URL",
)
testThemeTwo = LegoTheme(
"Test Theme Two Title",
"Test Theme Two Description.",
"Test Theme Two Thumbnail Image",
"Test Theme Two Logo Image",
"Test Theme Two URL",
)
testThemeThree = LegoTheme(
"Test Theme Three Title",
"Test Theme Three Description.",
"Test Theme Three Thumbnail Image",
"Test Theme Three Logo Image",
"Test Theme Three URL",
)
legoThemes = LegoThemes()
legoThemes.addTheme(testThemeOne)
legoThemes.addTheme(testThemeTwo)
legoThemes.addTheme(testThemeThree)
print(legoThemes.saveThemes())
saveThemes()is technically not json.[]or object{}. You have to ask yourself what would you expect ajsondecode of the result ofsaveThemes()to produce?