Fail to JSON serialize because of collection reference

Posts   
 
    
gilbert
User
Posts: 24
Joined: 11-Mar-2010
# Posted on: 20-Apr-2015 21:28:09   

LLBLGen Pro v3.1 Final

Hi, I am using WebApi to get data using LLBL and sending the entities back to JSON.

Saw this blog post and followed the instructions here: http://weblogs.asp.net/fbouma/how-to-make-asp-net-webapi-serialize-your-llblgen-pro-entities-to-json

However I am encountering a problem in which some entities fail to serialize and I see data coming back from the JSON call. Upon further inspection I noticed it happens when I have an entity (A) referencing another entity (B) which references back to EntityCollection<A>, and has more than one item in the collection.

For example:

I have an EntityCollection<Shoes>. Each Shoe references a Brand entity. Inside the Brand entity LLBL generated a EntityCollection<Shoes> which points back to the root entity. If I have multiple shoes of the same brand then only one result will serialize back correctly.

Looking in the generated code I see the [DataMember] attribute created for the collection.


[TypeContainedAttribute(typeof(ItemEntity))]
[DataMember]
public virtual EntityCollection<ItemEntity> Items
{
    get { return GetOrCreateEntityCollection<ItemEntity, ItemEntityFactory>("ProductLine", true, false, ref _items);    }
}

If I remove the DataMember then I am able to serialize properly. It seems that the two-way referencing is the culprit.

Since the code was generated using the designer I shouldn't really go in and change this every time. Is there any way to fix this (designer or code) so it doesn't generate DataMember attributes for these loopbacks? I can't remove this completely from NavigatorCollection because I still need it for some entity relationships.

Thanks, Gilbert

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 21-Apr-2015 00:58:29   

I trust you have disabled the circular references as in the blog post, correct?

What's the build no. of the LLBLGen runtime library used (check forum guidelines for more details)?

gilbert
User
Posts: 24
Joined: 11-Mar-2010
# Posted on: 21-Apr-2015 17:40:39   

Sorry, forgot the build no. It's LLBLGen Pro v3.1 Final (February 7th, 2011) - Version 3.1.11.0207

Where do you disable the circular reference? I read through the blog a couple times and don't see it mentioned. I followed all the steps in there, adding the attributes to the code gen and switching to the Newtonsoft Json Serializer.

Here's the piece of code I'm trying with.


EntityCollection<ItemEntity> collectionToFill = new EntityCollection<ItemEntity>();
IPrefetchPath2 prefetchPath = new PrefetchPath2((int)EntityType.ItemEntity);
prefetchPath.Add(ItemEntity.PrefetchPathItemBrand);

IPredicateExpression predicateExpression = new PredicateExpression(); ;

ISortExpression sortExpression = new SortExpression(ItemFields.ItemCd | SortOperator.Ascending);

using (DataAccessAdapter dataAdapter = new DataAccessAdapter())
{
     IRelationPredicateBucket bucket = new RelationPredicateBucket();
     bucket.PredicateExpression.Add(predicateExpression);
     dataAdapter.FetchEntityCollection(collectionToFill, bucket, 10, sortExpression, prefetchPath);
}

I noticed I can disable the DataMember attribute individually in the designer for the collections, that would make the serialization work. Is there another quicker way to do this?

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 22-Apr-2015 07:27:40   

This part.

var json = config.Formatters.JsonFormatter;
json.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.Objects;
json.SerializerSettings.ContractResolver = new DefaultContractResolver() 
    { 
        IgnoreSerializableInterface = true, 
        IgnoreSerializableAttribute = true 
    };
gilbert
User
Posts: 24
Joined: 11-Mar-2010
# Posted on: 23-Apr-2015 00:01:21   

Yes, that's been added in already. Looking at other threads I saw someone mention adding


config.Formatters.Clear();
config.Formatters.Add(json); 

I tried that also but still encountering the same problem.

When I check the result of the WebApi call, I get back the total number of objects, but the ones with a reference back to multiple items comes back as empty. Looks like the serializer fails because it doesn't know which of the item in the collection is correct.

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 23-Apr-2015 08:03:16   

gilbert wrote:

I noticed I can disable the DataMember attribute individually in the designer for the collections, that would make the serialization work. Is there another quicker way to do this?

I would go on remove the [DataMember] attribute individually on those entities you want, at LLBLGen Designer project.

David Elizondo | LLBLGen Support Team
gilbert
User
Posts: 24
Joined: 11-Mar-2010
# Posted on: 24-Apr-2015 23:10:22   

Thanks. I was hoping there was a faster way, with so many collections to remove. But I guess it only needs to be done once.