note 114688 added to jsonserializable.jsonserialize

From: Date: Sat, 22 Mar 2014 11:05:19 +0000
Subject: note 114688 added to jsonserializable.jsonserialize
Groups: php.notes 
Request: Send a blank email to php-notes+get-199013@lists.php.net to get a copy of this message
You can't throw exceptions in here. If you do, you'll get an exception with the message "Failed calling FooClass::jsonSerialize()" and the stacktrace will start at where you called the json_encode() method. Here's a code-example: class Foo implements JsonSerializable { private $triggerError = true; public function jsonSerialize() { if ($this->triggerError) { throw new RuntimeException("It failed!"); } return (array)$this; } } // Will throw an exception like new Exception("Failed calling Foo::jsonSerialize()") json_encode(new Foo()); ---- Server IP: 72.52.91.14 Probable Submitter: 93.200.227.155 ---- Manual Page -- http://php.net/manual/en/jsonserializable.jsonserialize.php Edit -- https://master.php.net/note/edit/114688 Del: integrated -- https://master.php.net/note/delete/114688/integrated Del: useless -- https://master.php.net/note/delete/114688/useless Del: bad code -- https://master.php.net/note/delete/114688/bad+code Del: spam -- https://master.php.net/note/delete/114688/spam Del: non-english -- https://master.php.net/note/delete/114688/non-english Del: in docs -- https://master.php.net/note/delete/114688/in+docs Del: other reasons-- https://master.php.net/note/delete/114688 Reject -- https://master.php.net/note/reject/114688 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#199013) next »