This is a migrated thread and some comments may be shown as answers.
Grid Custom JavaScriptSerializer
4 Answers 73 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.
Dustin
Top achievements
Rank 1
Dustin asked on 29 Jun 2011, 08:46 PM
On a recent project we had the requirement to have a custom Javascript Serializer to handle the conversion of business objects to JSON-encoded data. Specifically, on our View Model objects we have properties that have circular references. These properties aren't a problem when rendering data on the views or posting data back to the controller, but they cannot be serialized. 

Normally this is accomplished as follows:

var serializer = new JavaScriptSerializer()
var customConverter = new CustomJavascriptConverter();
serializer.RegisterConverters(new List<JavaScriptConverter>() {customConverter});


However, Telerik MVC doesn't expose the JavaScriptSerializer object. In fact, most places where the serializer is used follow this pattern:
var serializer = new JavaScriptSerializer()
return serializer.Serialize(obj)


Our solution is to create a JavaScriptSerializerFactory that holds a JavaScript Serializer object instance, and replace any instances of new JavaScriptSerializer() with JavaScriptSerializerFactory.Instance.

Additionally, JavaScriptSerializerFactory.Instance returns a serialization Interface to allow us to easily swap out the serializer with a third party json serializer by writing a wrapper class that implements the Interface.

It would be great if we could see similar functionality in future versions of Telerik MVC.

Please see the attached files for a patch (diff) of our changes. Use Git (or other patch tool) To apply this patch to the Telerik MVC 2011.Q1 release 

4 Answers, 1 is accepted

Sort by
0
Dustin
Top achievements
Rank 1
answered on 29 Jun 2011, 08:51 PM
Fixed Patch file
0
Stephen
Top achievements
Rank 1
answered on 03 Nov 2011, 08:04 PM
Any news on this?

Something like this needed desperately. The MVC JavaScriptSerializer is horrible all around, and unusable if you are using EF code first; you can't use the JavaScriptSerializer with any entities that have a navigation property because the EF generated proxy will always contain a cicular reference (even if the entities they wrap do not). 

We need a way to tell Telerik componts to use a different serializer, but I don't want to have to hand-hack in a fix each time you guys release a new version.
0
Patrick
Top achievements
Rank 1
answered on 11 Nov 2011, 06:09 AM
Indeed, is there some reason the ability to inject a new serializer can't make it into the Q3 release?  This is a fairly small change, but has major ramifications for those who are running into serialization problems.
0
loi
Top achievements
Rank 1
answered on 18 Nov 2011, 05:58 AM
Not sure if this will help, but we use the Json.net library at http://json.codeplex.com/ to serialize complex objects such as DataSet, DataTable, Dictionary, List.. It's working great!!

Here is our JsonNetResult class
using System.Web.Mvc;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
 
    public class JsonNetResult : JsonResult
    {
        public override void ExecuteResult(ControllerContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException("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)
            {               
                response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(Data));
            }
        }
    }

Using it from controller
public ActionResult SomeGetJsonDataMethod ()
{
    SomeClass obj = new SomeClass();
    return new JsonNetResult{ Data: obj};
}
Tags
General Discussions
Asked by
Dustin
Top achievements
Rank 1
Answers by
Dustin
Top achievements
Rank 1
Stephen
Top achievements
Rank 1
Patrick
Top achievements
Rank 1
loi
Top achievements
Rank 1
Share this question
or