mirror of
https://github.com/php/doc-ja.git
synced 2026-03-24 07:02:08 +01:00
162 lines
5.1 KiB
XML
162 lines
5.1 KiB
XML
<?xml version="1.0" encoding="utf-8"?>
|
||
<!-- $Revision$ -->
|
||
<!-- EN-Revision: d715365c098db000eaf7dcd987ee6093f6e83091 Maintainer: mumumu Status: ready -->
|
||
<refentry xml:id="function.json-validate" xmlns="http://docbook.org/ns/docbook">
|
||
<refnamediv>
|
||
<refname>json_validate</refname>
|
||
<refpurpose>文字列が有効なJSONかどうかを調べる</refpurpose>
|
||
</refnamediv>
|
||
|
||
<refsect1 role="description">
|
||
&reftitle.description;
|
||
<methodsynopsis>
|
||
<type>bool</type><methodname>json_validate</methodname>
|
||
<methodparam><type>string</type><parameter>json</parameter></methodparam>
|
||
<methodparam choice="opt"><type>int</type><parameter>depth</parameter><initializer>512</initializer></methodparam>
|
||
<methodparam choice="opt"><type>int</type><parameter>flags</parameter><initializer>0</initializer></methodparam>
|
||
</methodsynopsis>
|
||
<para>
|
||
指定された文字列が、文法的に有効なJSONかどうかを返します。
|
||
<function>json_validate</function> が &true; を返す場合、
|
||
同じ <parameter>depth</parameter> と <parameter>flags</parameter>
|
||
を指定して <function>json_decode</function> を実行してもデコードは成功します。
|
||
</para>
|
||
<para>
|
||
<function>json_validate</function> が &false; を返す場合、
|
||
原因は <function>json_last_error</function> と
|
||
<function>json_last_error_msg</function> を使って取得できます。
|
||
</para>
|
||
<para>
|
||
デコードされたJSONペイロードを使わない場合、
|
||
<function>json_validate</function> は
|
||
<function>json_decode</function> よりもメモリ使用量が少なくなります。
|
||
なぜなら、ペイロードを含む配列やオブジェクト構造を組み立てる必要がないからです。
|
||
</para>
|
||
<caution>
|
||
<para>
|
||
<function>json_decode</function> の直前に
|
||
<function>json_validate</function> をコールすると、
|
||
文字列を不必要に2回パースすることになります。
|
||
なぜなら、<function>json_decode</function>
|
||
はデコードしている間に暗黙のうちに文字列を検証するからです。
|
||
</para>
|
||
<para>
|
||
よって <function>json_validate</function> は、JSON のペイロードをすぐに使わない場合、かつ有効なJSON文字列かどうかを知る必要がある場合にのみ使うべきです。
|
||
</para>
|
||
</caution>
|
||
</refsect1>
|
||
|
||
<refsect1 role="parameters">
|
||
&reftitle.parameters;
|
||
<para>
|
||
<variablelist>
|
||
<varlistentry>
|
||
<term><parameter>json</parameter></term>
|
||
<listitem>
|
||
<para>
|
||
検証する文字列
|
||
</para>
|
||
<para>
|
||
この関数は、UTF-8 でエンコードされた文字列でのみ動作します。
|
||
</para>
|
||
&json.implementation.superset;
|
||
</listitem>
|
||
</varlistentry>
|
||
<varlistentry>
|
||
<term><parameter>depth</parameter></term>
|
||
<listitem>
|
||
<para>
|
||
デコードされるネスト構造の深さの最大値。
|
||
この値は <literal>0</literal> より大きく、
|
||
<literal>2147483647</literal> 以下でなければいけません。
|
||
</para>
|
||
</listitem>
|
||
</varlistentry>
|
||
<varlistentry>
|
||
<term><parameter>flags</parameter></term>
|
||
<listitem>
|
||
<para>
|
||
現状は、<constant>JSON_INVALID_UTF8_IGNORE</constant> のみを受け入れます。
|
||
</para>
|
||
</listitem>
|
||
</varlistentry>
|
||
</variablelist>
|
||
</para>
|
||
</refsect1>
|
||
|
||
<refsect1 role="returnvalues">
|
||
&reftitle.returnvalues;
|
||
<para>
|
||
指定された文字列が文法的に有効なJSONの場合、&true; を返します。
|
||
そうでない場合、&false; を返します。
|
||
</para>
|
||
</refsect1>
|
||
|
||
<refsect1 role="errors">
|
||
&reftitle.errors;
|
||
<para>
|
||
<parameter>depth</parameter> が許可された範囲外の値の場合、
|
||
<classname>ValueError</classname> がスローされます。
|
||
</para>
|
||
<para>
|
||
<parameter>flags</parameter> が有効なフラグでない場合、
|
||
<classname>ValueError</classname> がスローされます。
|
||
</para>
|
||
</refsect1>
|
||
|
||
<refsect1 role="examples">
|
||
&reftitle.examples;
|
||
<para>
|
||
<example>
|
||
<title><function>json_validate</function> の例</title>
|
||
<programlisting role="php">
|
||
<![CDATA[
|
||
<?php
|
||
var_dump(json_validate('{ "test": { "foo": "bar" } }'));
|
||
var_dump(json_validate('{ "": "": "" } }'));
|
||
?>
|
||
]]>
|
||
</programlisting>
|
||
&example.outputs;
|
||
<screen>
|
||
<![CDATA[
|
||
bool(true)
|
||
bool(false)
|
||
]]>
|
||
</screen>
|
||
</example>
|
||
</para>
|
||
</refsect1>
|
||
|
||
<refsect1 role="seealso">
|
||
&reftitle.seealso;
|
||
<para>
|
||
<simplelist>
|
||
<member><function>json_decode</function></member>
|
||
<member><function>json_last_error</function></member>
|
||
<member><function>json_last_error_msg</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
|
||
-->
|