Error deserializing Json . NET CORE 5 - Jsonexception: A possible Object Cycle was Detected

Asked

Viewed 252 times

1

I basically created an API project on. NET 5. My idea is to consume their API (Take repository information), and then make some information available in my API. The request is successful but there is always the same error in the part of converting a JSON to an object.

Repositoriescontroller:

using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text.Json;
using System.Threading.Tasks;

[ApiController]
[Route("[controller]")]
public class RepositoriesController : ControllerBase
{
    private static readonly HttpClient client = new HttpClient();

    private readonly ILogger<RepositoriesController> _logger;

    public RepositoriesController(ILogger<RepositoriesController> logger)
    {
        _logger = logger;
    }

    [HttpGet]
    public IActionResult Get(string number)
    {
        return Ok(ProcessRepositories());
    }

    private static async Task ProcessRepositories()
    {
        client.DefaultRequestHeaders.Accept.Clear();
        client.DefaultRequestHeaders.Accept.Add(
            new MediaTypeWithQualityHeaderValue("application/vnd.github.v3+json"));
        client.DefaultRequestHeaders.Add("User-Agent", ".NET Foundation Repository Reporter");

        var streamTask = client.GetStreamAsync("https://api.github.com/orgs/dotnet/repos?sort=created&per_page=5&direction=desc");
        var repositories = await JsonSerializer.DeserializeAsync<List<Repository>>(await streamTask);
    }
}

Repository class:

 public class Repository
{
    public string full_name { get; set; }
    public string node_id { get; set; }
    //public string description { get; set; }
}

But always in the stretch:

        var repositories = await JsonSerializer.DeserializeAsync<List<Repository>>(await streamTask);

The browser returns me the error:

Jsonexception: A possible Object Cycle was Detected. This can either be due to a Cycle or if the Object Depth is Larger than the Maximum allowed Depth of 32. Consider using Referencehandler.Preserve on Jsonserializeroptions to support Cycles.

I wanted to understand why only with two properties or even one ends up giving this error.

Documentation of the github api:

https://docs.github.com/en/rest/reference/repos

As a basis to understand how the API request works I used the official microsoft documentation:

https://docs.microsoft.com/pt-br/dotnet/csharp/tutorials/console-webapiclient#deserialize-the-json-result

1 answer

0

I did not understand but it was something related to the body of Sponse.

Basically:

 var streamTask = client
                   .GetStreamAsync("https://api.github.com/orgs/dotnet/repos?sort=created&per_page=5&direction=desc");
        var repositories = await JsonSerializer.DeserializeAsync<List<Repository>>(await streamTask);

Was to

using Newtonsoft.Json;

var response = client
                .GetAsync("https://api.github.com/orgs/dotnet/repos?sort=created&per_page=5&direction=desc").Result;
            string responseBody = response.Content.ReadAsStringAsync().Result;
            var repositories = JsonConvert.DeserializeObject<List<Repo>>(responseBody);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.