36

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?

3
  • Just got back from vacation, I will look at this tonight and let you know. Thanks! Commented Jun 7, 2010 at 20:33
  • 3
    You can also use [ScriptIgnore] as [JsonIgnore] seems to not be implemented. Commented Nov 19, 2011 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. stackoverflow.com/questions/32160530/… And this one: stackoverflow.com/questions/14591750/… *btw, sry, I wanted to put this references to comments, but having no reputation to do so Commented Jul 10, 2018 at 1:22

5 Answers 5

108

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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
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);

6 Comments

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!
thanks But how do I keep using JSon(models,"text/json",JsonRequestBehavior.AlloGet) ?
@Bellash I don't know what you're asking.
@Bellash, you can use something like return Content(JsonConvert.SerializeObject(foobars, Formatting.None, jsSettings), "application/json");
It seems this is necessary even if you use [JsonIgnore] on the property that contains the loops.
|
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

Comments

2

Using this one solved my issue

using System.Text.Json.Serialization;

1 Comment

this only works if what you are using IS NOT Newtonsoft.Json
-1

In case it helps anyone, I'm on .NET 8 and Newtonsoft.Json isn't supported yet. So, "explicitly" adding this help:

using System.Text.Json.Serialization;

1 Comment

Duplicate answer. Please check existing answers and use comments for additional context

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.