I have a class named Dinner, should this domain class know how to serialize itaelf as an JsonObject with Gson? By adding a method on it. Or should another class go through its properties and make the JsonObject from another class?
I want to make a custom representation so I cant use toJson method. But anyway the question still applies, which class does this belong to? What if new representations came? Xml etc. I would say that it fits in the domain class, but I amnotsure if itis correct?
I would use the Visitor pattern and model the JSON/GSON printer as a Visitor and Dinner class as Visitable. This will allow you to add as many new printers as possible without changing the specification of the Dinner class.
I have similar use case where the domain classes in a context free parser are all visitables and have different printers to print the parse entries in the parse tree in text/html format for different UIs.