utahiosmac / Marshal

Marshaling the typeless wild west of [String: Any]
MIT License
697 stars 62 forks source link

How to serialize an Marshal object to json string #115

Closed 1ud0v1c closed 7 years ago

1ud0v1c commented 7 years ago

Hi,

I succeed to deserialize my object thanks to Marshal but I try to do it the other way. To convert my object back to a json string. I don't succeed to be conform to the Marshling protocol. Here is my object:

class MarshalBook : Book, Marshaling, Unmarshaling {
    required init(object: MarshaledObject) throws {
        super.init()

        self.id = try object.value(for: "id")
        self.title = try object.value(for: "name")
        self.author = try object.value(for: "author")
        self.genre = try object.value(for: "genre")
        self.numpages = try object.value(for: "numpages")
        self.releaseDate = try object.value(for: "release_date")
        self.cover = try object.value(for: "cover")
    }

    func marshaled() -> [String: Any] {
        return {
            "id": id,
            "name" : title,
            "author": author,
            "genre": genre,
            "numpages": numpages,
            "release_date": releaseDate,
            "cover": cover
        }
    }
}

And I have an error :

Cannot convert return expression of type '() -> String' to return type '[String : Any]'

Did you see, what I'm doing wrong ? Regards