Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Newtonsoft ignore attributes? [duplicate]

I am currently using the same C# DTOs to pull data out of CouchDB, via LoveSeat which I am going to return JSON via an ASP MVC controller.

I am using the NewtonSoft library to seralise my DTOs before sending them down through the controller.

However, as CouchDB also uses NewtonSoft it is also respecting the property level NewtonSoft attributes such as

[JsonIgnore]
[JsonProperty("foo")]

Is there anyway to tell the newtonsoft library to ignore these attributes explicitly? LoveSeat allows me to provide my own implementation of IObjectSerializer, which gives me full control over netwonsofts JsonSerializerSettings. So, can I ignore the attributes by using those settings ?

I ask as the only alternative I can see at this point, is to dupe my DTOs. While not that's not terrible, it isn't great either.

The only other way I can see is to bring in my own version of the Newtonsoft.Json source into my project, with a different assembly name etc etc. But this way madness definitely lies and I will just dupe the DTOs before I go down this road.

like image 413
Jammin Avatar asked Jun 10 '11 17:06

Jammin


2 Answers

I'm not sure if this is what you're after, but from what I understand you're looking for the [JsonIgnore] attribute. Stops properties from being serialized with the rest of the object into to JSON.

[JsonIgnore]
public string Whatever{ get; set; }
like image 86
Jimbo Avatar answered Oct 14 '22 13:10

Jimbo


One suggestion that you may not like. For best practices, I recommend having two almost identical objects. One specifically for your Data Access Layer (Domain Object) which maps to your DB. And a separate DTO that your apps care about. This way the Domain Object will mostly contain more properties than the DTO and you can separate the concerns.

like image 15
zbugs Avatar answered Oct 14 '22 14:10

zbugs