I’m trying to serialize my business objects into JSON for consumption by a Javascript application. The problem is is that I’m trying to keep my business objects ‘pure’ in the sense they are not aware of data access or persistence. It seems to me that ‘diluting’ my objects with a toJSON() function would go against this goal. On the other hand, using an external object to serialize my business objects would not work since I keep all my instance variables private.
Am I approaching this totally the wrong way?
If the instance variables are private, they should not appear in a serialization that is being sent to a JavaScript application. By definition, if you’re serializing them and sending them to a separate application, they are public. So, an external object should have some way of accessing them, probably through some sort of getter methods.