Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save CraftyFella/6042173 to your computer and use it in GitHub Desktop.
Save CraftyFella/6042173 to your computer and use it in GitHub Desktop.
Replace Default Deserializer in MVC with Json.net Deserializer Example
public class TestEndPointController : Controller
{
[HttpPost]
public ActionResult Record(RecordInputModel model)
{
return
Json(
new
{
success = true,
message = string.Format("enquiryId: {0}. brands {1}", model.EnquiryId, string.Join(", ", model.Brands))
});
}
}
public class RecordInputModel
{
public Guid EnquiryId { get; set; }
public IEnumerable<string> Brands { get; set; }
}
<script>
$(document).ready(function () {
postBrandsTo("http://localhost:8849/EndPoint/91DDD04F-619C-4913-B6C9-73DD1CAD1BC3");
postBrandsTo("http://localhost:8849/EndPoint2/91DDD04F-619C-4913-B6C9-73DD1CAD1BC3");
});
function postBrandsTo(url) {
$.ajax({
url: url,
data: JSON.stringify({ Brands: ["Brand1", "Brand2"] }),
type: "POST", contentType: "application/json charset=utf-8",
success: function (result) {
console.log(result.message);
}
});
}
</script>
public class JsonNetValueProviderFactory : ValueProviderFactory
{
public override IValueProvider GetValueProvider(ControllerContext controllerContext)
{
// first make sure we have a valid context
if (controllerContext == null)
throw new ArgumentNullException("controllerContext");
// now make sure we are dealing with a json request
if (
!controllerContext.HttpContext.Request.ContentType.StartsWith("application/json",
StringComparison.OrdinalIgnoreCase))
return null;
// get a generic stream reader (get reader for the http stream)
StreamReader streamReader = new StreamReader(controllerContext.HttpContext.Request.InputStream);
// convert stream reader to a JSON Text Reader
JsonTextReader JSONReader = new JsonTextReader(streamReader);
// tell JSON to read
if (!JSONReader.Read())
return null;
// make a new Json serializer
JsonSerializer JSONSerializer = new JsonSerializer();
// add the dyamic object converter to our serializer
JSONSerializer.Converters.Add(new ExpandoObjectConverter());
// use JSON.NET to deserialize object to a dynamic (expando) object
Object JSONObject;
// if we start with a "[", treat this as an array
if (JSONReader.TokenType == JsonToken.StartArray)
JSONObject = JSONSerializer.Deserialize<List<ExpandoObject>>(JSONReader);
else
JSONObject = JSONSerializer.Deserialize<ExpandoObject>(JSONReader);
// create a backing store to hold all properties for this deserialization
Dictionary<string, object> backingStore =
new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);
// add all properties to this backing store
AddToBackingStore(backingStore, String.Empty, JSONObject);
// return the object in a dictionary value provider so the MVC understands it
return new DictionaryValueProvider<object>(backingStore, CultureInfo.CurrentCulture);
}
private static void AddToBackingStore(Dictionary<string, object> backingStore, string prefix, object value)
{
IDictionary<string, object> d = value as IDictionary<string, object>;
if (d != null)
{
foreach (KeyValuePair<string, object> entry in d)
{
AddToBackingStore(backingStore, MakePropertyKey(prefix, entry.Key), entry.Value);
}
return;
}
IList l = value as IList;
if (l != null)
{
for (int i = 0; i < l.Count; i++)
{
AddToBackingStore(backingStore, MakeArrayKey(prefix, i), l[i]);
}
return;
}
// primitive
backingStore[prefix] = value;
}
private static string MakeArrayKey(string prefix, int index)
{
return prefix + "[" + index.ToString(CultureInfo.InvariantCulture) + "]";
}
private static string MakePropertyKey(string prefix, string propertyName)
{
return (String.IsNullOrEmpty(prefix)) ? propertyName : prefix + "." + propertyName;
}
}
ValueProviderFactories.Factories.Remove(
ValueProviderFactories
.Factories
.OfType<JsonValueProviderFactory>()
.FirstOrDefault()
);
ValueProviderFactories.Factories.Add(new JsonNetValueProviderFactory());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment