From 3342a471af68ac9382a4816a0e79ac3247b3d925 Mon Sep 17 00:00:00 2001 From: Adam Harvey Date: Fri, 3 Aug 2012 08:45:49 +0000 Subject: [PATCH] Add JsonSerializable::jsonSerialize() examples. git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@326957 c90b9560-bf6c-de11-be94-00142212c4b1 --- .../json/jsonserializable/jsonserialize.xml | 138 +++++++++++++++++- 1 file changed, 133 insertions(+), 5 deletions(-) diff --git a/reference/json/jsonserializable/jsonserialize.xml b/reference/json/jsonserializable/jsonserialize.xml index f735fb14a4..39a5ded7ba 100644 --- a/reference/json/jsonserializable/jsonserialize.xml +++ b/reference/json/jsonserializable/jsonserialize.xml @@ -14,11 +14,9 @@ - + Serializes the object to a value that can be serialized natively by + json_encode. - - &warn.undocumented.func; - @@ -29,11 +27,141 @@ &reftitle.returnvalues; - Return data which should be serialized by json_encode. + Returns data which can be serialized by json_encode, + which is a value of any type other than a resource. + + &reftitle.examples; + + + + <methodname>JsonSerializable::jsonSerialize</methodname> example + returning an <type>array</type> + + +array = $array; + } + public function jsonSerialize() { + return $this->array; + } +} + +$array = [1, 2, 3]; +echo json_encode(new ArrayValue($array), JSON_PRETTY_PRINT); +?> +]]> + + &example.outputs; + + + + + + + <methodname>JsonSerializable::jsonSerialize</methodname> example + returning an associative <type>array</type> + + +array = $array; + } + + public function jsonSerialize() { + return $this->array; + } +} + +$array = ['foo' => 'bar', 'quux' => 'baz']; +echo json_encode(new ArrayValue($array), JSON_PRETTY_PRINT); +?> +]]> + + &example.outputs; + + + + + + + <methodname>JsonSerializable::jsonSerialize</methodname> example + returning an <type>integer</type> + + +number = (integer) $number; + } + + public function jsonSerialize() { + return $this->number; + } +} + +echo json_encode(new IntegerValue(1), JSON_PRETTY_PRINT); +?> +]]> + + &example.outputs; + + + + + + + <methodname>JsonSerializable::jsonSerialize</methodname> example + returning an <type>integer</type> + + +string = (string) $string; + } + + public function jsonSerialize() { + return $this->string; + } +} + +echo json_encode(new StringValue('Hello!'), JSON_PRETTY_PRINT); +?> +]]> + + &example.outputs; + + + + + +