1

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;
        }
    }
}
Jason
  • 51,583
  • 38
  • 133
  • 185
  • What you're trying to get is not valid JSON right? Are you sure that's what you want? – Jeff Mercado Jun 02 '12 at 01:40
  • no i do want valid JSON. I just don't want all the values escaped. Of course I want the internal quotes escaped (they have to be) but I don't want to escape my escapes, which is what it seems like it's doing – Jason Jun 02 '12 at 01:40
  • Ah so instead of `d` being assigned a string, you want that array? – Jeff Mercado Jun 02 '12 at 01:43
  • Yeah, I guess that would do it, too. – Jason Jun 02 '12 at 01:44
  • 1
    What does your object that you are trying to serialize look like? Looks like for some reason, the serializer thinks the `d` property is a string. – Jeff Mercado Jun 02 '12 at 01:53
  • Check out [newtonsoft](http://json.codeplex.com/). It may be overkill for your application, but it's a solid JSON library for C#. – allonhadaya Jun 02 '12 at 02:35

1 Answers1

0

I had this problem. The json library is not returning escaped text. I believe javascript itself is interpreting it as escaped.

<script type="text/javascript">
     $(document).ready(function () {
          var columnNumber = 1;
          var databaseTypes = <%= Html.Raw(ViewBag.DatabaseTypes) %>;

Take a look at the Viewbag area. This is how to inject text within a asp.net mvc view.

Luke101
  • 63,072
  • 85
  • 231
  • 359