Skip to content

Instantly share code, notes, and snippets.

@dax70
Created June 16, 2012 21:03
Show Gist options
  • Save dax70/2942507 to your computer and use it in GitHub Desktop.
Save dax70/2942507 to your computer and use it in GitHub Desktop.
JsonNetResult Logic
// Data (like the default JsonResult) is the Model being serialized.
Type type = this.Data.GetType();
// Wrap Enumerables to avoid potential Json Hijacking.
if (TypeHelper.TryWrappingForIEnumerableGenericOrSame(ref type))
{
this.Data = TypeHelper.GetTypeRemappingConstructor(type).Invoke(new object[] { this.Data });
}
using (JsonTextWriter jsonTextWriter = new JsonTextWriter(response.Output) { CloseOutput = false })
{
var serializer = JsonSerializer.Create(this.SerializerSettings);
serializer.Serialize(jsonTextWriter, this.Data);
jsonTextWriter.Flush();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment