0

I have a function with its own object type:

public RaceJson GetLatestRace()
{
     string filter = "example";
     List<RaceJson> currentRace = await gsaClient.SendCustomRequest<List<RaceJson>>("races?$filter=" + filter);
     return currentRace.FirstOrDefault();
}

I want to use a generic function which they will all use. I want it to be generic and deserialize the object to it's type that I'm sending. I currently have:

    public async Task<T> SendCustomRequest<T>(string odataFilter)
    {
        string response = await SendRequestAsync(odataFilter, true);

        if( !string.IsNullOrEmpty(response))
        {
            T converted = JsonConvert.DeserializeObject<T>(response);
            return converted;
        }

        return default;
    }

I get the error when trying to deserialize a list:

JsonSerializationException: Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[tf_gsa_client.Models.HorseRacing.RaceJson]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly. To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.

Thanks.

Zack Antony Bucci
  • 571
  • 1
  • 12
  • 29
  • 1
    When you say that it is wrong, what do you mean? – David Pilkington Oct 21 '19 at 09:44
  • 1
    @DavidPilkington question updated – Zack Antony Bucci Oct 21 '19 at 09:45
  • Missing the type when you call `SendCustomRequest` – Cid Oct 21 '19 at 09:46
  • 1
    Possible duplicate of [The type arguments for method cannot be inferred from the usage](https://stackoverflow.com/questions/3917249/the-type-arguments-for-method-cannot-be-inferred-from-the-usage) – Cid Oct 21 '19 at 09:47
  • You should pass in the type you want to deserialize to. `SendCustomRequest` – Orel Eraki Oct 21 '19 at 09:48
  • Thank you. Has fixed the problem. However, if it doesen't deserialize I want to return 'null'. It says I have to return default(T) instead. Is this the same thing? – Zack Antony Bucci Oct 21 '19 at 09:49
  • yes, use `return default(T);` instead of `return null;`, you don't know at runtime if `T` is nullable. `T` could be a struct – Cid Oct 21 '19 at 09:51
  • Or, instead of using `return default(T);` you can restrict `T` to be a class (and then, nullable) by adding on your method signature `where T : class` – Cid Oct 21 '19 at 09:55
  • For more informations, see [How can I return NULL from a generic method in C#?](https://stackoverflow.com/questions/302096/how-can-i-return-null-from-a-generic-method-in-c) – Cid Oct 21 '19 at 09:55

1 Answers1

5

You need to specify the generic type when calling your method:

public string GetRace()
{
     string filter = "example";
     RaceJson currentRace = await gsaClient.SendCustomRequest<RaceJson>("races?$filter=" + filter);
}

public string GetPeople()
{
     string filter = "example";
     List<PeopleJson> currentPeople = await gsaClient.SendCustomRequest<List<PeopleJson>>("people?$filter=" + filter);
}
Grabofus
  • 1,924
  • 14
  • 17
  • Thank you. Has fixed the problem. However, if it doesen't deserialize I want to return 'null'. It says I have to return default instead. Is this the same thing? – Zack Antony Bucci Oct 21 '19 at 09:49
  • If `` is not a nullable reference type, you might not be able return null. For example `SendCustomRequest` would have a default of `0` – Grabofus Oct 21 '19 at 09:51
  • I think you can safely go with `default`, as you're working with JSON objects, not primitive values. – Grabofus Oct 21 '19 at 09:52
  • It seems that if I try use the type as a list e.g. > I get an error upon runtime. I have updated my question with the error. I believe most of the Json returns in a list anyway. – Zack Antony Bucci Oct 21 '19 at 10:13
  • 1
    You should probably create a separate question for this.. and post a sample of your data. If you alter the question people facing the same issues will not understand the answer given to it. :) – Grabofus Oct 21 '19 at 10:19
  • He's right, you vandalized your own question by fixing your primary question and adding a new one – Cid Oct 21 '19 at 11:13