c#Json序列化必填字段

时间:2017-07-26 11:44:39

标签: c# api serialization

我正在使用内置DataContractJsonSerializer来准备传输数据。

我使用以下代码从业务对象进行序列化:

private string SerialiseRequest(RegistrationRequest request)
        {
            string strRequest = string.Empty;

            try
            {
                DataContractJsonSerializer serializer = new DataContractJsonSerializer(request.GetType());

                using (MemoryStream ms = new MemoryStream())
                {
                    serializer.WriteObject(ms, request);
                    strRequest = Encoding.Default.GetString(ms.ToArray());
                }
            }
            catch
            {
                return string.Empty;
            }

            return strRequest;
        }

当数据被序列化时,它似乎忽略了设置,最重要的设置是IsRequired,因为如果它不包含数据我不想传输字段。以下是我如何定义业务对象:

 [DataContract]
    public sealed class RegistrationRequest
    {
        [DataMember(Order = 0, IsRequired = true)]
        public Registration_Header registration_header { get; set; }

        [DataMember(Order = 1, IsRequired = true)]
        public Customer_Details primary_user { get; set; }

        [DataMember(Order = 2, IsRequired = true)]
        public StaffDetails partner_attributes { get; set; }

        [DataMember(Order = 3, IsRequired = true)]
        public CustomerAddress primary_residential_address { get; set; }      

        public class Registration_Header
        {
            [DataMember(Order = 0, IsRequired = true)]
            public string services_required { get; set; }

            [DataMember(Order = 1, IsRequired = true)]
            public string account_type { get; set; }

            [DataMember(Order = 2, IsRequired = true)]
            public string affiliate_number { get; set; }
        }

        public class Customer_Details
        {
            [DataMember(IsRequired = true)]
            public string title { get; set; }

            [DataMember(IsRequired = true)]
            public string first_name { get; set; }

            [DataMember(IsRequired = false)]
            public string middle_name { get; set; }

            [DataMember(IsRequired = true)]
            public string last_name { get; set; }

            [DataMember(IsRequired = true)]
            public string email { get; set; }

            [DataMember(IsRequired = true)]
            public string nationality { get; set; }

            [DataMember(IsRequired = true)]
            public string dob { get; set; }

            [DataMember(IsRequired = true)]
            public string occupation { get; set; }

            [DataMember(IsRequired = true)]
            public string residence_country { get; set; }

            [DataMember(IsRequired = true)]
            public string mobile_number { get; set; }

            [DataMember(IsRequired = false)]
            public string landline_number { get; set; }

            [DataMember(IsRequired = true)]
            public string source { get; set; }
        }

        public class StaffDetails
        {
            [DataMember(IsRequired = true)]
            public string reps_name { get; set; }

            [DataMember(IsRequired = true)]
            public string reps_location { get; set; }

            [DataMember(IsRequired = true)]
            public string customer_id { get; set; }
        }

        public class CustomerAddress
        {
            [DataMember(IsRequired = true)]
            public string postcode { get; set; }

            [DataMember(IsRequired = true)]
            public string street_or_address { get; set; }

            [DataMember(Name = "city", IsRequired = true)]
            public string city { get; set; }

            [DataMember(Name = "country", IsRequired = true)]
            public string country { get; set; }
        }

以下是我从'SerialiseRequest'获取的序列化数据示例方法:

  

{ “registration_header”:{ “ACCOUNT_TYPE”: “单”, “affiliate_number”: “A061535”, “services_required”: “转会”},   “primary_user”:{ “出生日期”: “23-10-1989”, “电子邮件”: “K @大风”, “FIRST_NAME”: “凯西”, “landline_number”: “”, “姓氏”: “大风”, “middle_name”: “”, “MOBILE_NUMBER”: “+ 44-79879879879”, “国籍”: “英国”, “职业”: “农民”, “residence_country”: “英国”, “源”: “其他”, “头衔”: “小姐”},   “partner_attributes”:{ “CUSTOMER_ID”: “273098”, “reps_location”:“斯托克顿北   东英国“,”reps_name“:”Richard Gale-551“},   “primary_residential_address”:{ “城市”: “拉克霍尔”, “国”: “英国”, “邮政编码”:“ML9   2LS“,”street_or_address“:”1 The Street“}}

2 个答案:

答案 0 :(得分:1)

您可以使用[DataMember(EmitDefaultValue = false)] attribute

[DataContract]
public sealed class RegistrationRequest
{
    [DataMember(Order = 0, IsRequired = true)]
    public Registration_Header registration_header { get; set; }

    [DataMember(Order = 1, IsRequired = true)]
    public Customer_Details primary_user { get; set; }

    [DataMember(Order = 2, IsRequired = true)]
    public StaffDetails partner_attributes { get; set; }

    [DataMember(Order = 3, IsRequired = true)]
    public CustomerAddress primary_residential_address { get; set; }

    public class Registration_Header
    {
        [DataMember(Order = 0, IsRequired = true)]
        public string services_required { get; set; }

        [DataMember(Order = 1, IsRequired = true)]
        public string account_type { get; set; }

        [DataMember(Order = 2, IsRequired = true)]
        public string affiliate_number { get; set; }
    }

    [DataContract]
    public class Customer_Details
    {
        [DataMember(IsRequired = true)]
        public string title { get; set; }

        [DataMember(IsRequired = true)]
        public string first_name { get; set; }

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

        [DataMember(IsRequired = true)]
        public string last_name { get; set; }

        [DataMember(IsRequired = true)]
        public string email { get; set; }

        [DataMember(IsRequired = true)]
        public string nationality { get; set; }

        [DataMember(IsRequired = true)]
        public string dob { get; set; }

        [DataMember(IsRequired = true)]
        public string occupation { get; set; }

        [DataMember(IsRequired = true)]
        public string residence_country { get; set; }

        [DataMember(IsRequired = true)]
        public string mobile_number { get; set; }

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

        [DataMember(IsRequired = true)]
        public string source { get; set; }
    }

    public class StaffDetails
    {
        [DataMember(IsRequired = true)]
        public string reps_name { get; set; }

        [DataMember(IsRequired = true)]
        public string reps_location { get; set; }

        [DataMember(IsRequired = true)]
        public string customer_id { get; set; }
    }

    public class CustomerAddress
    {
        [DataMember(IsRequired = true)]
        public string postcode { get; set; }

        [DataMember(IsRequired = true)]
        public string street_or_address { get; set; }

        [DataMember(Name = "city", IsRequired = true)]
        public string city { get; set; }

        [DataMember(Name = "country", IsRequired = true)]
        public string country { get; set; }
    }
}

另请注意,您必须使用Customer_Details修饰[DataContract]课程才能生效。

答案 1 :(得分:0)

Serializer仅用于序列化对象中的数据。它不会在课堂上装饰。将DataContract发送回服务时,将触发IsRequired属性。