rfc:is_json

This is an old revision of the document!


PHP RFC: is_json

Preliminary note

If the name of the function needs to be change, I will change it; the functionality is the important thing here

Introduction

This RFC introduces a new function called is_json() to validate if an string contains a valid json.

Most userland implementations to achieve this are done using json_decode() which by design generates an object/array while parsing the string, ergo using memory and processing, that could be save.

Fundaments/Reasons

Disadvantages of using json_decode

By design, json_decode() generates an object/array while parsing the string, ergo using memory and processing for it, that is not needed if the only thing to discover is if a string contains a valid json.

Disadvantages of using regex

Using a regex for this task forces different, error-prone, hard to maintain, implementations.

Needs from major projects and developers

In the “References” section, there is a list of major open-source php projects needing this feature; also in th mntioned section can find a link to one of the most popular StackOverflow questions, which somehow reflects the need from our developers to have a feature like this included.

Why not using a 3rd party package for this

- This feature is very web related, which is a huge coincidence with the first lines of the default RFC template provided to “us”:

PHP is and should remain:

1) a pragmatic web-focused language ....

- We should not force the community to install 3rd party packages for this kind of features (feature totally aligned with PHP's principles, and very easy to achieve), which would generate a dependency between the project and the package.

Complexity added in the core

At the moment, there is a JSON parser in the core, used by json_decode to do its job, so there is no need to write a new JSON parser for this RFC; the proposed function will use the existing JSON parser exclusively to parse an string without generating any object/array in memory for it.

Name of the function

If the links provided in the “References” section are checked, one can see that some of those major projects have created a function called this isJson().

Proposal

This RFC proposes a new function, that returns true if the given string is a valid json-string.

Function description, examples, technical strategy, current JSON parser, etc. ....

Backward Incompatible Changes

None, as this is a new function only.

is_json will no longer be available as a function name, could break potential userland implementations.

Proposed PHP Version(s)

next PHP 8.x

RFC Impact

This RFC has no impact on SAPIs, existing extensions, Opcache, etc.

Open Issues

- No open issues

Future Scope

- (To be defined)

Proposed Voting Choices

- (To be defined)

Implementation

- (To be done later after pushing to github)

References

- (To be done later) -- Links to external references, discussions or RFCs -- Links to major open-source projects emulating this function using json_decode -- Link to Stackoverflow pointing the question, which this proposed function satisfy the needs

Rejected Features

- No rejected features currently.

rfc/is_json.1660589573.txt.gz · Last modified: 2022/08/15 18:52 by juan_morales