Skip to content
This repository was archived by the owner on Jan 8, 2024. It is now read-only.

Self-referencing objects and JSON serialization #48

Open
svallis opened this issue Feb 23, 2017 · 4 comments
Open

Self-referencing objects and JSON serialization #48

svallis opened this issue Feb 23, 2017 · 4 comments
Assignees
Labels
bug Decribes a bug or an invalid behavior from the library
Milestone

Comments

@svallis
Copy link

svallis commented Feb 23, 2017

Objects that self-reference are triggering an exception when being serialized:

JsonSerializationException: Self referencing loop detected with type 'Project.Object'. Path '[0].property1.property2'.

See:

http://stackoverflow.com/questions/7397207/json-net-error-self-referencing-loop-detected-for-type

https://github.com/ALMMa/datatables.aspnet/blob/master/src/DataTables.AspNet.AspNetCore/DataTablesResponse.cs#L162

I'd imagine that ReferenceLoopHandling.Ignore would be a suitable default for this use case, but if there's concern around that it'd be great if the option was exposed in some way.

@jorismathijssen
Copy link

Hello @svallis im currently experiencing this problem. Is there a sollution or am i doing something wrong?

@svallis
Copy link
Author

svallis commented Mar 30, 2017

@jorismathijssen You can fork/copy the project and change the line I linked to above from:

var settings = new JsonSerializerSettings() { ContractResolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver() };

to

var settings = new JsonSerializerSettings() { ContractResolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver(), ReferenceLoopHandling = ReferenceLoopHandling.Ignore };

@jorismathijssen
Copy link

@svallis I did add the referenceLoopHandling to the global.asax file and had a lot of out of memory exceptions. I think there is a lot more wrong with my project. I will try this sollution!

@svallis
Copy link
Author

svallis commented Mar 30, 2017

@jorismathijssen If your types which reference each other reference the same instance of each other then you've got an infinite loop, yeah.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Decribes a bug or an invalid behavior from the library
Projects
None yet
Development

No branches or pull requests

3 participants