Created
July 29, 2020 08:23
-
-
Save HeshamMeneisi/67ddb14fb7eee438d7bce38c103fdbf4 to your computer and use it in GitHub Desktop.
Async JSON iteration in C#
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
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