0

I'm trying to make an API using .NET Core 3.1 with C# programming language which utilizes Firebase real-time database from Google through FireSharp Nu-Get package. When I ran it, I got this exception: System.Text.Json.JsonException: A possible object cycle was detected which is not supported. This can either be due to a cycle or if the object depth is larger than the maximum allowed depth of 32. I read here that I can just install Microsoft.AspNetCore.Mvc.NewtonsoftJson package and ignore the reference loop handling, which works fine. But the data format looks unreadable, very hard to deserialize, and way too long:

{
"stateMachine": {
    "<>1__state": 0,
    "<>t__builder": {},
    "bpjs": "12345678",
    "reservationTime": "24-11-2020",
    "<>4__this": {}
},
"context": {},
"moveNextAction": {
    "method": {
        "name": "MoveNext",
        "declaringType": ...
...
((lots of irrelevant things))
...

    }
},
"result": {
    "queueNumber": "1",
    "bpjs": "12345678",
    "name": "Addi",
    "poli": "Umum",
    "reservationTime": "24-11-2020",
    "status": "Queueing"
},
"id": 2,
"exception": null,
"status": 5,
"isCanceled": false,
"isCompleted": true,
"isCompletedSuccessfully": true,
"creationOptions": 0,
"asyncState": null,
"isFaulted": false}

Before this, I already tried to do some mapping and not directly use my data model by following this video's tutorial on doing DTOs, but it didn't work most likely because I don't use SQL Database.

Now I'm trying to use this API on a Flutter app. Is there anyway for me to maybe do some minor changes on my API code (anything that doesn't involve completely changing the database to SQL) to reformat my response? Or maybe a way to just partly deserialize my response, since I only need the "result" part and not anything else?

Edit:

This is my main code to get the data:

        public async Task<QueueData> GetQueueData(string bpjs, string reservationTime)
    {
        //set up connection
        IFirebaseConfig config = new FirebaseConfig
        {
            AuthSecret = myAuthSecret,
            BasePath = myBasePath
        };

        IFirebaseClient client = new FireSharp.FirebaseClient(config);

        //checkqueuenumber
        string queueCounter = await QueueDbConnection.CheckQueueNumber(reservationTime, client);

        //getresult
        QueueData result = await QueueDbConnection.GetResult(bpjs, reservationTime, queueCounter, client);
        return result;
    }

this is my controller to call the function above:

        [HttpGet("{bpjs}/{reservationTime}")]
    public ActionResult<QueueData> GetQueueData(string bpjs, string reservationTime)
    {
        var queueData = _repository.GetQueueData(bpjs, reservationTime);
        return Ok(queueData);
    }

This is my model for the QueueData, which is the type I'm trying to return as response:

    public class QueueData
{
    [Required]
    public string QueueNumber { get; set; }
    [Required]
    public string BPJS { get; set; }
    [Required]
    public string Name { get; set; }
    [Required]
    public string Poli { get; set; }
    [Required]
    public string ReservationTime { get; set; }
    [Required]
    public string Status { get; set; }
}

this is another model class called control to monitor the number of people queueing each day:

    public class QueueCounter
{
    [Required]
    public string q { get; set; }
}
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
  • That's not the `ReferenceLoopHandling.Ignore` format. All that [`ReferenceLoopHandling.Ignore`](https://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_ReferenceLoopHandling.htm) does is to *Ignore loop references and do not serialize.* Can you share a [mcve]? – dbc Dec 03 '20 at 15:41
  • @dbc Thank you, I added it just now. There are some iteration in the CheckQueueNumber and GetResult method, but I have used them in another program already and they work perfectly fine. I would gladly provide the code if it helps, though. – Alexandra Winastwan Dec 03 '20 at 16:09
  • Please don't put "solved" in your question title. Accepting an answer (as you have done here) is the mechanism to mark a question as solved. – Wai Ha Lee Dec 14 '20 at 08:03

1 Answers1

1

So, I solved this. I went back to here and apparently someone said the error I got in the first place was because not all async functions had been waited or handled properly. So I went through my codes once again and finally found the problem in my controller. After I made my controller async then changed the return type into Task<ActionResult>, everything went perfectly fine!