本文介绍了在数据协定中未设置数据成员字段时如何不返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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

类似...

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

如果客户仅要求提供具有ID和城市字段的客户列表,则响应应如下所示

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"}
]

我最初的设计是实现一个"Customer"类,然后将每个可能的"field"作为此类课程的一个领域.然后,在服务方法内部,我获得了由客户端指定的字段列表,并仅设置了那些属性来实例化客户对象.

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.

我的运营合同如下

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

但是问题是当我用"DataContract"装饰类并且每个字段作为"DataMember" .....如果未设置某些属性,当它们发送给客户端时,它们仍然会反序列化为NULL.我不希望这种情况发生.

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.

然后我想到让Operation返回IDictionary<string,object>,然后将每个字段和值迭代地添加到此集合.那是行不通的,因为当字典被序列化时,它会显示{"Key:dfas", "Value:34"}等,而不是我想要的

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?

我可以这样标记我的[DataContract]吗:如果未设置[DataMember]属性,即为null,则根本不应该将它们序列化并发送给客户端?

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?

推荐答案

您可以标记每个数据成员,如果它为null,则不会被序列化.必不可少的部分是:EmitDefaultValue = false

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; }
}

这篇关于在数据协定中未设置数据成员字段时如何不返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 02:49