// the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
// Boston, MA 02110-1301, USA.
+#nullable enable annotations
+
using System;
using System.Collections.Generic;
+using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Net.Http;
using System.Runtime.Serialization;
/// </summary>
public sealed class LazyJson<T> : IDisposable
{
- public HttpResponseMessage Response { get; }
+ public HttpResponseMessage? Response { get; }
private T instance;
private bool completed = false;
public LazyJson(HttpResponseMessage response)
- {
- this.Response = response;
- }
+ => this.Response = response;
internal LazyJson(T instance)
{
public async Task<T> LoadJsonAsync()
{
if (this.completed)
- return this.instance;
+ return this.instance!;
- using (var content = this.Response.Content)
- {
- var responseText = await content.ReadAsStringAsync()
- .ConfigureAwait(false);
+ using var content = this.Response.Content;
+ var responseText = await content.ReadAsStringAsync()
+ .ConfigureAwait(false);
- try
- {
- this.instance = MyCommon.CreateDataFromJson<T>(responseText);
- this.completed = true;
+ try
+ {
+ this.instance = MyCommon.CreateDataFromJson<T>(responseText);
+ this.completed = true;
- return this.instance;
- }
- catch (SerializationException ex)
- {
- throw TwitterApiException.CreateFromException(ex, responseText);
- }
+ return this.instance;
+ }
+ catch (SerializationException ex)
+ {
+ throw TwitterApiException.CreateFromException(ex, responseText);
}
}
public void Dispose()
- {
- this.Response?.Dispose();
- }
+ => this.Response?.Dispose();
}
public static class LazyJson
{
public static async Task IgnoreResponse<T>(this Task<LazyJson<T>> task)
{
- using (var lazyJson = await task.ConfigureAwait(false))
- {
- // レスポンスボディを読み込まず破棄する
- }
+ using var lazyJson = await task.ConfigureAwait(false);
+ // レスポンスボディを読み込まず破棄する
}
}
}