0

I have a List that looks like:

List<Product> products = new List<Product>();
Product p1 = new Product(1, "Apple", new Description("Red Apple"))
Product p2 = new Product(2, "Banana", new Description("Yellow Banana"))
products.Add(p1);
products.Add(p2);

A product looks like this:

//Product model
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual Description descriptions { get; set; }

//Description model
public string description { get; set }

Now I want to serialize this List<Product> to JSON with JSON.NET. I've tried:

var json = JsonConvert.SerializeObject(products);

But I get the following error:

Newtonsoft.Json.JsonSerializationException: 'Self referencing loop detected for property 'Module' with type 'System.Reflection.RuntimeModule'.

I also have the following line in my Startup.cs file that should avoid loops:

xy.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;

Any ideas what I'm doing wrong? Can I provide more/better informations? Thanks in advance:)

  • Kindly provide all the datastructure in details with constructors. Only thing is noticeable is `Product` constructor takes single `Description` so provide all necessary details. – Gaurang Dave Aug 27 '18 at 07:58
  • Can you please tell something about the `Description`? – Keyur Ramoliya Aug 27 '18 at 07:59
  • If your `Description` has a Product in it, try to add `[JsonIgnore]` in the variable, or if there is any objects that references the object itself. – Thaun_ Aug 27 '18 at 08:06
  • Possible duplicate of [JSON.NET Error Self referencing loop detected for type](https://stackoverflow.com/questions/7397207/json-net-error-self-referencing-loop-detected-for-type) – Daniel B Aug 27 '18 at 08:12
  • @DanielB It's not the exact same issue, because I have another error that could not be solved in your post –  Aug 27 '18 at 08:19
  • @KeyurRamoliya It's just an example I tried to give that is near on my real project –  Aug 27 '18 at 08:19
  • @Thaun_ Thanks! Tried it, but didn't resolve my issue –  Aug 27 '18 at 08:20
  • Should u add to config.Formatters.JsonFormatter.SerializerSettings? – Daniel B Aug 27 '18 at 08:33
  • Is xy.SerializerSettings for XmlSerialization? – Daniel B Aug 27 '18 at 08:34
  • @DanielB `xy.SerializerSettings` are for the JSON.NET...with that it *should* ignore the loops and normally don't throw this error –  Aug 27 '18 at 08:36
  • https://stackoverflow.com/questions/21815759/set-default-global-json-serializer-settings – Daniel B Aug 27 '18 at 08:39
  • @DanielB THANKS! It works now! You wanna write that as solution and I approve it? –  Aug 27 '18 at 08:52
  • No problem. Done! @RaphaelM. – Daniel B Aug 28 '18 at 14:56

1 Answers1

1

You should use JsonConvert's default Setting rather than SerializerSettings:

JsonConvert.DefaultSettings = () => new JsonSerializerSettings
{
    ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore
};
Daniel B
  • 3,109
  • 2
  • 33
  • 42