mirror of
https://github.com/php/doc-fr.git
synced 2026-03-24 15:12:13 +01:00
428 lines
12 KiB
XML
428 lines
12 KiB
XML
<?xml version="1.0" encoding="utf-8"?>
|
|
<!-- EN-Revision: 9f2e30a00afda6d6b6a3e19b13956150c2eaf2c1 Maintainer: girgias Status: ready -->
|
|
<!-- Reviewed: no -->
|
|
|
|
<refentry xml:id="function.json-encode" xmlns="http://docbook.org/ns/docbook">
|
|
<refnamediv>
|
|
<refname>json_encode</refname>
|
|
<refpurpose>Retourne la représentation JSON d'une valeur</refpurpose>
|
|
</refnamediv>
|
|
|
|
<refsect1 role="description">
|
|
&reftitle.description;
|
|
<methodsynopsis>
|
|
<type class="union"><type>string</type><type>false</type></type><methodname>json_encode</methodname>
|
|
<methodparam><type>mixed</type><parameter>value</parameter></methodparam>
|
|
<methodparam choice="opt"><type>int</type><parameter>flags</parameter><initializer>0</initializer></methodparam>
|
|
<methodparam choice="opt"><type>int</type><parameter>depth</parameter><initializer>512</initializer></methodparam>
|
|
</methodsynopsis>
|
|
<para>
|
|
Retourne une chaîne de caractères contenant la représentation JSON de la valeur
|
|
<parameter>value</parameter>. Si le paramètre est un &array; ou un &object;, il
|
|
sera sérialisé de manière récursive.
|
|
</para>
|
|
<para>
|
|
Si l'une des valeurs à sérialiser est un objet, alors par défaut seules les propriétés
|
|
visibles publiquement seront incluses. Une classe peut également implémenter
|
|
<interfacename>JsonSerializable</interfacename> pour contrôler la façon dont ses valeurs
|
|
sont sérialisées en <acronym>JSON</acronym>.
|
|
</para>
|
|
<para>
|
|
L'encodage est affecté par les <parameter>flags</parameter> fournis.
|
|
De plus, l'encodage des valeurs flottantes dépend de la valeur de
|
|
<link linkend="ini.serialize-precision">serialize_precision</link>.
|
|
</para>
|
|
</refsect1>
|
|
|
|
<refsect1 role="parameters">
|
|
&reftitle.parameters;
|
|
<para>
|
|
<variablelist>
|
|
<varlistentry>
|
|
<term><parameter>value</parameter></term>
|
|
<listitem>
|
|
<para>
|
|
La valeur à encoder. Peut être de n'importe quel type, excepté
|
|
une &resource;.
|
|
</para>
|
|
<para>
|
|
Toutes les chaînes doivent être encodées UTF-8.
|
|
</para>
|
|
&json.implementation.superset;
|
|
</listitem>
|
|
</varlistentry>
|
|
<varlistentry>
|
|
<term><parameter>flags</parameter></term>
|
|
<listitem>
|
|
<para>
|
|
Masque binaire composé de
|
|
<constant>JSON_FORCE_OBJECT</constant>,
|
|
<constant>JSON_HEX_QUOT</constant>,
|
|
<constant>JSON_HEX_TAG</constant>,
|
|
<constant>JSON_HEX_AMP</constant>,
|
|
<constant>JSON_HEX_APOS</constant>,
|
|
<constant>JSON_INVALID_UTF8_IGNORE</constant>,
|
|
<constant>JSON_INVALID_UTF8_SUBSTITUTE</constant>,
|
|
<constant>JSON_NUMERIC_CHECK</constant>,
|
|
<constant>JSON_PARTIAL_OUTPUT_ON_ERROR</constant>,
|
|
<constant>JSON_PRESERVE_ZERO_FRACTION</constant>,
|
|
<constant>JSON_PRETTY_PRINT</constant>,
|
|
<constant>JSON_UNESCAPED_LINE_TERMINATORS</constant>,
|
|
<constant>JSON_UNESCAPED_SLASHES</constant>,
|
|
<constant>JSON_UNESCAPED_UNICODE</constant>,
|
|
<constant>JSON_THROW_ON_ERROR</constant>.
|
|
Le comportement de ces constantes est décrit sur la page
|
|
des <link linkend="json.constants">constantes JSON</link>.
|
|
</para>
|
|
</listitem>
|
|
</varlistentry>
|
|
<varlistentry>
|
|
<term><parameter>depth</parameter></term>
|
|
<listitem>
|
|
<para>
|
|
Définit la profondeur maximale. Doit être supérieure à zéro.
|
|
</para>
|
|
</listitem>
|
|
</varlistentry>
|
|
</variablelist>
|
|
</para>
|
|
</refsect1>
|
|
|
|
<refsect1 role="returnvalues">
|
|
&reftitle.returnvalues;
|
|
<para>
|
|
Retourne un JSON encodé en tant que &string; en cas de succès &return.falseforfailure;.
|
|
</para>
|
|
</refsect1>
|
|
|
|
<refsect1 role="changelog">
|
|
&reftitle.changelog;
|
|
<para>
|
|
<informaltable>
|
|
<tgroup cols="2">
|
|
<thead>
|
|
<row>
|
|
<entry>&Version;</entry>
|
|
<entry>&Description;</entry>
|
|
</row>
|
|
</thead>
|
|
<tbody>
|
|
<row>
|
|
<entry>7.3.0</entry>
|
|
<entry>
|
|
Le <parameter>flags</parameter>
|
|
<constant>JSON_THROW_ON_ERROR</constant> a été ajouté.
|
|
</entry>
|
|
</row>
|
|
<row>
|
|
<entry>7.2.0</entry>
|
|
<entry>
|
|
Les <parameter>flags</parameter>
|
|
<constant>JSON_INVALID_UTF8_IGNORE</constant>, et
|
|
<constant>JSON_INVALID_UTF8_SUBSTITUTE</constant> ont été ajoutés.
|
|
</entry>
|
|
</row>
|
|
<row>
|
|
<entry>7.1.0</entry>
|
|
<entry>
|
|
Le <parameter>flags</parameter>
|
|
<constant>JSON_UNESCAPED_LINE_TERMINATORS</constant> a été ajouté.
|
|
</entry>
|
|
</row>
|
|
<row>
|
|
<entry>7.1.0</entry>
|
|
<entry>
|
|
<link linkend="ini.serialize-precision">serialize_precision</link> est
|
|
utilisé à la place de <link linkend="ini.precision">precision</link>
|
|
lors de l'encodage de valeurs <type>float</type>.
|
|
</entry>
|
|
</row>
|
|
</tbody>
|
|
</tgroup>
|
|
</informaltable>
|
|
</para>
|
|
</refsect1>
|
|
|
|
<refsect1 role="examples">
|
|
&reftitle.examples;
|
|
<para>
|
|
<example>
|
|
<title>Exemple avec <function>json_encode</function></title>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
$arr = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);
|
|
|
|
echo json_encode($arr);
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
&example.outputs;
|
|
<screen>
|
|
<![CDATA[
|
|
{"a":1,"b":2,"c":3,"d":4,"e":5}
|
|
]]>
|
|
</screen>
|
|
</example>
|
|
<example>
|
|
<title>Exemple avec <function>json_encode</function> montrant quelques drapeaux en action</title>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
$a = array('<foo>',"'bar'",'"baz"','&blong&', "\xc3\xa9");
|
|
|
|
echo "Normal : ", json_encode($a), "\n";
|
|
echo "Tags : ", json_encode($a, JSON_HEX_TAG), "\n";
|
|
echo "Apos : ", json_encode($a, JSON_HEX_APOS), "\n";
|
|
echo "Quot : ", json_encode($a, JSON_HEX_QUOT), "\n";
|
|
echo "Amp : ", json_encode($a, JSON_HEX_AMP), "\n";
|
|
echo "Unicode : ", json_encode($a, JSON_UNESCAPED_UNICODE), "\n";
|
|
echo "Toutes : ", json_encode($a, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP | JSON_UNESCAPED_UNICODE), "\n\n";
|
|
|
|
$b = array();
|
|
|
|
echo "Tableau vide sous forme de tableau : ", json_encode($b), "\n";
|
|
echo "Tableau vide sous forme d'objet : ", json_encode($b, JSON_FORCE_OBJECT), "\n\n";
|
|
|
|
$c = array(array(1,2,3));
|
|
|
|
echo "Tableau non-associatif sous forme de tableau : ", json_encode($c), "\n";
|
|
echo "Tableau non-associatif sous forme d'objet : ", json_encode($c, JSON_FORCE_OBJECT), "\n\n";
|
|
|
|
|
|
$d = array('foo' => 'bar', 'baz' => 'long');
|
|
|
|
echo "Tableau associatif affiché comme objet: ", json_encode($d), "\n";
|
|
echo "Tableau associatif affiché comme objet: ", json_encode($d, JSON_FORCE_OBJECT), "\n\n";
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
&example.outputs;
|
|
<screen>
|
|
<![CDATA[
|
|
Normal : ["<foo>","'bar'","\"baz\"","&blong&","\u00e9"]
|
|
Tags : ["\u003Cfoo\u003E","'bar'","\"baz\"","&blong&","\u00e9"]
|
|
Apos : ["<foo>","\u0027bar\u0027","\"baz\"","&blong&","\u00e9"]
|
|
Quot : ["<foo>","'bar'","\u0022baz\u0022","&blong&","\u00e9"]
|
|
Amp : ["<foo>","'bar'","\"baz\"","\u0026blong\u0026","\u00e9"]
|
|
Unicode : ["<foo>","'bar'","\"baz\"","&blong&","é"]
|
|
Toutes : ["\u003Cfoo\u003E","\u0027bar\u0027","\u0022baz\u0022","\u0026blong\u0026","é"]
|
|
|
|
Tableau vide sous forme de tableau : []
|
|
Tableau vide sous forme d'objet : {}
|
|
|
|
Tableau non-associatif sous forme de tableau : [[1,2,3]]
|
|
Tableau non-associatif sous forme d'objet : {"0":{"0":1,"1":2,"2":3}}
|
|
|
|
Tableau associatif affiché comme objet: {"foo":"bar","baz":"long"}
|
|
Tableau associatif affiché comme objet: {"foo":"bar","baz":"long"}
|
|
]]>
|
|
</screen>
|
|
</example>
|
|
<example>
|
|
<title>Exemple avec l'option JSON_NUMERIC_CHECK</title>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
echo "Les chaînes représentant des nombres sont automatiquement converties en nombres".PHP_EOL;
|
|
$numbers = array('+123123', '-123123', '1.2e3', '0.00001');
|
|
var_dump(
|
|
$numbers,
|
|
json_encode($numbers, JSON_NUMERIC_CHECK)
|
|
);
|
|
echo "Chaînes contenant des nombres mal formatés".PHP_EOL;
|
|
$strings = array('+a33123456789', 'a123');
|
|
var_dump(
|
|
$strings,
|
|
json_encode($strings, JSON_NUMERIC_CHECK)
|
|
);
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
&example.outputs.similar;
|
|
<screen>
|
|
<![CDATA[
|
|
Les chaînes représentant des nombres sont automatiquement converties en nombres
|
|
array(4) {
|
|
[0]=>
|
|
string(7) "+123123"
|
|
[1]=>
|
|
string(7) "-123123"
|
|
[2]=>
|
|
string(5) "1.2e3"
|
|
[3]=>
|
|
string(7) "0.00001"
|
|
}
|
|
string(28) "[123123,-123123,1200,1.0e-5]"
|
|
Chaînes contenant des nombres non formattés proprement
|
|
array(2) {
|
|
[0]=>
|
|
string(13) "+a33123456789"
|
|
[1]=>
|
|
string(4) "a123"
|
|
}
|
|
string(24) "["+a33123456789","a123"]"
|
|
]]>
|
|
</screen>
|
|
</example>
|
|
<example>
|
|
<title>Exemple avec un tableau séquentiel et un tableau non-séquentiel</title>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
echo "Tableau séquentiel".PHP_EOL;
|
|
$sequential = array("foo", "bar", "baz", "blong");
|
|
var_dump(
|
|
$sequential,
|
|
json_encode($sequential)
|
|
);
|
|
|
|
echo PHP_EOL."Tableau non-séquentiel".PHP_EOL;
|
|
$nonsequential = array(1=>"foo", 2=>"bar", 3=>"baz", 4=>"blong");
|
|
var_dump(
|
|
$nonsequential,
|
|
json_encode($nonsequential)
|
|
);
|
|
|
|
echo PHP_EOL."Tableau séquentiel avec une clé supprimée".PHP_EOL;
|
|
unset($sequential[1]);
|
|
var_dump(
|
|
$sequential,
|
|
json_encode($sequential)
|
|
);
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
&example.outputs;
|
|
<screen>
|
|
<![CDATA[
|
|
Tableau séquentiel
|
|
array(4) {
|
|
[0]=>
|
|
string(3) "foo"
|
|
[1]=>
|
|
string(3) "bar"
|
|
[2]=>
|
|
string(3) "baz"
|
|
[3]=>
|
|
string(5) "blong"
|
|
}
|
|
string(27) "["foo","bar","baz","blong"]"
|
|
|
|
Tableau non-séquentiel
|
|
array(4) {
|
|
[1]=>
|
|
string(3) "foo"
|
|
[2]=>
|
|
string(3) "bar"
|
|
[3]=>
|
|
string(3) "baz"
|
|
[4]=>
|
|
string(5) "blong"
|
|
}
|
|
string(43) "{"1":"foo","2":"bar","3":"baz","4":"blong"}"
|
|
|
|
Tableau séquentiel avec une clé supprimée
|
|
array(3) {
|
|
[0]=>
|
|
string(3) "foo"
|
|
[2]=>
|
|
string(3) "baz"
|
|
[3]=>
|
|
string(5) "blong"
|
|
}
|
|
string(33) "{"0":"foo","2":"baz","3":"blong"}"
|
|
]]>
|
|
</screen>
|
|
</example>
|
|
<example>
|
|
<title>Exemple avec l'option <constant>JSON_PRESERVE_ZERO_FRACTION</constant></title>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
var_dump(json_encode(12.0, JSON_PRESERVE_ZERO_FRACTION));
|
|
var_dump(json_encode(12.0));
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
&example.outputs;
|
|
<screen>
|
|
<![CDATA[
|
|
string(4) "12.0"
|
|
string(2) "12"
|
|
]]>
|
|
</screen>
|
|
</example>
|
|
</para>
|
|
</refsect1>
|
|
|
|
<refsect1 role="notes">
|
|
&reftitle.notes;
|
|
<note>
|
|
<para>
|
|
Lorsqu'une erreur survient lors de l'encodage, la fonction
|
|
<function>json_last_error</function> peut être utilisée pour déterminer
|
|
la nature exacte de l'erreur.
|
|
</para>
|
|
</note>
|
|
<note>
|
|
<para>
|
|
Lors de l'encodage d'un tableau, si les clés ne sont pas sous la
|
|
forme d'une séquence numérique continue commençant à 0, toutes
|
|
les clés seront encodées sous forme de chaînes de caractères,
|
|
et spécifiées explicitement pour chaque paire clé-valeur.
|
|
</para>
|
|
</note>
|
|
<note>
|
|
<para>
|
|
Tout comme l'encodeur JSON de référence, la fonction
|
|
<function>json_encode</function> génèrera un JSON qui est une valeur simple
|
|
(ni un objet, ni un tableau) si une &string;, un &integer;, un
|
|
&float;, ou un &boolean; est fourni en entrée pour le paramètre
|
|
<parameter>value</parameter>. Bien que certains décodeurs
|
|
acceptent ces valeurs comme JSON valide, d'autres ne les acceptent pas,
|
|
sachant que la spécification est ambiguë sur ce point.
|
|
</para>
|
|
<para>
|
|
Pour résumer, testez toujours que le décodeur JSON peut gérer
|
|
la sortie qu'on génère depuis la fonction
|
|
<function>json_encode</function>.
|
|
</para>
|
|
</note>
|
|
</refsect1>
|
|
|
|
<refsect1 role="seealso">
|
|
&reftitle.seealso;
|
|
<para>
|
|
<simplelist>
|
|
<member><interfacename>JsonSerializable</interfacename></member>
|
|
<member><function>json_decode</function></member>
|
|
<member><function>json_last_error</function></member>
|
|
<member><function>json_last_error_msg</function></member>
|
|
<member><function>serialize</function></member>
|
|
</simplelist>
|
|
</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
|
|
-->
|