I want to deserialize to my json string to my generic class. But i want to do this in one method. When i call this link;
Here is my Code; Its My class:
public class Test
{
public string userId { get; set; }
public string Id { get; set; }
}
Its My Deserialize class:
private static async Task<IRestResponse<T>> MapResponse<T>(HttpResponseMessage response) where T : class
{
var restResponse = new WebApiRestResponse<T>
{
Content = await response.Content.ReadAsStringAsync(),
StatusCode = response.StatusCode
};
try
{
var body = await response.Content.ReadAsStringAsync();
if (response.Content.Headers.ContentType.MediaType == "text/plain")
restResponse.Exception = new RestInteractionException(body);
else
restResponse.Data = JsonConvert.DeserializeObject<List<T>>(body); ;
}
catch (Exception e)
{
restResponse.Exception = e;
}
return restResponse;
}
public interface IRestResponse
{
string Content { get; }
Exception Exception { get; set; }
HttpStatusCode StatusCode { get; }
}
public interface IRestResponse<T> : IRestResponse
{
List<T> Data { get; }
}
https://jsonplaceholder.typicode.com/posts/1
Its returned to single object.
when i call this link;
https://jsonplaceholder.typicode.com/posts
MapResponse class make to deserialize to json to generic class. But sometimes i call single object, sometimes i call list object. so how can i do this in this code?
Best regards