1

From a web service method I am returning an object of type 'GridBindingDataSet'. But its not getting serialized as JSON automatically.

Is there some way of making sure that the object gets serialized as JSON? I am using AJAX enabled web service that can be called from client-side using jQuery.

public class GridBindingDataSet
{
  public int TotalCount { get; set; }
  public DataTable Data { get; set; }
}

EDIT 1: I am getting the following error when the web service method is called from jQuery:

A circular reference was detected while serializing an object of type 'System.Reflection.RuntimeModule'

EDIT 2: I used JSON.net to serialize the above object of GridBindingDataSet. The web service is now returning a string rather than a GridBindingObject. Code for this is as below. But the browser cannot understand d.TotalCount and d.Data even though they are there in JSON returned.

[WebMethod]
public string GetJSONDataSetForGrid()
{
     ...
     ...
     DataTable dt  = GetDataForPage0();
     int total = GetTotalCount();
     GridBindingDataSet gridBindingData = new  GridBindingDataSet ( total, dt);

     //return a JSON serialized string
     return JsonConvert.SerializeObject(gridBindingData, 
     Formatting.None, new JsonSerializerSettings
      {
        PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.None 
    });
}

But the JSON being returned is full of back slashes which the browser is not interpreting since the grid using the JSON string is showing up as empty. d.Data and d.TotalCount are not being parsed from the JSON string. JSON retuned is as below:

{"d":"{\"TotalCount\":81,\"Data\":[{\"ProductName\":\"Alice Mutton\",\"UnitPrice\":39.00,   
\"UnitsInStock\":0,\"Discontinued\":true},{\"ProductName\":\"Aniseed Syrup\",\"UnitPrice\":10.00,
\"UnitsInStock\":13,\"Discontinued\":false}]}"}
Sunil
  • 20,653
  • 28
  • 112
  • 197
  • JSON.net seems ok with it http://stackoverflow.com/questions/2979922/how-to-convert-datatable-to-json-string-using-json-net .. not sure how you would wire it up in your 'web service method' case. – mtmk Feb 09 '14 at 21:37
  • `But its not getting serialized as JSON automatically` isn't much descriptive. Do you get an error? What happens you don't expect? – L.B Feb 09 '14 at 21:39
  • @L.B - I added the error I am seeing under EDIT 1 of my post. – Sunil Feb 09 '14 at 21:41
  • @Maxwell Troy - Thanks for the comment. It is helpful but now I need to find how to use datatable serialization while serializing GridBindingDataSet object. – Sunil Feb 09 '14 at 21:53
  • If you are OK with double serialization, you can use Json.Net and return a `string` from your method. In jQuery, You can use `JSON.parse` to convert your string to a real object. – L.B Feb 09 '14 at 21:56
  • @Sunil, You might be able to make an exception for your type http://msdn.microsoft.com/en-us/library/bb763183%28v=vs.100%29.aspx .. or return 'string' from your method, sending the serialised JSON yourself – mtmk Feb 09 '14 at 22:06
  • @Maxwell Troy - I did try returning JSON as described in EDIT 2 of my post but it is not understood by the client. Did you mean adding JSON.net as the json convertor in convertor settings when you said make an exception? – Sunil Feb 09 '14 at 22:55
  • @Sunil, read my last comment again :) – L.B Feb 09 '14 at 22:56

1 Answers1

1

Also its worth having a look at Json.Net, many would say one of the best Json serializers available, I use it for all of my projects.

In response to the circular referece have a look at preserving references in the documentation, from their examples:

Directory root = new Directory { Name = "Root" };
Directory documents = new Directory { Name = "My Documents", Parent = root };

File file = new File { Name = "ImportantLegalDocument.docx", Parent = documents };

documents.Files = new List<File> { file };

string preserveReferenacesObjects = JsonConvert.SerializeObject(documents, Formatting.Indented, new JsonSerializerSettings
{
    PreserveReferencesHandling = PreserveReferencesHandling.Objects
});

// {
//   "$id": "1",
//   "Name": "My Documents",
//   "Parent": {
//     "$id": "2",
//     "Name": "Root",
//     "Parent": null,
//     "Files": null
//   },
//   "Files": [
//     {
//       "$id": "3",
//       "Name": "ImportantLegalDocument.docx",
//       "Parent": {
//         "$ref": "1"
//       }
//     }
//   ]
// }
shenku
  • 11,969
  • 12
  • 64
  • 118
  • What about *circular references*? Although Json.Net can handle it very well, not as you have posted. – L.B Feb 09 '14 at 22:09
  • ive updated my example as per your comment, thanks for the feedback. – shenku Feb 09 '14 at 22:16
  • Now the missing part is: How will OP use this in his/her code? (Both in aspx & jQuery) – L.B Feb 09 '14 at 22:20