[wp-trac] [WordPress Trac] #59234: Introduce a `wp_json_decode()` function, including validation when available

WordPress Trac noreply at wordpress.org
Tue Aug 29 04:55:16 UTC 2023


#59234: Introduce a `wp_json_decode()` function, including validation when
available
-------------------------------+---------------------
 Reporter:  jrf                |       Owner:  (none)
     Type:  enhancement        |      Status:  new
 Priority:  normal             |   Milestone:  6.4
Component:  General            |     Version:  trunk
 Severity:  normal             |  Resolution:
 Keywords:  php83 needs-patch  |     Focuses:
-------------------------------+---------------------

Comment (by ayeshrajans):

 Thanks for opening this ticket. I have a few thoughts on this:

  - If we were to introduce a `wp_json_decode` function (which I actually
 did, that lead me to these points), we will have to throw an exception to
 handle errors. This is because `false` itself is a valid return value, now
 we are overstepping the general use case of "decode" to "validate and
 decode".

  - If we were to run PHP 8.3 native `json_validate` _and_ `json_decode`
 inside the new `wp_json_decode`, wouldn't that be a performance
 degradation for valid data?


  - I think a more mild approach would be to polyfill `json_validate`
 function? That way, we are giving the choice to the programmer if we are
 dealing with potentially invalid JSON. For PHP 8.3, we don't have a
 performance penalty, but at least now the choice is explicit?

 {{{#!php
 <?php

 /**
  * Decodes a JSON string. It runs a more performance data validation if
  * json_decode function is available.
  *
  * @since 6.4.0
  *
  * @param string $json The json string being decoded.
  * @param bool $associative Optional. When true, JSON objects will be
 returned
  *     as associative arrays; when false, JSON objects will be returned as
  *     objects. Note that PHP >= 7.2 native json_decode() function has
  *     $associative as a nullable parameter. In wp_json_decode, it is not
  *     nullable to ensure PHP < 7.2 compatibility.
  * @param int $depth Optional. Maximum nesting depth of the structure
 being
  *     decoded. The value must be greater than 0, and less than or equal
 to
  *     2147483647.
  * @param int $flags Optional. Bitmask of JSON decode options. @see
 json_decode
  *     for available options.
  *
  * @return mixed Returns the value encoded in json as an appropriate PHP
 type.
  *    Unquoted values true, false and null are returned as true, false and
 null
  *    respectively. null is returned if the json cannot be decoded or if
 the
  *    encoded data is deeper than the nesting limit.
  *
  */
 function wp_json_decode( string $json, bool $associative = FALSE, int
 $depth = 512, int $flags = 0 ) {
         if ( function_exists( 'json_validate' ) && json_validate( $json,
 $depth, $flags & JSON_INVALID_UTF8_IGNORE ) === FALSE ) {
                 return FALSE;
         }

         return json_decode( $json, $associative, $depth, $flags );
 }

 }}}

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/59234#comment:1>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list