-
Notifications
You must be signed in to change notification settings - Fork 762
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[PHP 8.3] Added json_validate function description. #2906
Conversation
4b9d32d
to
4f816f8
Compare
4f816f8
to
01cb1ee
Compare
<para> | ||
Check if a specified string contains a valid json. | ||
Errors during validation can be fetched by using | ||
<function>json_last_error</function> and/or | ||
<function>json_last_error_msg</function>. | ||
</para> | ||
<para> | ||
It's guaranteed that the json valid in <function>json_validate</function> | ||
is also valid in <function>json_decode</function>. | ||
</para> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
<para>
Returns whether the given &string; is syntactically valid JSON.
If <function>json_validate</function> returns &true;, <function>json_decode</function>
will successfully decode the given string when using the same
<parameter>depth</parameter> and <parameter>flags</parameters>.
</para>
<para>
If <function>json_validate</function> returns &false;, the cause
can be retrieved using <function>json_last_error</function> and
<function>json_last_error_msg</function>.
</para>
<para>
<function>json_validate</function> uses less memory than
<function>json_decode</function> if decoded JSON payload is
not used, because it does not need to build the array or
object structure containing the payload.
</para>
<caution>
<para>
Calling <function>json_validate</function> immediately before
<function>json_decode</function> will unnecessarily parse the string
twice, as <function>json_decode</function> implicitly performs
validation during decoding.
</para>
<para>
<function>json_validate</function> should therefore only be used
if the decode JSON payload is not immediately used and knowing whether
the string contains valid JSON is needed.
</para>
</caution>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Small nit to the above suggestion:
json_validate uses less memory than
json_decode if the decoded JSON payload is
not used, because it does not need to build the array or
object structure containing the payload.
Bitmask of | ||
<constant>JSON_INVALID_UTF8_IGNORE</constant>. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is confusing?
Should be a bitmask of JSON_*
constatns no?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems that json_validate accepts PHP_JSON_INVALID_UTF8_IGNORE
only.
if ((options != 0) && (options != PHP_JSON_INVALID_UTF8_IGNORE)) {
zend_argument_value_error(3, "must be a valid flag (allowed flags: JSON_INVALID_UTF8_IGNORE)");
RETURN_THROWS();
}
We should change as the following?
<varlistentry>
<term><parameter>flags</parameter></term>
<listitem>
<para>
Currently only
<constant>JSON_INVALID_UTF8_IGNORE</constant>
is accepted.
</para>
</listitem>
</varlistentry>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes I think this makes more sense. :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mostly agree with @TimWolla's suggestions
Co-authored-by: Tim Düsterhus <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM now, except for Girgias' remark regarding the flags
parameter.
Based on RFC and json_validate source code.