Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Too many childobjects on a parent after deserialization #658

Closed
hcd opened this issue Sep 20, 2015 · 1 comment
Closed

Too many childobjects on a parent after deserialization #658

hcd opened this issue Sep 20, 2015 · 1 comment

Comments

@hcd
Copy link

hcd commented Sep 20, 2015

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&#133169 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.

@JamesNK
Copy link
Owner

JamesNK commented Sep 21, 2015

Lists contain duplicate values all the time. Skipping them isn't what a user would expect when deserializing JSON.

@JamesNK JamesNK closed this as completed Sep 21, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants