mirror of
https://github.com/sigmasternchen/php-doc-en
synced 2025-03-19 10:28:54 +00:00
184 lines
3.9 KiB
XML
184 lines
3.9 KiB
XML
<?xml version="1.0" encoding="utf-8"?>
|
|
<!-- $Revision$ -->
|
|
<refentry xml:id="jsonserializable.jsonserialize" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
|
|
<refnamediv>
|
|
<refname>JsonSerializable::jsonSerialize</refname>
|
|
<refpurpose>Specify data which should be serialized to JSON</refpurpose>
|
|
</refnamediv>
|
|
|
|
<refsect1 role="description">
|
|
&reftitle.description;
|
|
<methodsynopsis>
|
|
<modifier>public</modifier> <type>mixed</type><methodname>JsonSerializable::jsonSerialize</methodname>
|
|
<void/>
|
|
</methodsynopsis>
|
|
<para>
|
|
Serializes the object to a value that can be serialized natively by
|
|
<function>json_encode</function>.
|
|
</para>
|
|
</refsect1>
|
|
|
|
<refsect1 role="parameters">
|
|
&reftitle.parameters;
|
|
&no.function.parameters;
|
|
</refsect1>
|
|
|
|
<refsect1 role="returnvalues">
|
|
&reftitle.returnvalues;
|
|
<para>
|
|
Returns data which can be serialized by <function>json_encode</function>,
|
|
which is a value of any type other than a &resource;.
|
|
</para>
|
|
</refsect1>
|
|
|
|
<refsect1 role="examples">
|
|
&reftitle.examples;
|
|
<para>
|
|
<example>
|
|
<title>
|
|
<methodname>JsonSerializable::jsonSerialize</methodname> example
|
|
returning an &array;
|
|
</title>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
class ArrayValue implements JsonSerializable {
|
|
public function __construct(array $array) {
|
|
$this->array = $array;
|
|
}
|
|
|
|
public function jsonSerialize() {
|
|
return $this->array;
|
|
}
|
|
}
|
|
|
|
$array = [1, 2, 3];
|
|
echo json_encode(new ArrayValue($array), JSON_PRETTY_PRINT);
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
&example.outputs;
|
|
<screen>
|
|
<![CDATA[
|
|
[
|
|
1,
|
|
2,
|
|
3
|
|
]
|
|
]]>
|
|
</screen>
|
|
</example>
|
|
<example>
|
|
<title>
|
|
<methodname>JsonSerializable::jsonSerialize</methodname> example
|
|
returning an associative &array;
|
|
</title>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
class ArrayValue implements JsonSerializable {
|
|
public function __construct(array $array) {
|
|
$this->array = $array;
|
|
}
|
|
|
|
public function jsonSerialize() {
|
|
return $this->array;
|
|
}
|
|
}
|
|
|
|
$array = ['foo' => 'bar', 'quux' => 'baz'];
|
|
echo json_encode(new ArrayValue($array), JSON_PRETTY_PRINT);
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
&example.outputs;
|
|
<screen>
|
|
<![CDATA[
|
|
{
|
|
"foo": "bar",
|
|
"quux": "baz"
|
|
}
|
|
]]>
|
|
</screen>
|
|
</example>
|
|
<example>
|
|
<title>
|
|
<methodname>JsonSerializable::jsonSerialize</methodname> example
|
|
returning an &integer;
|
|
</title>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
class IntegerValue implements JsonSerializable {
|
|
public function __construct($number) {
|
|
$this->number = (integer) $number;
|
|
}
|
|
|
|
public function jsonSerialize() {
|
|
return $this->number;
|
|
}
|
|
}
|
|
|
|
echo json_encode(new IntegerValue(1), JSON_PRETTY_PRINT);
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
&example.outputs;
|
|
<screen>
|
|
<![CDATA[
|
|
1
|
|
]]>
|
|
</screen>
|
|
</example>
|
|
<example>
|
|
<title>
|
|
<methodname>JsonSerializable::jsonSerialize</methodname> example
|
|
returning a &string;
|
|
</title>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
class StringValue implements JsonSerializable {
|
|
public function __construct($string) {
|
|
$this->string = (string) $string;
|
|
}
|
|
|
|
public function jsonSerialize() {
|
|
return $this->string;
|
|
}
|
|
}
|
|
|
|
echo json_encode(new StringValue('Hello!'), JSON_PRETTY_PRINT);
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
&example.outputs;
|
|
<screen>
|
|
<![CDATA[
|
|
"Hello!"
|
|
]]>
|
|
</screen>
|
|
</example>
|
|
</para>
|
|
</refsect1>
|
|
</refentry>
|
|
<!-- Keep this comment at the end of the file
|
|
Local variables:
|
|
mode: sgml
|
|
sgml-omittag:t
|
|
sgml-shorttag:t
|
|
sgml-minimize-attributes:nil
|
|
sgml-always-quote-attributes:t
|
|
sgml-indent-step:1
|
|
sgml-indent-data:t
|
|
indent-tabs-mode:nil
|
|
sgml-parent-document:nil
|
|
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
|
|
sgml-exposed-tags:nil
|
|
sgml-local-catalogs:nil
|
|
sgml-local-ecat-files:nil
|
|
End:
|
|
vim600: syn=xml fen fdm=syntax fdl=2 si
|
|
vim: et tw=78 syn=sgml
|
|
vi: ts=1 sw=1
|
|
-->
|