I would like to write a JavaScript converter that outputs in JSON rather than an IDictionary<string,object>.
Why? Say for instance I have a class:
class MyClass
{
public int X;
public int Y;
public int Add()
{
return X + Y;
}
public int Subtract()
{
return X - Y;
}
public int Average
{
get
{
return (X + Y) / 2;
}
}
}
and I want to serialize this class to JSON like this:
<script type='text/javascript'>
var myc =
{
"X" : 21,
"Y" : 99,
"Add" : function() {
return X + Y;
},
"Subtract" : function() {
return X - Y;
},
"get_Average" : function() {
return (X + Y) / 2;
}
}
</script>
or
<script type='text/javascript'>
var MyClass = {
"Add" : function() {
return X + Y;
},
"Subtract" : function() {
return X - Y;
},
"get_Average" : function() {
return (X + Y) / 2;
}
}
var myc =
{
"X" : 21,
"Y" : 99,
"Add" : MyClass.Add,
"Subtract" : MyClass.Subtract,
"get_Average" : MyClass.get_Average
}
</script>
or
<script type='text/javascript'>
function MyClass(X,Y)
{
this.X = X;
this.Y = Y;
this.Add = function() {
return this.X + this.Y;
},
this.Subtract = function() {
return this.X - this.Y;
},
this.get_Average = function() {
return (this.X + this.Y) / 2;
}
}
var myc = new MyClass(21,99);
</script>
You can do this in AjaxPro.Net (www.ajaxpro.info).
Apparently, I can't do this with a custom JavaScriptConverter class, since Serialize must return an IDictionary<string,object>. The best I could think of is putting the function text as a string in the dictionary, but that would be serialized as a string. Is there any way to do this?