This question is locked. New answers and comments are not allowed.
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:
However, Telerik MVC doesn't expose the JavaScriptSerializer object. In fact, most places where the serializer is used follow this pattern:
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
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