سؤال

Is it possible to send/receive nullable objects over a JSON WCF Service, how does it look like when sent? And what about sending/receiving Enumerations?

For example, we have an object underneath that is sent to the user ui through JSON webservice

  public enum GenderTypes
  {
      Male = 0,
      Female = 1
  }
  public class Human { 
      public int? Age { get; set; }
      public GenderTypes Gender {get; set;}
  }
هل كانت مفيدة؟

المحلول

Yes it is possible to send/receive nullable objects over a JSON WCF Service.

If your object has nullable type property and if you want that client which is calling the service send its value as null then you can simply don't pass the value of that property in the request body and after that deserialize your object.

Now Your object will have the value of that property as null.

For example: your request body would look like this (content type json)

{ 
  "Age" : 12,
  "Gender":"M"
}

When you deserialize this request into your object, your object will have obj.Age=12 and obj.Gender="M", now if you request is similar like this

{ 
  "Gender":"M"
}

then your object will have obj.Age=null and obj.Gender="M".

(Note: I am assuming here Gender as String data type)

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top