如何序列化从用 DataContract(IsReference=true) 装饰的类派生的类?

发布于 2024-12-18 08:08:49 字数 917 浏览 5 评论 0原文

我有从 System.Data.Objects.DataClasses.EntityObject 派生的 A 类。 当我尝试使用序列化时,

var a = new A();
DataContractJsonSerializer serializer = new DataContractJsonSerializer(a.GetType());
serializer.WriteObject(Response.OutputStream, a);  

出现错误

TestController+A._Id' 未标记为OptionalFieldAttribute,因此表明它必须被序列化。但是,“TestController+A”派生自用 DataContractAttribute 标记且 IsReference 设置为“True”的类。 IsReference 类上不可能有必需的数据成员。使用OptionalFieldAttribute 装饰“TestController+A._Id”,或者禁用相应父类上的IsReference 设置。

即使我使用OptionalFieldAttribute 装饰字段,我也会得到

The type ' TestController+A' 无法序列化为 JSON,因为其 IsReference 设置为“True”。 JSON 格式不支持引用,因为没有用于表示引用的标准化格式。要启用序列化,请禁用该类型或该类型的相应父类上的 IsReference 设置。

我无法修改 EntityObject 类。我想创建与 A 类完全相同的 A_Bag 类,并填充它并序列化它,而不是 A,但我认为有更优雅的方法它。

你能建议我该怎么做吗?

I have class A that derives from System.Data.Objects.DataClasses.EntityObject.
When I try to serialize using

var a = new A();
DataContractJsonSerializer serializer = new DataContractJsonSerializer(a.GetType());
serializer.WriteObject(Response.OutputStream, a);  

I get error

TestController+A._Id' is not marked with OptionalFieldAttribute, thus indicating that it must be serialized. However, 'TestController+A' derives from a class marked with DataContractAttribute and an IsReference setting of 'True'. It is not possible to have required data members on IsReference classes. Either decorate 'TestController+A._Id' with OptionalFieldAttribute, or disable the IsReference setting on the appropriate parent class.

Even if I decorate the field with OptionalFieldAttribute I get

The type 'TestController+A' cannot be serialized to JSON because its IsReference setting is 'True'. The JSON format does not support references because there is no standardized format for representing references. To enable serialization, disable the IsReference setting on the type or an appropriate parent class of the type.

I cannot modify EntityObject class. I thought to create A_Bag class exactly as A class and fill it and serialize it instead of A, but I think there's more elegant way to do it.

Can you suggest how I can do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

む无字情书 2024-12-25 08:08:49

我认为您可以在此处使用“数据契约代理”(通过 IDataContractSurrogate 接口使用)。

数据契约代理是基于您已经使用的数据契约模型构建的高级功能。当您想要更改类型的序列化、反序列化或(如果您正在处理 XML)投影到架构中的方式时,它允许您进行类型自定义和替换。

在您的情况下,使用 IDataContractSurrogate 可以让您在每个类型或每个对象的基础上进行自定义 JSON 序列化和反序列化。 IDataContractSurrogate 将提供在序列化和反序列化期间由 DataContractSJsonerializer 将一种类型替换为另一种类型所需的方法,并且您可能希望为您的场景提供不同的“特殊”中间类型。

希望这有帮助!

I think you can use a "data contract surrogate" here (used via the IDataContractSurrogate interface.)

The data contract surrogate is an advanced feature built upon the Data Contract model you're already using. It lets you do type customization and substitution in situations where you want to change how a type is serialized, deserialized, or (if you're dealing with XML) projected into schema.

In your case, the use of IDataContractSurrogate lets you do custom JSON serialization and deserialization on a per-type or per-object basis. An IDataContractSurrogate would provide the methods needed to substitute one type for another by the DataContractSJsonerializer during serialization and deserialization, and you may want to provide a different "special" intermediary type for your scenario.

Hope this helps!

何以心动 2024-12-25 08:08:49

JSON.Net 支持使用 IsReference=true 标记的对象的序列化。

这里有详细的演练:

http://dotnet.learningtree.com/2012/04/03/working-with-the-entity-framework-and-the-web-api/

JSON.Net supports serialization of objects marked with IsReference=true.

There is a detailed walkthrough here:

http://dotnet.learningtree.com/2012/04/03/working-with-the-entity-framework-and-the-web-api/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文