JsonIgnore attributes not working in ASP.NET?
Asked Answered
K

4

34

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?

Kalif answered 1/6, 2010 at 23:26 Comment(3)
Just got back from vacation, I will look at this tonight and let you know. Thanks!Kalif
You can also use [ScriptIgnore] as [JsonIgnore] seems to not be implemented.Fluoridate
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. #32161030 And this one: #14592250 *btw, sry, I wanted to put this references to comments, but having no reputation to do soRashidarashidi
T
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);
Tupelo answered 4/6, 2010 at 6:24 Comment(6)
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!Fluoridate
thanks But how do I keep using JSon(models,"text/json",JsonRequestBehavior.AlloGet) ?Doublet
@Doublet I don't know what you're asking.Tupelo
@Bellash, you can use something like return Content(JsonConvert.SerializeObject(foobars, Formatting.None, jsSettings), "application/json");Tupelo
Just received a downvote for this. I would appreciate constructive criticism as to why this is not a useful answer to the question.Tupelo
It seems this is necessary even if you use [JsonIgnore] on the property that contains the loops.Surrender
O
98

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.

Optician answered 6/1, 2021 at 17:54 Comment(1)
I am now using both attributes in both namespaces. ASP.NET uses "system.Text.json" variety when you return Json(model) from an MVC ActionOphthalmology
T
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);
Tupelo answered 4/6, 2010 at 6:24 Comment(6)
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!Fluoridate
thanks But how do I keep using JSon(models,"text/json",JsonRequestBehavior.AlloGet) ?Doublet
@Doublet I don't know what you're asking.Tupelo
@Bellash, you can use something like return Content(JsonConvert.SerializeObject(foobars, Formatting.None, jsSettings), "application/json");Tupelo
Just received a downvote for this. I would appreciate constructive criticism as to why this is not a useful answer to the question.Tupelo
It seems this is necessary even if you use [JsonIgnore] on the property that contains the loops.Surrender
L
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

Lucaslucca answered 21/9, 2019 at 18:29 Comment(0)
C
2

Using this one solved my issue

using System.Text.Json.Serialization;
Credible answered 6/3 at 12:1 Comment(1)
this only works if what you are using IS NOT Newtonsoft.JsonBurrill

© 2022 - 2024 — McMap. All rights reserved.