self referencing loop exception with [JsonObject(IsReference = true)]

799 views
Skip to first unread message

Antonio

unread,
May 22, 2012, 9:06:00 AM5/22/12
to ravendb
I'm doing some test with RavenDb and during the "SaveChanges" method
invocation I receive a JsonSerializationException with error:

{"Self referencing loop detected for type
'System.Collections.Generic.List`1[RavenDbTest.Category]'."}

The class that I try to save is the following:

[JsonObject( IsReference = true )]
public class Category
{
public Guid Id { get; set; }
public string Name { get; set; }
public Category Parent { get; set; }
public IList<Category> Children { get; set; }

public Category()
: base()
{
Children = new List<Category>();
}

public void Add( Category category )
{
category.Parent = this;
Children.Add( category );
}

public override Boolean Equals( Object obj )
{
return true;
}
}

As suggested at http://ravendb.net/docs/client-api/advanced/custom-serialization
I have added the JsonObject attribute in order to avoid self reference
error. The problem arise again if I override the Equals method and
returing always true.

Of course this is a unreal implementation, but I have a project where
the same error arise.

Oren Eini (Ayende Rahien)

unread,
May 22, 2012, 9:13:05 AM5/22/12
to rav...@googlegroups.com
This should go to the json.net forum, not here.

Antonio

unread,
May 25, 2012, 3:55:55 PM5/25/12
to ravendb
I have posted the bug to the json.net forum.

Just to know that the bug in the Json.net library doesn't allow to
RavnDB (build 888) to store the following class:

static void Main( string[] args )
{
var documentStore = new EmbeddableDocumentStore()
{
DataDirectory =
Path.Combine(Path.GetTempPath(), "foo"),
UseEmbeddedHttpServer =
true
};
documentStore.Initialize();

var person1 = new Dumb {Name = "John"};
var person2 = new Dumb { Name = "John" };
person1.Children.Add(person1);

using ( var session = documentStore.OpenSession() )
{
session.Store( person1 );
session.SaveChanges();
}
}

[JsonObject( IsReference = true )]
public class Dumb
{
public Guid Id { get; set; }
public String Name { get; set; }
public List<Dumb> Children { get; private set; }

public Dumb()
{
Name = String.Empty;
Children = new List<Dumb>();
}

public override bool Equals( object obj )
{
return true;
}
}

On May 22, 3:13 pm, "Oren Eini (Ayende Rahien)" <aye...@ayende.com>
wrote:

Tony Henrique

unread,
May 25, 2012, 7:39:03 PM5/25/12
to ravendb
Novice using Raven. I am having problem with this.

raven Self referencing loop detected for type

How to solve it?

Oren Eini (Ayende Rahien)

unread,
May 26, 2012, 3:36:26 AM5/26/12
to rav...@googlegroups.com

 [JsonObject(IsReference = true)]

Reply all
Reply to author
Forward
0 new messages