Created
September 11, 2024 15:08
-
-
Save karenpayneoregon/d86e032e823b004d2325cbda0ecbead4 to your computer and use it in GitHub Desktop.
Read json asynchronous
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 interface IBaseEntity | |
{ | |
int Id { get; set; } | |
} |
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 System.Net.Http.Json; | |
using System.Text.Json; | |
namespace DeserializeAsyncApp.Classes; | |
internal class JsonHelpers | |
{ | |
public static async Task<T> FromStreamAsync<T>(string filePath) | |
{ | |
await using FileStream stream = File.OpenRead(filePath); | |
return await JsonSerializer.DeserializeAsync<T>(stream); | |
} | |
public static async Task<List<T>> ReadJson1Async<T>(string filePath) | |
=> await FromStreamAsync<List<T>>(filePath); | |
public static async Task<List<T>> ReadJsonWithTimeOutAsync<T>(string url, double timeOut = 2) | |
{ | |
using HttpClient httpClient = new() { Timeout = TimeSpan.FromSeconds(timeOut) }; | |
return JsonSerializer.Deserialize<List<T>>(await httpClient.GetStringAsync(url)); | |
} | |
public static async Task<List<T>> ReadJsonAsync<T>(string url, CancellationToken cancellationToken = default) | |
{ | |
using HttpClient client = new(); | |
return await client.GetFromJsonAsync<List<T>>(new Uri(url), Options, cancellationToken); | |
} | |
public static async Task<T> ReadJson_Async<T>(string url, CancellationToken cancellationToken = default) where T : IBaseEntity | |
{ | |
using HttpClient client = new(); | |
var customers = await client.GetFromJsonAsync<List<T>>(new Uri(url), Options, cancellationToken); | |
return customers.FirstOrDefault(x => x.Id == 1); | |
} | |
private static readonly JsonSerializerOptions Options = new(JsonSerializerDefaults.Web); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment