Skip to content

Instantly share code, notes, and snippets.

@WiredUK
Created June 14, 2016 16:26
Show Gist options
  • Save WiredUK/2712bbe54f82e07d2061fc22ed3388de to your computer and use it in GitHub Desktop.
Save WiredUK/2712bbe54f82e07d2061fc22ed3388de to your computer and use it in GitHub Desktop.
Json Extensions class to allow deserialisation of JSON directly from a stream. Adapted from http://stackify.com/improved-performance-using-json-streaming
public static class JsonExtensions
{
public static T Deserialise<T>(this Stream jsonStream)
where T : class
{
if (jsonStream == null)
{
throw new ArgumentNullException(nameof(jsonStream));
}
if (!jsonStream.CanRead)
{
throw new ArgumentException("JSON stream must support reading", nameof(jsonStream));
}
using (var streamReader = new StreamReader(jsonStream))
using (var jsonReader = new JsonTextReader(streamReader))
{
var serialiser = new JsonSerializer();
return serialiser.Deserialize<T>(jsonReader);
}
}
}
@WiredUK
Copy link
Author

WiredUK commented Jun 14, 2016

Pretty much the same code as the original but changed for my own personal style. Oh and I'm British so z=>s!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment