Is there a standardized way to store classes in JSON and then convert back to classes from a string again? For example, I may have an array of objects of type "Questions". I would like to serialize this to JSON and send it (for example) to a JavaScript page that converts the JSON string back to objects. But he should then be able to convert Questions into objects of type Question, using the constructor already created:
function Question(id, title, description){ }
Is there a standardized way to do this? I have a few ideas on how to do this, but reinvent the wheel, etc.
Edit:
To clarify what I mean by classes: several languages ββcan use classes (JAVA, PHP, C #), and they will often communicate with JavaScript via JSON. On the server side, data is stored in instances of classes, but when they are serialized, they are lost. After deserialization, you get an object structure that does not indicate what type of objects you have. JavaScript supports prototype OOP, and you can create objects from constructors that will become typeof this constructor, for example, the question above. The idea I had would be for classes to implement the JSONType interface with two functions:
public interface JSONType{ public String jsonEncode();
For example, the Question class will implement JSONType, so when I serialize my array, it will call jsonEncode for every element in this array (it detects that it implements JSONType). The result will be something like this:
[{typeof:"Question", id:0, title:"Some Question", description:"blah blah blah"}, {typeof:"Question", id:0, title:"Some Question", description:"blah blah blah"}, {typeof:"Question", id:0, title:"Some Question", description:"blah blah blah"}]
Then the javascript code will see the typeof attribute and look for the Question function, and then call the static function of the Question object, similar to the interface above (yes, I understand that there is an XSS security hole). The jsonDecode object returns an object of type Question and recursively decodes JSON values ββ(for example, there may be a comment value, which is an array of comments).