Created
June 14, 2016 16:26
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Pretty much the same code as the original but changed for my own personal style. Oh and I'm British so z=>s!