I am doing some work with C#, AJAX and JSON and am getting a Self referencing loop error. I am managing to get around this using the JsonIgnore attribute, but I was wondering if someone can give me a proper explanation as to what is actually happening here.
Many thanks.
Dave
We don’t get much detail on your problem, but it’s probably exactly what you’re describing: a loop of self-reference, or a circular chain of references.
Say you’ve got a variable of the type
Userthat has a propertypublic UserImage Image. Now, say the typeUserImagehas a propertyUserthat references back to the user.In your .NET code, it’s just that.
myImage.Usergives yo uthe user,myUser.Imagegives you the image. But imagine you want to serializemyUser(into JSON, say). Then you have to cycle every property ofUserand serialize that, recursively. The serializer would start off like thisnow it has to serialize the user image. And remember
UserImagehas the variable “User”.but the user is the very same user that we’re trying to reference
now we have to serialize the image for that user, but that, again, is the same image as before:
so we never reach an end product if we’re constantly serializing a circular reference.