0

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())
4
  • btw the string returned from saveThemes() is technically not json. Commented Nov 5, 2020 at 14:07
  • @quamrana Because they are not encapsulated within one JSON 'string'? Commented Nov 5, 2020 at 14:16
  • If you look on the website the only allowed top level objects are list [] or object {}. You have to ask yourself what would you expect a json decode of the result of saveThemes() to produce? Commented Nov 5, 2020 at 14:28
  • @quamrana excellent, thank you fully understood. Commented Nov 5, 2020 at 14:41

1 Answer 1

1

You can convert a list of the __dict__ member as json:

    def saveJson(self):
        return json.dumps([t.__dict__ for t in self.__themes])
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.