Custom JSON converter

Last post 01-02-2007 4:56 PM by alancnet. 4 replies.

Sort Posts:

  • Custom JSON converter

    12-29-2006, 6:17 PM
    • Loading...
    • alancnet
    • Joined on 05-30-2006, 12:21 PM
    • Posts 5

    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? 

    Filed under: , ,
  • Re: Custom JSON converter

    12-30-2006, 2:59 PM
    • Loading...
    • Rama Krishna
    • Joined on 01-24-2006, 9:33 PM
    • Atlanta, GA
    • Posts 307
    The example you gave is not actual JSON. Look at the JSON spec: http://www.json.org/ The only values permitted are string, number, object({}), array([]), true,false and null. So you cannot have function. ASP.NET Ajax follows the JSON spec as a result functions are not supported.
  • Re: Custom JSON converter

    01-02-2007, 1:23 PM
    • Loading...
    • alancnet
    • Joined on 05-30-2006, 12:21 PM
    • Posts 5

    I see nothing in the documentation forbidding the use of the "new" keyword to symbolize an object, so  new myobject(a,b,c) should be ok.

     Anyways, I am not concerned with following RFC 4627 guidelines in my application, especially at the cost of extra manhours. Can it be done?

  • Re: Custom JSON converter

    01-02-2007, 2:21 PM
    Answer

    The JSON spec does forbid what you are attempting. A json string must be: object (starting with "{"), an array (starting with "["), a string (quoted with double quote), a number, true, false, or null. "new Object()" simply doesn't meet any of those.

    Converters return a dictionary of string/object because that is the best representation of a json object. That means you are pretty much restricted to "correct" json with converters.

    It's not my place to question your design, but do consider that there are other options. Your converter could return plain data, and on the client side you can convert it into the right client side type. The futures CTP does this for DataTables, for example. The JSON for a datatable is plain data. Then on the client side there's a method that knows how to convert the object into an actual DataTable class instance.

    If you still want to do as you describe, you can do it by serializing your json as a string. On the client side, you'll get a string, which you can then manually deserialize. So you're still living within the JSON spec, because your custom json is serialized within the string of another json string.

    { myCode: "new Object(....)" }

    To deserialize it,

    var obj = Sys.Serialization.JavaScriptSerializer.deserialize(jsonObj.myCode);

    End the confusion.
    Infinities Loop: TRULY Understanding ViewState
    .NET from a new perspective.

    This posting is provided "AS IS".
  • Re: Custom JSON converter

    01-02-2007, 4:56 PM
    • Loading...
    • alancnet
    • Joined on 05-30-2006, 12:21 PM
    • Posts 5

    I suppose if I have to live with it, I will. I will, however, miss the transparency.

     Thanks for the response Big Smile

Page 1 of 1 (5 items)
Microsoft Communities
Page view counter