0

I have a database structure that has several referential loops. When I try to serialize this with JsonSerializer I get:

Self referencing loop detected with type: Path 'employee.EmployeeStockAssignment[0].Person.Department.Company.Departments'.

If I set ReferenceLoopHandling = ReferenceLoopHandling.Ignore this instead gives a stackoverflow.

Is there anyway do this without creating a new object?

EDIT: To expand on WHY I don't want to create a new object ( or modify the existing one ). The object tree's I'm working on are fairly complex, and if I for some reason forget to create a new object/modify the existing one 100% correct, this will actually crash the web server (together with ReferenceLoopHandling.Ignore that is). For now I'm casting everything to anomyous objects before I serialize, and I'm running ReferenceLoopHandling.Error in case I forget somewhere.

devzero
  • 2,510
  • 4
  • 35
  • 55
  • You need to disable the serialization of the `Departments` property inside your `Company` class. If you disable that serialization, this error should not occur anymore. – Flater Oct 04 '17 at 09:56
  • What to I do then when I want to serialize Company WITH all departmens? This is just the same as creating a new object... – devzero Oct 04 '17 at 09:58
  • I meant "the Departments property inside your Company class", not "disable serialization inside your Company class", but I see how my comment was ambiguous. Note that serialization can be disabled in many ways, e.g. in your current serialization method (so that it doesn't affect other serializations), mapping a Company to an anonymous object with the same properties **minus** the Departments property. – Flater Oct 04 '17 at 10:04
  • If I disable serialization on the Departments property, I'm forced to create a new anoymous object when I want company with departments. Also, I'm aware that I can create an anonymous object. The question spesificaly asks if there is any way to to this WITHOUT having to create that anonymous object. – devzero Oct 04 '17 at 11:21
  • (1) Your question never mentions anonymous objects, I cannot know that you're excluding that as an answer (2) Read my second comment again. I wasn't arguing to disable serialization in the class and then use anonymous objects to get around the disabled serialization. I was suggesting the opposite: **not** disabling the serialization in the class, but using an anonymous method to _temporarily_ disable serialization (by omitting the properties you do not want to serialize). (3) Another option would be to **empty** the Departments property for this particular serialization. – Flater Oct 04 '17 at 11:25
  • (1) My question says " without creating a new object" anonymous or not does not really matter to me. (2) Se my comment to (1), disabling serialization only moves the creation of new objects from one place to another – devzero Oct 04 '17 at 11:50
  • 1
    Have you tried setting `PreserveReferencesHandling` to `Objects` instead of setting `ReferenceLoopHandling` to `Ignore`? – Brian Rogers Oct 04 '17 at 14:18
  • Have you tried fix #2 or fix #3 from [this answer](https://stackoverflow.com/a/18223985/3744182)? You may also have to disable proxies and lazy loading as shown [here](https://stackoverflow.com/a/22999355/3744182). – dbc Oct 04 '17 at 19:01
  • #2 and #3 both violates what I'm asking for "without creating any new objects". I will try to diasable proxies and lazy loading – devzero Oct 05 '17 at 10:35

0 Answers0