title: Remove null properties of an object sent to Json MVC
body: <pre class="lang-c# prettyprint-override"><code>namespace Booking.Areas.Golfy.Models
  {
      public class Time
      {
          public string   time            { get; set; }
          public int      holes           { get; set; }
          public int      slots_available { get; set; }
          public decimal? price           { get; set; }
          public int?     Nextcourseid    { get; set; }
  
          public bool ShouldSerializeNextcourseid
          {
              get
              {
                  return this.Nextcourseid != null;
              }
          }
  
          public bool? allow_extra { get; set; }
  
          public bool ShouldSerializeallow_extra
          {
              get
              {
                  return this.allow_extra != null;
              }
          }
      }
  }
  </code></pre>
  
  <p><br/></p>
  
  <pre class="lang-c# prettyprint-override"><code>namespace Booking.Areas.Golfy.Controllers
  {
      public class TeetimesController : Controller
      {
          //
          // GET: /Golfy/Teetimes/
          public ActionResult Index(
              string date,
              int?   courseid = null,
              int?   clubid = null,
              int?   holes = null,
              int?   available = null,
              int?   prices = null
          )
          {
              var DateFrom = DateTime.ParseExact(date, "yyyy-MM-dd", CultureInfo.InvariantCulture);
  
              Teetimes r = BookingManager.GetBookings(new Code.Classes.Params.ParamGetBooking()
              {
                  ClubID = clubid
                  , DateFrom = DateFrom
                  , DateTo = DateFrom.AddDays(1)
                  , GroundID = courseid
              });
  
              return Json(r, JsonRequestBehavior.AllowGet);
          }
      }
  }
  </code></pre>
  
  <p>The webservice above returns a json string with several intance of Time.</p>
  
  <p>I'd like properties Nextcourseid and allow_extra to be left out of the output when their values are null.</p>
  
  <p>I tried ShouldSerializeXxx but it doesn't seems to work.
  <br/>FYI: I also tried [ScriptIgnore] which work but isn't conditional.</p>