JSON with classes?

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();//Returns serialized JSON string public static JSONType jsonDecode(String json); } 

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).

+4
source share
4 answers

You will need to create your own serializer, which detects Question (and other classes) and serializes them as constructor calls instead of JSON object notation. Note that you will need a way to map the object to the constructor call. (What properties come from which parameters?)

Everything else: by class, it means functions used as constructors; I guess he is trying to keep a prototype.

+6
source

Have you looked at JSON.NET ? It can serialize / de-serialize .NET objects in JS and vice versa. This is a mature project and I highly recommend it.

In addition, if you can change the definition of your Question function to take a single object with properties instead of separate arguments, you can do something like this:

Working demo

  function Question(args) { this.id = args.id; this.text = args.text; } Question.prototype.alertText = function() { alert(this.text); }; $(document).ready( function() { var objectList = { 'list' : [ { 'id' : 1, 'text' : 'text one' } ,{ 'id' : 2, 'text' : 'text two' } ,{ 'id' : 3, 'text' : 'text three'} ], 'type' : 'Question' }; var functionName = objectList['type']; var constructor = window[functionName]; if(typeof constructor !== 'function') { alert('Could not find a function named ' + functionName); return; } var list = objectList['list']; $.each(list, function() { var question = new constructor(this); question.alertText(); } ); } ); 

On the side of the note, please attach your .NET interfaces to "I", for example. it should be IJsonType, not JsonType.

typeof is a keyword, so quote the property name if you want to use it.

+1
source

This is very dangerous, but eval can process any text and execute it. http://www.w3schools.com/jsref/jsref_eval.asp

But DO NOT use it. It can do something like passing over the parameters that you want to pass to the constructor as a JSON object, and call the constructor with it ...

 function Question({"id" : "val", "title", "mytitle", "description" : "my desc"}){ ... } 
0
source

This may be helpful. http://nanodeath.github.com/HydrateJS/ https://github.com/nanodeath/HydrateJS

Use hydrate.stringify to serialize the object and hydrate.parse to deserialize.

0
source

All Articles