0

I have the requirement that my web controller needs to return a class with only public fields. E.g. I have

public class RunningScenario
{
    public int Id;
    public string Name;
}

and want to return this from my controller like this

[ApiController]
[Route("api/[controller]")]
public class ScenarioController : ControllerBase
{
    [HttpPost("start")]
    [ProducesResponseType(StatusCodes.Status404NotFound)]
    [ProducesResponseType(StatusCodes.Status400BadRequest)]
    [ProducesResponseType(StatusCodes.Status200OK)]
    public async Task<ActionResult<RunningScenario>> StartScenario(int scenarioId)
    {
        // Error handling

        var runningScenario = await _scenariorRunner.Start(scenarioId);
        return Ok(runningScenario);
    }
}

Though, running this just returns {} instead of {"Id": 1, "Name": "Scenario 1"}.

I also tried to add the [JsonInclude] attribute to the fields, with no effect. The result is still an empty object.

So, my question is, how do I get the public fields to be serialized and deserialized?

Pierre K
  • 3
  • 3
  • Do not use fields, use properties instead. If you cannot change the fields to properties, then create a new class and map the class to the new class and return the new class. – Fred May 04 '23 at 09:20
  • In the `Program.cs` file you might able to configure `JsonSerializerOptions` by setting `IncludeFields` to `true`. – Fred May 04 '23 at 09:21
  • Thank you, Fred , sadly I can't avoid using fields, because I am sharing this class with a Unity client and there serialization only works with fields and not properties. That's why I am stuck with fields. – Pierre K May 04 '23 at 09:28

2 Answers2

1

Newer versions of aspnet use the json serializer from System.Text.Json by default instead of Newtonsoft.Json. By default the serializer of System.Text.Json will not serialize fields, unlike Newtonsoft.Json.

The better option is to make the fields properties (e.g. public string Name { get; set; }). If that's really not an option you can adjust the settings of the serializer in your startup. E.g.

builder.Services.AddControllers().AddJsonOptions(o =>
{
    o.JsonSerializerOptions.IncludeFields = true;
});
NotFound
  • 5,005
  • 2
  • 13
  • 33
  • 1
    An alternative would be to create a DTO class, such as `RunningScenarioDto` and map the fields from `RunningScenario` to the corresponding properties in the DTO class. – Fred May 04 '23 at 11:00
0

I fix this issue in .net 6, by adding this to the Program.cs file:

using System.Text.Json.Serialization;

builder.Services.AddControllersWithViews()
                .AddJsonOptions(x => x.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.IgnoreCycles);
builder.Services.AddMvc()
                .AddJsonOptions(x => x.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.IgnoreCycles);

Check this solution: 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

toyota Supra
  • 3,181
  • 4
  • 15
  • 19
isghioua
  • 1
  • 1