You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi,
I'm having a issue with deserialization of objectgraphs, that are generated entites, using LLBLGen as my ORM. LLBLgen has built in 2-way child-parent relationships, so that when you set a Parent-property on a Child, the child is automatically added to the Children-property on the Parent. Really cool, but not really compatible with the way the Json.Net deserialization works. Json.Net will deserialize the child correclty and set the Parent property, so due to the 2way relation, the child is now also in the children-collection on the parent. But afterwards the child is added again tot he Children-property on the Parent. So the result is that the child object appears twice in the children-collection.
(see also http://www.llblgen.com/TinyForum/Messages.aspx?ThreadID=23482&StartAtMessage=0𠠱 for a more elaborate discussion on it and code sample)
I've downloaded the code to debug this, and pinpointed the problem to line 1405 in the JsonSerializerInternalReader.cs : list.Add(value);
As a quickifx to test , I changed this line 1405 to : if (!list.Contains(value)) list.Add(value); and now my collections are correct ! Probably this could be written in a more effectient way than using Contains(), just comparing Object.ReferenceEquels() would be enough.
Edit :
This should be the most performant : if (!list.Cast<object>().Any(existing => Object.ReferenceEquals(existing, value))) list.Add(value);
But maybe this is more readable :
bool exists = false;
foreach (var existing in list)
if (Object.ReferenceEquals(existing, value))
{
exists = true;
break;
}
if (!exists) list.Add(value);
Edit : hmm .there might be side effects , eg. when you actually want to serialize entities with subobject-lists that contain duplicates ...
Kind regards,
Sven.
The text was updated successfully, but these errors were encountered:
Hi,
I'm having a issue with deserialization of objectgraphs, that are generated entites, using LLBLGen as my ORM. LLBLgen has built in 2-way child-parent relationships, so that when you set a Parent-property on a Child, the child is automatically added to the Children-property on the Parent. Really cool, but not really compatible with the way the Json.Net deserialization works. Json.Net will deserialize the child correclty and set the Parent property, so due to the 2way relation, the child is now also in the children-collection on the parent. But afterwards the child is added again tot he Children-property on the Parent. So the result is that the child object appears twice in the children-collection.
(see also http://www.llblgen.com/TinyForum/Messages.aspx?ThreadID=23482&StartAtMessage=0𠠱 for a more elaborate discussion on it and code sample)
I've downloaded the code to debug this, and pinpointed the problem to line 1405 in the JsonSerializerInternalReader.cs :
list.Add(value);
As a quickifx to test , I changed this line 1405 to :
if (!list.Contains(value)) list.Add(value);
and now my collections are correct ! Probably this could be written in a more effectient way than using Contains(), just comparingObject.ReferenceEquels()
would be enough.Edit :
This should be the most performant :
if (!list.Cast<object>().Any(existing => Object.ReferenceEquals(existing, value))) list.Add(value);
But maybe this is more readable :
bool exists = false;
foreach (var existing in list)
if (Object.ReferenceEquals(existing, value))
{
exists = true;
break;
}
if (!exists) list.Add(value);
Edit : hmm .there might be side effects , eg. when you actually want to serialize entities with subobject-lists that contain duplicates ...
Kind regards,
Sven.
The text was updated successfully, but these errors were encountered: