WebMethod automatically returns class object as JSON

Can someone explain to me how ASP.NET handles the conversion from a class object to a JSON object in WebMethods?

For example, you have the following WebMethod that returns an object Person:

    [WebMethod]
    public static Person GetPerson()
    {
        Person p = new Person() 
        {
            Id = 1,
            Name = "Test"
        };

        return p;
    }

In my jQuery, where I call WebMethod, I get a response that contains from a json object.

How did ASP.NET do this automatically? Does he use a class JavaScriptSerializer?

You also see many examples of using JSON converters to convert a class object to a json object. Why is this? Is it because of the class JavaScriptSerializerthat it uses and its poor performance or ...?

+4
source share
1

ASP.NET ?

, , - WebMethod, , , , WebMethod , .

JavaScriptSerializer?

. - , . . , .

JSON json. ? - JavaScriptSerializer ...?

WebMethod JSON, accept. :

[WebMethod]
public static void GetPerson()
{
    Person p = new Person() 
    {
        Id = 1,
        Name = "Test"
    };
    HttpContext.Current.Response.ResponseType = "application/json";
    HttpContext.Current.Response.Write(JsonConvert.SerializeObject(p));
    HttpContext.Current.Response.End();
}

( , ), .

+3