Re: RFC Idea - is_json - looking for feedback

From: Date: Sat, 30 Jul 2022 14:01:09 +0000
Subject: Re: RFC Idea - is_json - looking for feedback
References: 1  Groups: php.internals 
Request: Send a blank email to internals+get-118322@lists.php.net to get a copy of this message
On Fri, Jul 29, 2022 at 4:27 PM juan carlos morales < dev.juan.morales@gmail.com> wrote: > I am following the RFC guideline for the first time. ( > https://wiki.php.net/rfc/howto) > > As suggested there, I am here to get a feeling from you, regarding the > following RFC for PHP. > > # Change (draft): > > New function in php called like: > > is_json(string $string): bool > > ## Description > ### Parameters > string $string -> string to find out if is a valid JSON or not > > ### Return > Returns a bool. The function is capable to determine if the passed string > is a valid JSON (true) or not (false). > > # Why this function ? > > At the moment the only way to determine if a JSON-string is valid we have > to execute the json_decode() function. > > The drawback about this, is that json_decode() generates an in memory an > object/array (depending on parameters) while parsing the string; this leads > to a memory usage that is not needed (because we use memory for creating > the object/array) and also can cause an error for reaching the memory-limit > of the php process. > > Sometimes we just need to know is the string is a valid json or not, and > nothing else. > > # Do we need something like this? If a check to an string is valid JSON > then for sure I will have to use it in my code either as an object or an > array. > > Well that is not true. There are plenty of cases where you just need to > check if a string is a valid json and that is it. Just looking into > stackoverflow will give you an idea about how many people is looking for > something like this in an efficient way. > Could you please give some specific examples where the proposed functionality would be useful? Regards, Nikita

« previous php.internals (#118322) next »