-1

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

  • You need to write the code that implements the logic you are describing. Alternatively, you could provide sample code that you have tried and people may give you a hand here. – Vidmantas Blazevicius Mar 19 '18 at 08:34
  • It is impossible to understand your problem without posting your code. On SO, people take their free time to help you out of a problem. It's not asking too much that you take the time to adequately describe your problem. – Sefe Mar 19 '18 at 08:34
  • I share code. i hope you understand – user1436795 Mar 19 '18 at 08:43
  • Possibly a duplicate of [How to handle both a single item and an array for the same property using JSON.net](https://stackoverflow.com/q/18994685/3744182) – dbc Mar 19 '18 at 08:43
  • I did it. But its return stack overflow error. When you try single object it says to you System.Collections.Generic.List`1[Item]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly. https://dotnetfiddle.net/SgM0LY – user1436795 Mar 19 '18 at 08:45
  • @user1436795 - you would need to use `SingleOrArrayConverter` where `Item` is the root object that might or might not be in an array. See https://dotnetfiddle.net/GJMaYi. Or `SingleOrArrayConverter` for your `Test` class. – dbc Mar 19 '18 at 08:50
  • Thanks Alot of. Its working so good.Have a nice day. You should write answer here and i will check it :) – user1436795 Mar 19 '18 at 08:57

1 Answers1

2

Since you can have list as result sometimes, return it as a list always if you want single method. When you have a single object, just create new list and add it.

Roman
  • 259
  • 1
  • 3
  • 8
  • I share my code. But i cant understand when its single, when its list. How can i understand it? can you tell me. I can return list in every condition. – user1436795 Mar 19 '18 at 08:43