title: How to control json_encode behavior?
body: <p><em>Is there any way to control <code>json_encode</code> behavior on objects? Like excluding empty arrays, null fields and so on?</em></p>
  
  <p>I mean something like when using <code>serialize()</code>, where you can implement magic <code>__sleep()</code> method and specify what properties should be serialized:</p>
  
  <pre><code>class MyClass
  {
     public $yes   = "I should be encoded/serialized!";
     public $empty = array(); // // Do not encode me!
     public $null  = null; // Do not encode me!
  
     public function __sleep() { return array('yes'); }
  }
  
  $obj = new MyClass();
  var_dump(json_encode($obj));
  </code></pre>