tags:

views:

111

answers:

1

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!

+2  A: 

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