This is a migrated thread and some comments may be shown as answers.
Abstract JSON (De)serialization
4 Answers 63 Views
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Kevin Watkins
Top achievements
Rank 1
Kevin Watkins asked on 29 Jun 2010, 10:42 AM
Hi,

Any chance you could abstract the JSON (de)serialization a bit in the code? The JavaScriptSerializer used isn't great; I need the extra flexibility offered by JSON.NET. (http://json.codeplex.com/) Be nice if I could just plugin my own JSON service and use that instead of having to patch the code each new release.

Thanks,

Kev

4 Answers, 1 is accepted

Sort by
0
Atanas Korchev
Telerik team
answered on 29 Jun 2010, 11:36 AM
Hello Kevin Watkins,

The GridAction attribute is actually not using the JavaScriptSerializer class at all. It is using the built-in JsonResult result class which upon execution is using JavaScriptSerializer:
filterContext.Result = new JsonResult
                {
                    Data = new
                    {
                        data = dataProcessor.ProcessedDataSource,
                        total = dataProcessor.Total
                    }
                };

I am not sure if it is possible to make the JsonResult use any other serializer than the built-in.

Regards,
Atanas Korchev
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Kevin Watkins
Top achievements
Rank 1
answered on 29 Jun 2010, 01:30 PM
Hi,

It's easy to use a different serializer, simply create a class that extends JsonResult and overrides ExecuteResults, e.g.:

/// <summary>  
/// <see cref="JsonResult" /> that uses the Json.NET library's <see cref="JsonSerializer" /> to serialize to JSON.  
/// </summary>  
public sealed class JsonNetResult : JsonResult  
{  
    public override void ExecuteResult(ControllerContext context)  
    {  
        ValidateArgument.IsNotNull(context, "context");  
        HttpResponseBase response = context.HttpContext.Response;  
        if (!string.IsNullOrEmpty(ContentType))  
        {  
            response.ContentType = ContentType;  
        }  
        else 
        {  
            response.ContentType = "application/json";  
        }  
        if (ContentEncoding != null)  
        {  
            response.ContentEncoding = ContentEncoding;  
        }  
        if (Data != null)  
        {  
            using (var writer = new StreamWriter(response.OutputStream))  
            {  
                JsonSerializer serializer = JsonSerializer.Create();  
                serializer.Serialize(writer, Data);  
            }  
        }  
    }  

You also explicitly use the JavaScriptSerializer elsewhere, e.g. ClientSideObjectWriter.

You could define an interface that had a few methods to serialize and deserialize, then a static property somewhere with a default implementation (like ModelBinders.Default for example in MVC) that used JavaScriptSerializer. If I wanted to swap out the serializer then I could write my own implementation of your interface and simply set the property with my implementation to use it throughout your code.

Cheers,

Kev
0
Atanas Korchev
Telerik team
answered on 29 Jun 2010, 03:08 PM
Hello Kevin Watkins,

Thank you for your clarification. I will log it as a feature request and based on other customer's feedback we will prioritize it. I have updated your telerik points as a token of gratitude!

Regards,
Atanas Korchev
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Patrick
Top achievements
Rank 1
answered on 12 Nov 2011, 07:26 AM
This is also discussed in this thread:

http://www.telerik.com/community/forums/aspnet-mvc/general/grid-custom-javascriptserializer.aspx

The ability to change the serializer provides for very significant developer time savings when dealing with anything but the most simple of object graphs.
Tags
Grid
Asked by
Kevin Watkins
Top achievements
Rank 1
Answers by
Atanas Korchev
Telerik team
Kevin Watkins
Top achievements
Rank 1
Patrick
Top achievements
Rank 1
Share this question
or