UnionOfRAD / lithium

li₃ is the fast, flexible and most RAD development framework for PHP
http://li3.me
BSD 3-Clause "New" or "Revised" License
1.22k stars 238 forks source link

Media::encode() causes $data objects that implement JsonSerializer to json_encode incorrectly. #1154

Open adallaway opened 9 years ago

adallaway commented 9 years ago

Media::encode() casts objects in the view data to arrays. This prevents jsonSerialize() from being called on objects that implement JsonSerializable.

I'm not sure if this is intended behavior but here's a possible fix: https://github.com/UnionOfRAD/lithium/blob/master/net/http/Media.php#L766

if ($data instanceof \JsonSerializable)) {
  return $data;
}

As a workaround, a dummy to() method can be added to the objects:

public function to()
{
  return $this;
}
mariuswilms commented 9 years ago

Until we find a clean solution for this, try reregistering the json handler with 'cast' set to false. That only works if you always hand JsonSerializable objects to the view.