J4N
J4N

Reputation: 20687

Why RestSharp is unable to deserialize the received JSON?

I'm trying to retrieve a list of users of my JIRA instance:

options.Authenticator = new HttpBasicAuthenticator(_jiraConfig.UserName, _jiraConfig.Token);
options.BaseUrl = new Uri(_jiraConfig.Url);
_restClient = new RestClient(options);
var request = new RestRequest($"rest/api/2/group/member?groupname={_jiraConfig.GroupName}&includeInactiveUsers=false&startAt={start}&maxResults=100", Method.Get);
PagedQueryResult<JiraGroupUser> jiraUsers = await _restClient.GetAsync<PagedQueryResult<JiraGroupUser>>(request);

I properly receive the results:

{
    "self": "https://xxx.atlassian.net/rest/api/2/group/member?includeInactiveUsers=false&maxResults=50&groupId=f4a5587d-92ec-4cbf-824e-8489f38d147f&startAt=0",
    "nextPage": "https://xxx.atlassian.net/rest/api/2/group/member?includeInactiveUsers=false&maxResults=50&groupId=f4a5587d-92ec-4cbf-824e-8489f38d147f&startAt=50",
    "maxResults": 50,
    "startAt": 0,
    "total": 94,
    "isLast": false,
    "values": [
        {
            "self": "https://xxx.atlassian.net/rest/api/2/user?accountId=yyy",
            "accountId": "yyy",
            "avatarUrls": {
                "48x48": "https://avatar-management--avatars.us-west-2.prod.public.atl-paas.net/yyy/aaa/48",
                "24x24": "https://avatar-management--avatars.us-west-2.prod.public.atl-paas.net/yyy/aaa/24",
                "16x16": "https://avatar-management--avatars.us-west-2.prod.public.atl-paas.net/yyy/aaa/16",
                "32x32": "https://avatar-management--avatars.us-west-2.prod.public.atl-paas.net/yyy/aaa/32"
            },
            "displayName": "ABC",
            "active": true,
            "timeZone": "Europe/Zurich",
            "accountType": "atlassian"
        },
        {
            "self": "https://xxx.atlassian.net/rest/api/2/user?accountId=zzz",
            "accountId": "zzz",
            "avatarUrls": {
                "48x48": "https://avatar-management--avatars.us-west-2.prod.public.atl-paas.net/zzz/bbb/48",
                "24x24": "https://avatar-management--avatars.us-west-2.prod.public.atl-paas.net/zzz/bbb/24",
                "16x16": "https://avatar-management--avatars.us-west-2.prod.public.atl-paas.net/zzz/bbb/16",
                "32x32": "https://avatar-management--avatars.us-west-2.prod.public.atl-paas.net/zzz/bbb/32"
            },
            "displayName": "DEF",
            "active": true,
            "timeZone": "Europe/Zurich",
            "accountType": "app"
        },
        //....
    ]
}

That I'm trying to deserialize in those objects:

internal class PagedQueryResult<T> : IEnumerable<T>
{
    public List<T> Values { get; set; } = new List<T>();
    public int StartAt { get; set; }
    public int MaxResults { get; set; }
    public int Total { get; set; }
    public bool IsLast { get; set; }
    public string Self { get; set; }
    public string NextPage { get; set; }


    public PagedQueryResult()
    {
    }



    /// <summary>
    /// Returns an enumerator that iterates through the collection.
    /// </summary>
    public IEnumerator<T> GetEnumerator()
    {
        return Values.GetEnumerator();
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return Values.GetEnumerator();
    }
}

and

public class JiraGroupUser
{
    public string AccountId { get; set; }
    public string AccountType { get; set; }
    public bool Active { get; set; }
    public string DisplayName { get; set; }
    public string EmailAddress { get; set; }
    public string TimeZone { get; set; }
    public string Self { get; set;}

}

The weird part is that this code was working with a quite old RestSharp version, but after updating to the last one It doesn't work anymore.

Now I get this error, which doesn't help me:

System.Text.Json.JsonException: 'The JSON value could not be converted to CloudToolsUsersExporter.JiraExporter.Models.PagedQueryResult`1[CloudToolsUsersExporter.JiraExporter.Models.JiraGroupUser]. Path: $ | LineNumber: 0 | BytePositionInLine: 1.'

The only thing I could see is that the JSON has some additional fields(like the avatarUrls) that I don't need and therefore don't have in model.

What am I missing?

Upvotes: 0

Views: 78

Answers (1)

MindSwipe
MindSwipe

Reputation: 7855

RestSharp (probably) updated the library they're using to deserialize JSON, and the new version sees that PagedQueryResult implements IEnumerable, so it's expecting a JSON array as the root object but the actual return value is a JSON object, hence why

Path: $ | LineNumber: 0 | BytePositionInLine: 1

The parser is expecting a [ as the first character.

To fix this, remove : IEnumerable<T> from your PagedQueryReult class definition.


P.S. it is generally advised not to implement/ inherit IEnumerable/ collection classes on types that are not a collection. A PagedQueryResult isn't a collection, it's an object with some metadata and a collection as a child property.

Upvotes: 1

Related Questions