Files
doc-fr/reference/json/functions/json-last-error.xml
George Peter Banyard 207d6996ac Remove PHP 5 mentions
And other small drive-by fixes
2021-05-20 18:11:37 +01:00

253 lines
6.8 KiB
XML

<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<!-- EN-Revision: 8cdc6621f9826d04abc3e50438c010804d7e8683 Maintainer: girgias Status: ready -->
<!-- Reviewed: no -->
<refentry xml:id="function.json-last-error" xmlns="http://docbook.org/ns/docbook">
<refnamediv>
<refname>json_last_error</refname>
<refpurpose>Retourne la dernière erreur JSON</refpurpose>
</refnamediv>
<refsect1 role="description">
&reftitle.description;
<methodsynopsis>
<type>int</type><methodname>json_last_error</methodname>
<void />
</methodsynopsis>
<para>
Retourne la dernière erreur, s'il y en a eu, survenue lors de la dernière
opération d'encodage/décodage JSON, qui n'a pas spécifié
<constant>JSON_THROW_ON_ERROR</constant>.
</para>
</refsect1>
<refsect1 role="parameters">
&reftitle.parameters;
&no.function.parameters;
</refsect1>
<refsect1 role="returnvalues">
&reftitle.returnvalues;
<para>
Retourne une des constantes suivantes :
</para>
<table>
<title>Codes d'erreur JSON</title>
<tgroup cols="2">
<thead>
<row>
<entry>Constante</entry>
<entry>Signification</entry>
<entry>Disponibilité</entry>
</row>
</thead>
<tbody>
<row>
<entry><constant>JSON_ERROR_NONE</constant></entry>
<entry>Aucune erreur n'est survenue</entry>
<entry></entry>
</row>
<row>
<entry><constant>JSON_ERROR_DEPTH</constant></entry>
<entry>La profondeur maximale de la pile a été atteinte</entry>
<entry></entry>
</row>
<row>
<entry><constant>JSON_ERROR_STATE_MISMATCH</constant></entry>
<entry>JSON invalide ou mal formé</entry>
<entry></entry>
</row>
<row>
<entry><constant>JSON_ERROR_CTRL_CHAR</constant></entry>
<entry>Erreur lors du contrôle des caractères ; probablement un encodage incorrect</entry>
<entry></entry>
</row>
<row>
<entry><constant>JSON_ERROR_SYNTAX</constant></entry>
<entry>Erreur de syntaxe</entry>
<entry></entry>
</row>
<row>
<entry><constant>JSON_ERROR_UTF8</constant></entry>
<entry>Caractères UTF-8 malformés, possiblement mal encodés</entry>
<entry></entry>
</row>
<row>
<entry><constant>JSON_ERROR_RECURSION</constant></entry>
<entry>Une ou plusieurs références récursives sont présentes dans
la valeur à encoder</entry>
<entry></entry>
</row>
<row>
<entry><constant>JSON_ERROR_INF_OR_NAN</constant></entry>
<entry>
Une ou plusieurs valeurs
<link linkend="language.types.float.nan"><constant>NAN</constant></link>
ou <link linkend="function.is-infinite"><constant>INF</constant></link>
sont présentes dans la valeurs à encoder.
</entry>
<entry></entry>
</row>
<row>
<entry><constant>JSON_ERROR_UNSUPPORTED_TYPE</constant></entry>
<entry>Une valeur d'un type qui ne peut être encodée a été fournie</entry>
<entry></entry>
</row>
<row>
<entry><constant>JSON_ERROR_INVALID_PROPERTY_NAME</constant></entry>
<entry>Un nom de propriété qui ne peut pas être encodé a été donné</entry>
<entry></entry>
</row>
<row>
<entry><constant>JSON_ERROR_UTF16</constant></entry>
<entry>Caractères UTF-16 mal formés, probablement mal encodé</entry>
<entry></entry>
</row>
</tbody>
</tgroup>
</table>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<para>
<example>
<title>Exemple avec <function>json_last_error</function></title>
<programlisting role="php">
<![CDATA[
<?php
// Une chaîne JSON valide
$json[] = '{"Organisation": "Équipe de Documentation PHP"}';
// Une chaîne json invalide qui va générer une erreur de syntaxe,
// ici, utilisation de ' au lieu de "
$json[] = "{'Organisation': 'Équipe de Documentation PHP'}";
foreach ($json as $string) {
echo 'Décodage : ' . $string;
json_decode($string);
switch (json_last_error()) {
case JSON_ERROR_NONE:
echo ' - Aucune erreur';
break;
case JSON_ERROR_DEPTH:
echo ' - Profondeur maximale atteinte';
break;
case JSON_ERROR_STATE_MISMATCH:
echo ' - Inadéquation des modes ou underflow';
break;
case JSON_ERROR_CTRL_CHAR:
echo ' - Erreur lors du contrôle des caractères';
break;
case JSON_ERROR_SYNTAX:
echo ' - Erreur de syntaxe ; JSON malformé';
break;
case JSON_ERROR_UTF8:
echo ' - Caractères UTF-8 malformés, probablement une erreur d\'encodage';
break;
default:
echo ' - Erreur inconnue';
break;
}
echo PHP_EOL;
}
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
Décodage : {"Organisation": "Équipe de Documentation PHP"} - Aucune erreur
Décodage : {'Organisation': 'Équipe de Documentation PHP'} - Erreur de syntaxe ; JSON malformé
]]>
</screen>
</example>
</para>
<para>
<example>
<title><function>json_last_error</function> avec <function>json_encode</function></title>
<programlisting role="php">
<![CDATA[
<?php
// Une séquence UTF8 invalide
$text = "\xB1\x31";
$json = json_encode($text);
$error = json_last_error();
var_dump($json, $error === JSON_ERROR_UTF8);
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
string(4) "null"
bool(true)
]]>
</screen>
</example>
</para>
<para>
<example>
<title><function>json_last_error</function> et <constant>JSON_THROW_ON_ERROR</constant></title>
<programlisting role="php">
<![CDATA[
<?php
// Une séquence UTF8 invalide qui cause JSON_ERROR_UTF8
json_encode("\xB1\x31");
// Ceci ne produit pas une erreur JSON
json_encode('okay', JSON_THROW_ON_ERROR);
// L'état d'erreur global n'a pas été modifié par le json_encode() ci-dessus
var_dump(json_last_error() === JSON_ERROR_UTF8);
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
bool(true)
]]>
</screen>
</example>
</para>
</refsect1>
<refsect1 role="seealso">
&reftitle.seealso;
<para>
<simplelist>
<member><function>json_last_error_msg</function></member>
<member><function>json_decode</function></member>
<member><function>json_encode</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
-->