We are running into a situation on an MVC3 project with both the Microsoft JSON serializers and JSON.NET.
Everybody knows DateTime's are basically broken in Microsoft's serializers, so we switched to JSON.NET to avoid this issue. That works great, except that some of the classes we are trying to serialize are POCOs with DataContract/DataMember attributes. They are defined in an assembly that is referenced in multiple places. Additionally, they have some other display properties that are not marked as DataMembers for efficiency. For instance, a Customer
[DataContract]
public class Customer
{
[DataMember]
public string FirstName { get; set;}
[DataMember]
public string LastName { get; set;}
public string FullName
{
get
{ return FirstName + " " + LastName; }
}
}
When this customer is passed over WCF the client side can reference that assembly and use the FullName just fine, but when serialized with JSON.NET it sees that FullName isn't a [DataMember]
and doesn't serialize it. Is there an option to pass to JSON.NET to tell it to ignore the fact that a class has [DataContract]
attribute applied?
Note: Using the JavaScriptSerializer in .NET works fine for the FullName property, but DateTimes are broken. I need JSON.NET to ignore the fact that this class has DataContract/DataMember attributes and just do standard public field serialization like it would if they weren't there.
If you want to ignore the presence of
DataContractAttribute
for all types without having to add additional attributes, then a custom contract resolver is the correct solution. However, as of Json.NET 9.0.1 Amry's resolver no longer works. Doolali's resolver works but it has the additional side effect of serializing all public properties including those marked with[JsonIgnore]
. If you require a contract resolver that only ignores the presence ofDataContractAttribute
but otherwise behaves like the default contract resolver, the following can be used:You may want to cache the contract resolver for best performance.
Have you tried this?
IgnoreDataMemberAttribute
Simply use Json.Net's OptOut attribute. It will take precedence over DataContract.
I was having a problem almost related to what you're having, and managed to find a solution by going through Json.NET's codes. So it may not be the best solution, but it works for me.
To do this, you need to implement your own
IContractResolver
. An over-simplified implementation of that to include all parameters and ignores all attributes (not justDataContract
but other built-in Json.NET's rules as well, so whatever options you set that should originally affect the members selection is now being overidden by this code):And here comes the code usage example:
As Amry said you can uses your own IContractResolver.
Unfortunately the solution provided by Amry didn't work for me, below is the solution that I managed to get working:
There are a few lines commented, these wern't required to make my solution work, but you never know!
This has the same usage as Amry's solution:
Hope this helps!
In accordance to the Json.NET documentation
[DataMember]
attributes are ignored if the properties are also annotated with Json.NET specific attributes (such as[JsonProperty]
).See the Serialization Attributes documentation for details:The documentation only covers the name property, but for my experience the
[JsonProperty]
attribute also fully shadows settings done by the[DataMember]
attribute. So, if it is feasible for your case, also add Json.NET attributes to the properties for which the [DataMember] annotation should be ignored.