如何在数据协定中未设置数据成员字段时不返回 null [英] how to not return null when a Data member field is not set in the data contract

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

问题描述

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

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

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

有点像……

<预><代码>[{"firstname":"john","lastname":"Goodman","city":"NY"},{"firstname":"brad","lastname":"newman","city":"LA"}]

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

<预><代码>[{"id":"1234","city":"NY"},{"id":"1235","city":"LA"}]

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

我的运营合同是这样的

[操作契约][WebGet(BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json, UriTemplate = "Customers?fields={value}")]列出<客户>GetCustomers(字符串值);

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

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

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

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

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

解决方案

您可以标记每个数据成员,以便在它为空时不会被序列化.基本部分是:EmitDefaultValue = false

[数据契约]公开课人{[数据成员(EmitDefaultValue = false)]公共字符串 ID { 获取;放;}[数据成员(EmitDefaultValue = false)]公共字符串名字{获取;放;}[数据成员(EmitDefaultValue = false)]公共字符串姓氏 { 获取;放;}[数据成员(EmitDefaultValue = false)]公共字符串城市{获取;放;}}

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?

解决方案

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆