当数据协定中未设置数据成员字段时如何不返回 null

发布于 2024-11-01 21:29:46 字数 1330 浏览 4 评论 0原文

我的 WCF 服务遇到一个奇怪的问题,该服务以 JSON 格式返回数据。 我想根据客户发送的请求返回有关“客户”的信息。

客户可以请求他们需要有关客户的哪些信息字段,并且服务只需发送有关客户的信息。

例如:如果客户要求提供客户列表并说他们想要名字、姓氏、城市 那么服务器应该发送一个带有每个字段名称和相应值的 json 响应,

就像...

[
  {"firstname":"john","lastname":"Goodman","city" :"NY"},
  {"firstname":"brad","lastname":"newman","city" :"LA"}
]

如果客户端只要求提供具有 ID 和城市字段的客户列表,那么响应应该如下所示

[
  {"id" :"1234","city" :"NY"},
  {"id":"1235","city" :"LA"}
]

我最初的设计是实现一个“客户”类,然后拥有每个可能的“字段” 作为这个类的一个字段。然后在我的服务方法中,我获取客户端指定的字段列表,并仅使用设置的这些属性实例化客户对象。

我的操作合同看起来像这样

[OperationContract]
[WebGet(BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json, UriTemplate = "Customers?fields={value}")]
List<Customer> GetCustomers(string value);

,但问题是当我用“DataContract”装饰类并将每个字段装饰为“DataMember”时......如果未设置某些属性,则在发送到客户端时它们仍然会被反序列化为 NULL。我不希望这种事发生。

此外,客户可能的字段列表非常长,因此类变得非常大。我宁愿将这些字段存储为服务中的枚举集合,而不是类的字段。

然后我想到让我的操作返回一个 IDictionary ,然后将每个字段和值迭代添加到该集合中。这不起作用,因为当字典被序列化时,它显示 {"Key:dfas", "Value:34"} 等,而不是我想要的

所以我有点卡住了。解决这个问题的最佳方法是什么?

我可以这样标记我的 [DataContract] 吗?如果未设置 [DataMember] 属性(即 null),那么它们根本不应该被序列化并发送到客户端?

I'm having a strange problem with my WCF service that returns data in JSON format.
I want to return information about a "Customer" based on the request sent by client.

Client can request what fields of information about a customer they need and the service needs to send only that information about the customer.

For Example: If client asks for a list of customers and says they want firstname, lastname, city
of each customer then server should send a json response with each field name and corresponding value

Something like...

[
  {"firstname":"john","lastname":"Goodman","city" :"NY"},
  {"firstname":"brad","lastname":"newman","city" :"LA"}
]

if client asks for list of customers with ID and city fields only then the response should look like this

[
  {"id" :"1234","city" :"NY"},
  {"id":"1235","city" :"LA"}
]

My initial design was to implement a "Customer" class and then have each possible "field"
as a field of this class. Then inside my service method, I was getting the list of fields specified by the client and instantiating customer objects with only those properties set.

My operation contract looks like this

[OperationContract]
[WebGet(BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json, UriTemplate = "Customers?fields={value}")]
List<Customer> GetCustomers(string value);

But the problem is when I decorate the class with "DataContract" and each Field as "DataMember"....if some properties are not set, they still get deserialized as NULL when sent to client. I dont want this to happen.

Also, the list of possible fields a customer is very long and so the class became very large. I would rather store these fields as an enum collection in my service rather than fields of a class.

I then thought of having my Operation return a IDictionary<string,object> and then add each field and value iteratively to this collection. That didn't work because, when a dictionary is serialized it shows {"Key:dfas", "Value:34"} etc etc. not what i want

So I'm kinda stuck. What's the best way to solve this problem?

Can I mark my [DataContract] such a way that if [DataMember] properties are not set i.e, null then they shouldn't be serialized and send to client at all?

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

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

发布评论

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

评论(1

岁月静好 2024-11-08 21:29:48

您可以标记每个数据成员,这样如果它为空,则不会对其进行序列化。重要部分是:EmitDefaultValue = false

[DataContract]
public class Person
{
    [DataMember(EmitDefaultValue = false)]
    public string id { get; set; }

    [DataMember(EmitDefaultValue = false)]
    public string firstname { get; set; }

    [DataMember(EmitDefaultValue = false)]
    public string lastname { get; set; }

    [DataMember(EmitDefaultValue = false)]
    public string city { get; set; }
}

You can mark each data member so it won't be serialized if it's null. The essential part is: EmitDefaultValue = false

[DataContract]
public class Person
{
    [DataMember(EmitDefaultValue = false)]
    public string id { get; set; }

    [DataMember(EmitDefaultValue = false)]
    public string firstname { get; set; }

    [DataMember(EmitDefaultValue = false)]
    public string lastname { get; set; }

    [DataMember(EmitDefaultValue = false)]
    public string city { get; set; }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文