I have a few composed object being returned on a WebApi Restful service (for simplicity):
class Item {
List<Category> Categories { get; set; }
object Value { get; set; }
}
class Category {
List<Item> ItemsInCategory { get; set; }
}
These values are being served by a simple ApiController:
publicHttpResponseMessage getItems()
{
List<Category> categories;
...
return Request.CreateResponse(HttpStatusCode.OK, new { results = items});
}
The problem: Say Item(A) is in Category(A) this will result a circular dependency which will "stuck" the serialization. Therefore the webapi team (which is the last time I am using as a backend B.T.W) has exposed reference loop handling: (WebApiConfig.cs)
JsonMediaTypeFormatter jsonFormatter = GlobalConfiguration.Configuration.Formatters.JsonFormatter;
var jSettings = new JsonSerializerSettings()
{
Formatting = Formatting.Indented,
DateTimeZoneHandling = DateTimeZoneHandling.Utc,
ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
};
jsonFormatter.SerializerSettings = jSettings;
That said it doesn't really work.
Nor the "MaxDepth" property.
I've searched many ways and couldn't find a built-in way to achieve this (there are many "hacking") that achieve some variations of JsonConverter (and also [JsonIgnore]) that help achieve some proportions of this.
This is a VERY common request to control the depth of the response, isn't it?
Any chance Microsoft has completely ignored this issue and there is no built in way to deal with this issue????