I have a class whose instances are an object representing messages in a standardized format. These instances are eventually output as a JSON object so that they can be transferred between processes.
The class has some very helpful methods which I use to validate and control the message without delving into the object structure. This is fine and dandy when I'm creating the message and sending it out, but when I receive the JSON object it contains only data, and none of these helpful methods.
What is the best way to map these incoming JSON objects to a class instance?
I can think of four ways...
1) Add a static method to the class which accepts a JSON string and produces a new instance of the class.
2) Add a method to the class which accepts a JSON string and maps the JSON object to that instance.
3) Have an optional argument in the constructor which accepts a JSON string and maps it to the instance on construction.
4) Turn the class into a service with a factory method.
I would love to do 3, but the problem is that I cannot return feedback if the object wasn't correctly formatted, etc.