rfc:is_json
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revisionLast revisionBoth sides next revision | ||
rfc:is_json [2022/08/14 22:06] – created juan_morales | rfc:is_json [2022/08/17 14:00] – juan_morales | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== PHP RFC: is_json() function | + | ====== PHP RFC: is_json ====== |
* Version: 0.9 | * Version: 0.9 | ||
* Date: 2022-08-14 | * Date: 2022-08-14 | ||
Line 5: | Line 5: | ||
* Status: Draft | * Status: Draft | ||
* First Published at: http:// | * First Published at: http:// | ||
+ | * Implementation: | ||
- | This is a suggested template for PHP Request for Comments (RFCs). Change this template to suit your RFC. Not all RFCs need to be tightly specified. | + | ===== Preliminary note ===== |
- | Read https:// | + | |
+ | **If the name of the function needs to be change, I will change it; the functionality is the important thing here** | ||
===== Introduction ===== | ===== Introduction ===== | ||
- | This RFC introduces a new function called is_json() to validate if an string | + | This RFC introduces a new function called is_json() to validate if an string |
- | ===== Fundaments ===== | + | Most userland implementations to achieve this are done using json_decode() which by design generates an object/ |
- | - Disadvantages of using json_decode | + | |
- | - Reasons for this and projects emulating this with json_decode. | + | |
- | - Use existing JSON parser in a more isolated way. | + | |
===== Proposal ===== | ===== Proposal ===== | ||
- | This RFC proposes a new function, that returns true if the given string | + | ==== Description ==== |
+ | <code php> | ||
+ | is_json(string $json, int $depth = 512, int $flags = 0): bool | ||
+ | </ | ||
+ | |||
+ | ==== Parameters ==== | ||
+ | **'' | ||
+ | |||
+ | The json string being analyzed. | ||
+ | |||
+ | This function | ||
+ | |||
+ | Note: | ||
+ | |||
+ | PHP implements a superset of JSON as specified in the original » RFC 7159. | ||
+ | |||
+ | **'' | ||
+ | |||
+ | Maximum nesting depth of the structure being decoded. | ||
+ | |||
+ | **'' | ||
+ | |||
+ | Bitmask of **JSON_INVALID_UTF8_IGNORE**, | ||
Function description, | Function description, | ||
- | .... | + | |
+ | ==== Return values ==== | ||
+ | |||
+ | Returns **true** if the string passed contains a valid json, otherwise returns **false**. | ||
+ | |||
+ | ==== Extra behavior ==== | ||
+ | - | ||
+ | ==== Examples ==== | ||
+ | - | ||
+ | ===== Fundaments/ | ||
+ | ==== Disadvantages of using json_decode ==== | ||
+ | |||
+ | By design, json_decode() generates an object/ | ||
+ | |||
+ | ==== Disadvantages of using regex ==== | ||
+ | |||
+ | Using a regex for this task forces different, error-prone, | ||
+ | |||
+ | ==== Needs from major projects and developers ==== | ||
+ | |||
+ | In the " | ||
+ | |||
+ | ==== 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/ | ||
===== Backward Incompatible Changes ===== | ===== Backward Incompatible Changes ===== | ||
Line 47: | Line 92: | ||
===== References ===== | ===== References ===== | ||
- | - (To be done later) | + | |
- | -- Links to external references, discussions or RFCs | + | __Major Open-Source projects needing this__ |
- | -- Links to major open-source projects emulating | + | |
- | -- Link to Stackoverflow pointing | + | [[https:// |
+ | |||
+ | <code php> | ||
+ | class JsonValidator extends ConstraintValidator | ||
+ | </ | ||
+ | |||
+ | [[https:// | ||
+ | |||
+ | <code php> | ||
+ | public function validateJson($attribute, $value) | ||
+ | { | ||
+ | if (is_array($value)) { | ||
+ | return false; | ||
+ | } | ||
+ | |||
+ | if (! is_scalar($value) && ! is_null($value) && ! method_exists($value, | ||
+ | return false; | ||
+ | } | ||
+ | |||
+ | json_decode($value); | ||
+ | |||
+ | return json_last_error() === JSON_ERROR_NONE; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | [[https:// | ||
+ | |||
+ | <code php> | ||
+ | public static function isJson($value) | ||
+ | { | ||
+ | </ | ||
+ | |||
+ | [[https:// | ||
+ | |||
+ | <code php> | ||
+ | private function getJSONString($input) | ||
+ | { | ||
+ | $output = json_decode($input); | ||
+ | return $output ? $this-> | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | [[https:// | ||
+ | |||
+ | <code php> | ||
+ | protected function isValidJsonValue($value) | ||
+ | { | ||
+ | if (in_array($value, [' | ||
+ | || (json_decode($value) !== null && json_last_error() === JSON_ERROR_NONE) | ||
+ | ) { | ||
+ | return true; | ||
+ | } | ||
+ | //JSON last error reset | ||
+ | json_encode([]); | ||
+ | return false; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | [[https:// | ||
+ | |||
+ | <code php> | ||
+ | public function isValid($string) | ||
+ | { | ||
+ | if ($string !== false && $string !== null && $string !== '' | ||
+ | json_decode($string); | ||
+ | if (json_last_error() === JSON_ERROR_NONE) { | ||
+ | return true; | ||
+ | } | ||
+ | } | ||
+ | return false; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | |||
+ | [[https:// | ||
+ | |||
+ | <code php> | ||
+ | public static function validateJson($value, | ||
+ | { | ||
+ | return (bool) (@json_decode($value)); | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | |||
+ | [[https:// | ||
+ | |||
+ | <code php> | ||
+ | public function getPrettyJson() | ||
+ | { | ||
+ | $decoded = json_decode($this-> | ||
+ | |||
+ | return \JSON_ERROR_NONE === json_last_error() ? json_encode($decoded, | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | |||
+ | [[https:// | ||
+ | |||
+ | <code php> | ||
+ | final class Json extends AbstractRule | ||
+ | { | ||
+ | /** | ||
+ | * {@inheritDoc} | ||
+ | */ | ||
+ | public | ||
+ | { | ||
+ | if (!is_string($input) || $input === '' | ||
+ | return false; | ||
+ | } | ||
+ | |||
+ | | ||
+ | |||
+ | return json_last_error() === JSON_ERROR_NONE; | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | [[https:// | ||
+ | |||
+ | <code php> | ||
+ | final class Json extends AbstractRule | ||
+ | { | ||
+ | /** | ||
+ | * {@inheritDoc} | ||
+ | */ | ||
+ | public function validate($input): | ||
+ | { | ||
+ | if (!is_string($input) || $input === '' | ||
+ | return false; | ||
+ | } | ||
+ | |||
+ | json_decode($input); | ||
+ | |||
+ | return json_last_error() === JSON_ERROR_NONE; | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | [[https:// | ||
+ | |||
+ | |||
+ | <code php> | ||
+ | public function actionIndex() | ||
+ | { | ||
+ | Yii::$app-> | ||
+ | |||
+ | if(!SecuritySettings:: | ||
+ | return; | ||
+ | } | ||
+ | |||
+ | $json_data = file_get_contents(' | ||
+ | if ($json_data = json_decode($json_data)) { //<----- json_decode() just to check if is valid json-string only | ||
+ | $json_data = json_encode($json_data, | ||
+ | $json_data = preg_replace('/ | ||
+ | Yii:: | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | [[https:// | ||
+ | |||
+ | <code php> | ||
+ | public static function isJson($string) | ||
+ | { | ||
+ | json_decode($string); | ||
+ | |||
+ | return json_last_error() == JSON_ERROR_NONE; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | [[https:// | ||
+ | |||
+ | <code php> | ||
+ | function is_json( $argument, $ignore_scalars = true ) { | ||
+ | if ( ! is_string( $argument ) || '' | ||
+ | return false; | ||
+ | } | ||
+ | |||
+ | if ( $ignore_scalars && ! in_array( $argument[0], | ||
+ | return false; | ||
+ | } | ||
+ | |||
+ | json_decode( $argument, $assoc = true ); | ||
+ | |||
+ | return json_last_error() === JSON_ERROR_NONE; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | [[https:// | ||
+ | |||
+ | <code php> | ||
+ | if (\is_string($value)) { | ||
+ | json_decode($value); | ||
+ | |||
+ | // Check if value is a valid JSON string. | ||
+ | if ($value !== '' | ||
+ | /** | ||
+ | * If the value is not empty and is not a valid JSON string, | ||
+ | * it is most likely a custom field created in Joomla 3 and | ||
+ | * the value is a string that contains the file name. | ||
+ | */ | ||
+ | if (is_file(JPATH_ROOT . '/' | ||
+ | $value = ' | ||
+ | } else { | ||
+ | $value = ''; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | |||
+ | |||
+ | __Stackoverflow questions related to this__ | ||
+ | |||
+ | In PHP, this question | ||
+ | |||
+ | [[https:// | ||
+ | |||
+ | Viewed 484k times | ||
+ | |||
+ | [[https:// | ||
+ | |||
+ | __Person asking how to do exactly | ||
+ | |||
+ | [[https:// | ||
+ | |||
+ | __Someone has also doing exactly this , in JAVA__ | ||
+ | |||
+ | [[https:// | ||
===== Rejected Features ===== | ===== Rejected Features ===== | ||
- No rejected features currently. | - No rejected features currently. |
rfc/is_json.txt · Last modified: 2022/08/17 14:04 by juan_morales