通过检查 WCF 中传入的 JSON 进行动态类型选择?

发布于 2024-11-03 15:43:36 字数 821 浏览 1 评论 0原文

我有一个发布到 WCF 服务的 JSON 对象的复杂层次结构。根据设计和其他要求,每个 JSON 对象都有一个整数 id,从概念上表示其类型或字段布局(我们将其称为类型 id)。

我想要完成的是通过检查每个传入的整数类型 id 来控制为每个 JSON 对象的反序列化选择哪个 .NET 类型。

输入示例:

{
 "typeId": 4,
 "someField1": "foo",
 "someField2": "bar",
 "otherObject": 
    {
     "typeId": 7
     "someField3": "abc",
     "someField4": "xyz"
    }
}

示例(理想) 流程:

1. I receive partially parsed object.
2. I inspect "typeId" which has value 4.
3. I notify the deserialization process that I elect to use my .NET type FooBarA.
4. I receive partially parsed object.
5. I inspect "typeId" which has value 7.
6. I notify the deserialization process that I elect to use my .NET type FooBarB.

可以这样做吗?我似乎记得 asmx 风格的服务曾经包含一个类似于我的类型 id 的 __type 字段,但我不记得它的确切用途或是否可以在 WCF 中启用它作为替代方案。

I have a complex heirarchy of JSON objects posting to a WCF service. By design and other requirements, each JSON object has an integer id representing conceptually its type or field layout (let's call this the type id).

What I would like to accomplish is control over which .NET type is chosen for each JSON object's deserialization, through the inspection of each incoming integer type id.

Example input:

{
 "typeId": 4,
 "someField1": "foo",
 "someField2": "bar",
 "otherObject": 
    {
     "typeId": 7
     "someField3": "abc",
     "someField4": "xyz"
    }
}

Example (Ideal) Process:

1. I receive partially parsed object.
2. I inspect "typeId" which has value 4.
3. I notify the deserialization process that I elect to use my .NET type FooBarA.
4. I receive partially parsed object.
5. I inspect "typeId" which has value 7.
6. I notify the deserialization process that I elect to use my .NET type FooBarB.

Is this or similar possible to do? I seem to recall asmx-style services used to include a __type field similar to my type id I suppose, but I don't recall its exact purpose or whether that can be enabled in WCF as an alternative perhaps.

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

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

发布评论

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

评论(1

你的他你的她 2024-11-10 15:43:36

您可以将传入的 JSON 收集为字符串(如果您想要“具体”类型),也可以接受 JSON 作为流(如果您处于基于 REST 的场景中,则非常有用),然后将其复制到 MemoryStream 中多次处理。由于 DataContractJsonSerializer 在 Stream 之外工作,因此最好的用例是将其转储到 MemoryStream 中。

您描述的反序列化用例将相当难以实现,因为如果嵌套对象(在您的情况下为“otherObject”)不是父类(在本例中为 FooBarA)的成员,那么反序列化过程就会继续忽略该值,因为它无处可放。

换个角度来说,使用 Json.Net 是一个更耗时的过程,您可以像使用 Linq to XML 一样以类似的方式单步执行 JSON。 Json.Net 的优点是它允许您使用不一定完美映射到具体类的 JSON。它在序列化链中与 WCF 配合得不好,但我在 WCF 端使用过几次它来处理提供的 JSON,该 JSON 与具体类正在寻找的内容不完全匹配。

您可以在这里找到 Json.Net - http://json.codeplex.com/

You can collect the incoming JSON as a either a string (if you want the "concrete" type) or you can accept the JSON as a Stream (very useful if you are in a REST based scenario) and then copy it into a MemoryStream for processing several times over. Since the DataContractJsonSerializer works off the Stream, your best use case is dumping it into the MemoryStream.

The deserialization use case you are describing is going to be rather difficult to pull off because if the nested object (in your case "otherObject") is not a member of the parent class (in this case FooBarA) then the deserialization process is just going to ignore the value because it has nowhere to put it.

Switching gears, a more time consuming process would use Json.Net and you could step through the JSON in a similar style as you would with Linq to XML. The advantage of Json.Net is that it allows you to play with JSON that doesn't necessarily map perfectly to concrete classes. It doesn't play nicely with WCF in the serialization chain, but I've used it a few times on the WCF side to deal with JSON that gets supplied that doesn't match up exactly with what an concrete class is looking for.

You can find Json.Net here - http://json.codeplex.com/

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