Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save HeshamMeneisi/67ddb14fb7eee438d7bce38c103fdbf4 to your computer and use it in GitHub Desktop.
Save HeshamMeneisi/67ddb14fb7eee438d7bce38c103fdbf4 to your computer and use it in GitHub Desktop.
Async JSON iteration in C#
using (WebClient client = new WebClient())
{
client.Headers = new WebHeaderCollection{{"x-access-token", "..."}};
await using (Stream stream = client.OpenRead("http://...dump.json"))
using (StreamReader streamReader = new StreamReader(stream ?? throw new NullJsonDumpException()))
using (JsonTextReader reader = new JsonTextReader(streamReader))
{
reader.SupportMultipleContent = true;
var serializer = new JsonSerializer();
while (reader.Read())
{
if (reader.TokenType == JsonToken.StartObject)
{
yield return serializer.Deserialize<JObject>(reader);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment