Newtonsoft忽略属性? [英] Newtonsoft ignore attributes?

查看:889
本文介绍了Newtonsoft忽略属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前使用相同的C#的DTO拉出来的CouchDB的数据,通过坐椅我是通过一个ASP MVC控制器返回JSON。

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

我使用的是NewtonSoft库通过控制器发送下来之前,我seralise DTO的。

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

然而,随着CouchDB中也使用NewtonSoft它也尊重财产水平NewtonSoft属性,如

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

[JsonIgnore]
[JsonProperty("foo")]

反正是有告诉newtonsoft库明确忽略这些属性?坐椅可以让我提供我自己的IObjectSerializer的IMPL,这使我在netwonsofts JsonSerializerSettings完全控制。因此,通过这些设置可以无视我的属性?

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

请问,我可以看到在这一点上是欺骗我的DTO的唯一选择。虽然并不可怕,它是不是很大。

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

唯一的其他办法,我可以看到的是把我自己的版本Newtonsoft.Json源到我的项目,使用不同的程序集的名称等等等等,但是这样一来绝对疯狂谎言和我之前,我只是欺骗了的DTO走这条路。

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.

谢谢大家。

推荐答案

我最终作出我需要的所有属性只添加属性的虚拟,而在另一个类单独覆盖他们,与相关newtonsoft属性。

I ended up making all properties I needed to only add attributes to virtual, and overriding them alone in another class, with the relevant newtonsoft attributes.

这让我有不同的序列化行为从CouchDB的时候去连载连载,并为GET,没有太多的欺骗。它是好的,和奖金,这两个耦合;在基地的任何改变,我会想反正

This allows me to have different serialisation behavior when de-serialising from CouchDB and serialising for a GET, without too much dupe. It is fine, and a bonus, that the two are coupled; any changes in the base i would want anyway.

它仍然将是不错的知道,如果我原来的问题是可能的?

It would still be nice to know if my original question is possible?

感谢

这篇关于Newtonsoft忽略属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆