33

I've got an object in my project with circular references. I've put [JsonIgnore] above the field like so:

    [JsonIgnore]
    public virtual Foobar ChildObject { get; set; }

I'm still getting circular reference errors when I serialize the object. The only fields that do not have JsonIgnore are string fields and should not cause this. Is there something else I need to do to get JsonIgnore to work?

Thanks!

JustinStolle
  • 4,182
  • 3
  • 37
  • 48
chum of chance
  • 6,200
  • 10
  • 46
  • 74
  • Just got back from vacation, I will look at this tonight and let you know. Thanks! – chum of chance Jun 07 '10 at 20:33
  • 3
    You can also use [ScriptIgnore] as [JsonIgnore] seems to not be implemented. – defines Nov 19 '11 at 17:12
  • For more information about why JsonIgnore is not working. You might need to know the differences between ASP.NET WebAPI and ASP.NET MVC. * Why JsonIgnore is not working: the two are not using the same Please also refer to the following answers. serializer. https://stackoverflow.com/questions/32160530/jsonignore-not-working-in-system-web-mvc-controller And this one: https://stackoverflow.com/questions/14591750/setting-the-default-json-serializer-in-asp-net-mvc *btw, sry, I wanted to put this references to comments, but having no reputation to do so – Kit Law Jul 10 '18 at 01:22

3 Answers3

75

I had incorrectly resolved the JsonIgnore reference.

Note that this attribute exists in more than one namespace:

  • System.Text.Json.Serialization
  • Newtonsoft.Json

I had resolved this in VS to System.Text.Json.Serialization.JsonIgnore - however I was using the Newtonsoft library for my actual Serialise/Deserialise - and hence the attribute was ignored. Changing the reference to Newtonsoft.Json.JsonIgnore resolved.

user369142
  • 2,575
  • 1
  • 21
  • 9
  • I am now using both attributes in both namespaces. ASP.NET uses "system.Text.json" variety when you return `Json(model)` from an MVC Action – Glenn Ferrie Aug 04 '23 at 14:22
34

You likely have some other property that links back to its parent. Use the ReferenceLoopHandling.Ignore setting to prevent self-referencing loops.

using Newtonsoft.Json;

JsonSerializerSettings jsSettings = new JsonSerializerSettings();
jsSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;

string json = JsonConvert.SerializeObject(foobars, Formatting.None, jsSettings);
JustinStolle
  • 4,182
  • 3
  • 37
  • 48
  • I've been looking everywhere for this; several mentions that Newtonsoft supports ignoring circular references and no mention of the actual property to set. Thanks! – defines Nov 19 '11 at 17:11
  • 1
    thanks But how do I keep using `JSon(models,"text/json",JsonRequestBehavior.AlloGet)` ? – Bellash Sep 25 '14 at 13:38
  • 2
    @Bellash I don't know what you're asking. – JustinStolle Sep 25 '14 at 18:21
  • @Bellash, you can use something like `return Content(JsonConvert.SerializeObject(foobars, Formatting.None, jsSettings), "application/json");` – JustinStolle Oct 14 '20 at 07:58
  • Just received a downvote for this. I would appreciate constructive criticism as to why this is not a useful answer to the question. – JustinStolle Oct 14 '20 at 08:02
  • 1
    It seems this is necessary even if you use [JsonIgnore] on the property that contains the loops. – Mr.Technician May 18 '21 at 16:35
4

If anyone needs an ASP.Net Core implementation of ignore child references, here it is.

public void ConfigureServices(IServiceCollection services)
{
...

    services.AddMvc()


         .AddJsonOptions(
            options => options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore
        );

    ...
}

src: https://learn.microsoft.com/en-us/ef/core/querying/related-data

burnt1ce
  • 14,387
  • 33
  • 102
  • 162