tags:

views:

258

answers:

2

As the question goes , how do i go about serializing a subsonic class to JSON. While i can manually create the JSON object i need some way to do it automatically.

Any recommendations would be awesome as i don't really want to do this manually. Could you also please recommend a could Library to help with the JSON serialization.

BTW i am doing all of this in ASP.Net 2.0

A: 

I would check out either Json.NET:

The Json.NET library makes working with JavaScript and JSON formatted data in .NET simple. Quickly read and write JSON using the JsonReader and JsonWriter or serialize your .NET objects with a single method call using the JsonSerializer.

or DataContractJsonSerializer:

Use the DataContractJsonSerializer class to serialize instances of a type into a JSON document and to deserialize a JSON document into an instance of a type. For example, you can create a type named Person with properties that contain essential data, such as a name and address. You can then create and manipulate an instance of the Person class and write all of its property values in a JSON document for later retrieval. This JSON document can later be deserialized into the Person class or another class with an equivalent data contract.

Andrew Hare
I have looked at Json.Net but its still a very manual process where i have to manually create my Json objects. I will have a loot at the DataContractJsonSerializer and see if it works but it seems what its what i am looking for.
RC1140
+2  A: 

Have you looked at the JavaScriptSerializer class? It will create a JSON version of a .NET type, including any public properties and a __type property so that the object can be reconstructed/deserialized when necessary.

Also not that WebMethods return JSON, which makes it incredibly convenient to send objects back and forth from AJAX to the server.

EDIT: added some example code in response to OP's comments.

http://encosia.com/2008/05/29/using-jquery-to-directly-call-aspnet-ajax-page-methods/

public partial class _Default : Page 
{
  [WebMethod]
  public static string GetDate()
  {
    return DateTime.Now.ToString();
  }
}

The method GetDate() above is a webmethod. Note that it has the [WebMethod] attribute applied, and that it is static. Note that the page in which a webMethod resides will call it's page_load() method each time you call one of its webMethods. Code your pages accordingly.

To call a webMethod from JavaScript, you can use jQuery:

$.ajax({
      type: "POST",
      url: "Default.aspx/GetDate",
      data: "{}",
      contentType: "application/json; charset=utf-8",
      dataType: "json",
      success: function(result) {
        // Replace the div's content with the page method's return.

        alert('Received from webmethod: '+result.d);
      }
    });

The example webMethod here returns a string, but just about any object type can be returned instead. There are many good references in SO, as well as around the .NET, on using webMethods.

If you don't need the power of a webMethod and, instead, wish only to serialize an object into JSON, just use the JSON serializer:

public string MyClassToJson(MyClass mc)
{
    JavaScriptSerializer serializer = new JavaScriptSerializer();

    string serializedObject = serializer.Serialize(mc);

    return serializedObject;
}
David Lively
What WebMethods are you referring to as that may help me as well
RC1140
A WebMethod is a static method declared inside of an ASP.NET page that is intended to be called from client-side script. It's similar to a method in a web service (.asmx), but is declared inside of a regular .aspx page, instead. I've entered some additional references in my reply above.
David Lively
Thanks dude awesome info , i found it alot easier to do using JSON.Net , all it takes is the following string output = JsonConvert.SerializeObject(myClass);I havent tested your way of doing it but will mark it as complete any way
RC1140