質問

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