title: c# Json Serialization Required Fields
body: <p>I am using the inbuilt <code>DataContractJsonSerializer</code> to prepare data for transmission.</p>
  
  <p>I am Serializing from a business object using the following code:</p>
  
  <pre><code>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;
          }
  </code></pre>
  
  <p>When the data is serialized, it seems to be ignoring the settings, the most important setting is IsRequired, as I do not want to transmit a field if it doesnt contain data. Here is how I have the Business Object Defined:</p>
  
  <pre><code> [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; }
          }
  </code></pre>
  
  <p>Here is a sample of the Serialized data that I am getting back from 'SerialiseRequest' Method:</p>
  
  <blockquote>
    <p>{"registration_header":{"account_type":"Single","affiliate_number":"A061535","services_required":"Transfers"},
    "primary_user":{"dob":"23-10-1989","email":"k@gale","first_name":"Kathy","landline_number":"","last_name":"Gale","middle_name":"","mobile_number":"+44-79879879879","nationality":"UK","occupation":"Farmer","residence_country":"UK","source":"Other","title":"Miss"},
    "partner_attributes":{"customer_id":"273098","reps_location":"Stockton-North
    East UK","reps_name":"Richard Gale-551"},
    "primary_residential_address":{"city":"Larkhall","country":"UK","postcode":"ML9
    2LS","street_or_address":"1 The Street"}}</p>
  </blockquote>