有没有办法让 JavaScriptSerializer 忽略某种泛型类型的属性?
我正在为我的模型使用实体框架,并且我需要将它们序列化为 JSON。问题是 EF 包含所有这些非常好的导航集合(例如,我的 User 模型上有一个 Orders 属性),当我去序列化这些对象时,序列化程序会尝试获取这些集合的值,而 EF 会因为我尝试而对我大喊大叫使用已处置的上下文
ObjectContext 实例已被释放,不能再用于需要连接的操作。
我知道我可以使用 [ScriptIgnore] 装饰我的属性,以使序列化程序不理会它们,但这是 EF 的问题,因为它会为这些属性生成代码。
有没有办法使序列化器不序列化通用类型 EntityCollection<> 的属性?
或者有没有办法使用另一个强大的 json 库(如 JSON.Net)来做到这一点?
I am using the Entity Framework for my models, and i have need to serialize them to JSON. The problem is that EF includes all these really nice navigational collections (For instance my User model has an Orders property on it) and when I go to serialize these objects the serializer tries to get the value for those collections and EF yells at me for trying to use a disposed context
The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.
I know I can decorate my properties with [ScriptIgnore] to make the serializer leave them alone, but thats a problem with EF as it generates the code for those properties.
Is there a way to make the serializer not serialize properties that are of the generic type EntityCollection<>?
Alternatively is there a way to do this with another robust json library like JSON.Net?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以声明一个自定义合约解析器来指示要忽略哪些属性。这是一个通用的“可忽略”,基于 我在这里找到的答案:
和用法:
You can declare a custom contract resolver which indicates which properties to ignore. Here's a general-purpose "ignorable", based on the answer I found here:
And usage:
如果想法只是将这些对象返回到客户端,为什么不使用匿名类返回所需的内容呢?
假设您有一个丑陋的 EntityFrameworkClass 对象列表,您可以这样做:
If the idea is simply to return those objects to the client side why don't you just return what you need using anonymous classes?
Assuming you have this ugly heavy list of EntityFrameworkClass objects, you could do this:
这就是我的一点小小的贡献。对 @drzaus 答案的一些更改。
描述:进行了一些重新调整并启用了 Fluent。并进行了一些修复以使用 PropertyType 而不是 DeclaringType。
用法:
This is my little contribution. Some changes to @drzaus answer.
Description: Some resharped changes and Fluent enabled. And a little fix to use PropertyType instead of DeclaringType.
usage:
添加到 @drzaus 的答案,我修改了
IsIgnored
方法以使其更加通用 -并且它的用法:
上面帮助我制作了一个通用序列化器,用于将 Thrift 对象转换为标准 JSON。我想在序列化实现 Thrift.Protocol.TBase 接口的类的对象时忽略 __isset 属性。
希望有帮助。
PS - 我知道将 Thrift 对象转换为标准 JSON 达不到其目的,但这是与遗留系统接口的要求。
Adding on to @drzaus's answer, I modified the
IsIgnored
method to make it more generic -And its usage:
The above helped me make a generic serializer for converting Thrift objects to standard JSON. I wanted to ignore the
__isset
property while serializing objects of classes that implement theThrift.Protocol.TBase
interface.Hope it helps.
PS - I know converting a Thrift object to standard JSON defeats its purpose, but this was a requirement for interfacing with a legacy system.
如果您使用 JSON.NET,则可以使用 JsonIgnore 等属性来忽略某些属性。我在序列化通过 NHibernate 加载的对象时使用此功能。
我认为也有可能添加约定。也许您可以为您的 EF 属性实现一个过滤器。
If you use JSON.NET you can use attributes like JsonIgnore to ignore certain properties. I use this feature when serializing objects loaded via NHibernate.
I think there is a possibility to add conventions, too. Maybe you can implement a filter for your EF properties.