This project has moved. For the latest updates, please go here.
2

Closed

WebAPI serialization fails with Entities

description

I'm using .NET's WebAPI for creating web services. Using it with MongoRepository I ran into an issue where the serialization from the JSON to my models inheriting from Entity doesn't work, none of the properties are captured. I found out that it is because of the DataContract and DataMember attributes used inside the Entity class within MongoRepository. Once I removed those the JSON serialization worked for my models.
Closed Feb 12, 2015 at 5:02 PM by RobIII
Today we moved the MongoRepository project to GitHub so I would kindly like to request you open your issue there if you still want to comment / discuss or think the issue is not ready to be closed yet to keep everything in one place.

We're sorry for any inconvenience.

comments

fbrooke wrote Aug 30, 2014 at 10:51 PM

Adding this attribute after the DataContract one rather than removing it also solved the issue
[JsonObject(MemberSerialization.OptOut)]

RobIII wrote Sep 5, 2014 at 7:39 PM

Won't that break serialization for others wich would want to "OptIn"? I don't see how I could keep both 'camps' happy? I'm currently in a hurry and can't see the direct consequences this change would have. I'd have to investigate a bit more. I'll try to get back to this soon.

wrote Dec 2, 2014 at 3:49 PM

fbrooke wrote Dec 26, 2014 at 10:15 AM

The problem was that the DataContract attribute forced opt in serialization and I didn't realise at the time. I didn't want to clutter my models with DataMember attributes on each property when all my properties were intended to be serialized.
Maybe you should rethink having the DataContract attribute. MSDN says the attribute does not inherit but the Json.NET library doesn't adhere to that http://json.codeplex.com/discussions/357850.
Similar experience is described here http://refactorthat.com/2014/01/18/getting-datacontract-atttribute-and-web-api-to-work-together/

wrote Feb 12, 2015 at 5:02 PM