mirror of
https://github.com/php/doc-es.git
synced 2026-03-26 00:12:06 +01:00
git-svn-id: https://svn.php.net/repository/phpdoc/es/trunk@327362 c90b9560-bf6c-de11-be94-00142212c4b1
189 lines
4.2 KiB
XML
189 lines
4.2 KiB
XML
<?xml version="1.0" encoding="utf-8"?>
|
|
<!-- $Revision$ -->
|
|
<!-- EN-Revision: 589ebcb6d54e699c3eed82b1f72c9d70a83dcfcd Maintainer: seros Status: ready -->
|
|
<!-- Reviewed: no Maintainer: seros -->
|
|
|
|
<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>Especifica los datos que deberían serializarse para JSON</refpurpose>
|
|
</refnamediv>
|
|
|
|
<refsect1 role="description">
|
|
&reftitle.description;
|
|
<methodsynopsis>
|
|
<modifier>abstract</modifier> <modifier>public</modifier> <type>mixed</type><methodname>JsonSerializable::jsonSerialize</methodname>
|
|
<void />
|
|
</methodsynopsis>
|
|
<para>
|
|
Serializa el objeto a un valor que puede ser serializado de forma nativa por
|
|
<function>json_encode</function>.
|
|
</para>
|
|
</refsect1>
|
|
|
|
<refsect1 role="parameters">
|
|
&reftitle.parameters;
|
|
&no.function.parameters;
|
|
</refsect1>
|
|
|
|
<refsect1 role="returnvalues">
|
|
&reftitle.returnvalues;
|
|
<para>
|
|
Devuelve los datos que pueden ser serializados por <function>json_encode</function>,
|
|
los cuales son un valor de cualquier tipo distinto de <type>resource</type>.
|
|
</para>
|
|
</refsect1>
|
|
|
|
<refsect1 role="examples">
|
|
&reftitle.examples;
|
|
<para>
|
|
<example>
|
|
<title>
|
|
Ejemplo de <methodname>JsonSerializable::jsonSerialize</methodname>
|
|
devolviendo un <type>array</type>
|
|
</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>
|
|
Ejemplo de <methodname>JsonSerializable::jsonSerialize</methodname>
|
|
devolviendo un <type>array</type> asociativo
|
|
</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>
|
|
Ejemplo de <methodname>JsonSerializable::jsonSerialize</methodname>
|
|
devolviento un <type>integer</type>
|
|
</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>
|
|
Ejemplo de <methodname>JsonSerializable::jsonSerialize</methodname>
|
|
devolviendo un <type>string</type>
|
|
</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('¡Hola!'), JSON_PRETTY_PRINT);
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
&example.outputs;
|
|
<screen>
|
|
<![CDATA[
|
|
"¡Hola!"
|
|
]]>
|
|
</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
|
|
-->
|