Skip to content

Instantly share code, notes, and snippets.

@gtarsia
Last active August 14, 2017 15:35
Show Gist options
  • Save gtarsia/e312f289ba149b33c951c1f95f9de29b to your computer and use it in GitHub Desktop.
Save gtarsia/e312f289ba149b33c951c1f95f9de29b to your computer and use it in GitHub Desktop.
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
public static class StreamExtensions
{
public static IEnumerable<TResult> ReadJson<TResult>(this Stream stream)
{
var serializer = new JsonSerializer();
using (var reader = new StreamReader(stream))
using (var jsonReader = new JsonTextReader(reader))
{
jsonReader.SupportMultipleContent = true;
while (jsonReader.Read())
{
if (jsonReader.TokenType == JsonToken.StartObject)
{
yield return serializer.Deserialize<TResult>(jsonReader);
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment