Quote Prevention without Rebuilding JSON.Net

Sep 8, 2009 at 6:37 PM

First, JSON.Net is really terrific. Thanks so much for making such a useful library available.

I am having one issue using the library that I assume is incredibly common. I am generating JSON on a server side page that gets used on the client side. I need to specify certain fields within a single JSON object that need to be deserialized as un-quoted strings while also specifying fields that need to be deserialized as quoted strings.

For example, the callback parameter in the following JSON needs to be un-quoted but all other strings need to be quoted:


I am currently doing this in the very hacky method described here

objJSONTabControls.TabControls[i].autoLoad.callback = "DELQUOTEfunction(){console.info('Working');}DELQUOTE";

objJsonSerializer.Serialize(objJsonTextWriter, objJSONTabControls);

strFinalJSONString = objStringWriter.ToString();

strFinalJSONString = strFinalJSONString.Replace("\"DELQUOTE","");
strFinalJSONString = strFinalJSONString.Replace("DELQUOTE\"", "");


Any suggestions for how I might use existing functionality to prevent this horribly bad hack form living in my code?


Sep 8, 2009 at 10:20 PM

You could change the type of callback property from string to JsonRaw, with the content of the JsonRaw as the function text. The serializer will then write the function text without quotes.

Sep 9, 2009 at 11:37 AM

Worked like a charm. Thanks very much for your support.

May 6, 2011 at 2:15 PM


I'm trying to do exactly this, can you provide a bit more info please?


thanks in advance


May 6, 2011 at 2:41 PM

actually, don't worry.  i've found it.

thanks anyway.

The example below will serialise the function without quotes.

Hope that helps someone.


using JavascriptFunction = Newtonsoft.Json.Linq.JRaw;


dynamic obj = new ExpandoObject();

obj.listeners = new { change = new JavascriptFunction(@"function(field, newValue, oldValue){
                       field.setValue(newValue.toUpperCase());  }")