I wrote a .ToJson()
extension method like so:
public static string ToJson(this object obj)
{
JavaScriptSerializer serializer = new JavaScriptSerializer();
return serializer.Serialize(obj);
}
This allows me to do something like myObject.ToJson()
to get a JSON string to send back to my application via my .asmx
web service. This all works great.
A problem has arisen where I'm building a page that requires massive amounts of asynchronous data be transferred to the page and I'm starting to reach data payloads of upwards of 100k. In looking at the JSON, I've noticed that it encodes whatever I send back, which adds a lot of extra characters. As this application is an admin tool for a very small audience, I'm not really worried a whole lot about this kind of security, but I can't seem to find it in here how to control encoding.
Here's what I'm receiving (example):
{"d":"[{\"OrderId\":1308,\"Approved\":true,
\"Status\":\"\\u003cimg class=\\\"statusicon\\\" src=\\\"/images/ ...
when I should be getting something like:
{"d": [{"OrderId":1308,"Approved":true,"Status":"<img class=\"statusicon\"
src=\"/images/ ...
Is there any way to control whether or not the JSON data gets escaped?
One thing to note is that I'm forced to use this in my .ajaxSetup()
to parse this data coming back:
dataFilter: function (data) {
if (data) {
var msg;
if (typeof (JSON) !== 'undefined' && typeof (JSON.parse) === 'function') {
msg = JSON.parse(data);
} else {
msg = eval('(' + data + ')');
}
if (msg.hasOwnProperty('d')) {
return msg.d;
} else {
return msg;
}
}
}