Skip to content

[PHP 8.3] Added json_validate function description.#2906

Merged
Girgias merged 4 commits into
php:masterfrom
mumumu:add-json-validate-function-manual
Nov 14, 2023
Merged

[PHP 8.3] Added json_validate function description.#2906
Girgias merged 4 commits into
php:masterfrom
mumumu:add-json-validate-function-manual

Conversation

@mumumu
Copy link
Copy Markdown
Member

@mumumu mumumu commented Nov 2, 2023

@mumumu mumumu added this to the PHP 8.3 milestone Nov 2, 2023
@mumumu mumumu force-pushed the add-json-validate-function-manual branch from 4b9d32d to 4f816f8 Compare November 2, 2023 20:16
@mumumu mumumu force-pushed the add-json-validate-function-manual branch from 4f816f8 to 01cb1ee Compare November 3, 2023 03:38
Comment thread reference/json/functions/json-validate.xml Outdated
Comment on lines +17 to +26
<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>
Copy link
Copy Markdown
Member

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>

Copy link
Copy Markdown
Member

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.

Comment thread reference/json/functions/json-validate.xml Outdated
Comment thread reference/json/functions/json-validate.xml Outdated
@TimWolla TimWolla requested a review from Girgias November 3, 2023 19:30
Comment on lines +59 to +60
Bitmask of
<constant>JSON_INVALID_UTF8_IGNORE</constant>.
Copy link
Copy Markdown
Member

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?

Copy link
Copy Markdown
Member Author

@mumumu mumumu Nov 6, 2023

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();
	}

https://github.com/php/php-src/blob/ea299d44a155e0ef39c24a4d79b0e51accf8d9d0/ext/json/json.c#L329-L332

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>

Copy link
Copy Markdown
Member

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. :)

Copy link
Copy Markdown
Member

@Girgias Girgias left a 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

@mumumu mumumu changed the title Added json_validate function description. [PHP 8.3] Added json_validate function description. Nov 6, 2023
mumumu and others added 2 commits November 6, 2023 20:09
Copy link
Copy Markdown
Member

@TimWolla TimWolla left a 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.

@Girgias Girgias merged commit de195fc into php:master Nov 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants