wp-media/search-and-replace

Json Decode

Closed this issue · 1 comments

widoz commented

Notice that the Replace class use a method is_json that call json_decode to test if the $string passed to the method is a valid json or not.

When the json is much consistent it is possible that the operation may take much execution time that can be avoided by testing for scalar typing before to try to decode the json.

For example:

if (! is_string($data) || ! trim($data)) {
        return false;
    }

    return (
               // Maybe an empty string, array or object.
               $data === '""' ||
               $data === '[]' ||
               $data === '{}' ||
               // Maybe an encoded JSON string.
               $data[0] === '"' ||
               // Maybe a flat array.
               $data[0] === '[' ||
               // Maybe an associative array.
               $data[0] === '{'
           ) && json_decode($data) !== null;

Thoughts?

widoz commented

I close this one since there is no use of is_json within the project. I remove the method in fb7ffd4