rfc:is_json
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
rfc:is_json [2022/08/15 19:12] – juan_morales | rfc:is_json [2022/08/17 14:04] (current) – juan_morales | ||
---|---|---|---|
Line 5: | Line 5: | ||
* Status: Draft | * Status: Draft | ||
* First Published at: http:// | * First Published at: http:// | ||
+ | * Implementation: | ||
===== Preliminary note ===== | ===== Preliminary note ===== | ||
Line 14: | Line 15: | ||
Most userland implementations to achieve this are done using json_decode() which by design generates an object/ | Most userland implementations to achieve this are done using json_decode() which by design generates an object/ | ||
- | |||
- | ===== 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 " | ||
- | |||
- | ==== 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 " | ||
- | < | ||
- | 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/ | ||
- | |||
- | ==== Name of the function ==== | ||
- | |||
- | If the links provided in the " | ||
===== Proposal ===== | ===== Proposal ===== | ||
Line 76: | Line 46: | ||
Returns **true** if the string passed contains a valid json, otherwise returns **false**. | 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 101: | Line 92: | ||
===== References ===== | ===== References ===== | ||
- | - (To be done later) | + | |
- | -- Links to external references, discussions or RFCs | + | ==== Major Open-Source projects that will benefit out of 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 | ||
+ | |||
+ | Viewed 484k times. [[https:// | ||
+ | |||
+ | Person asking how to do exactly | ||
+ | |||
+ | Someone has also doing exactly this , in JAVA. [[https:// | ||
===== Rejected Features ===== | ===== Rejected Features ===== | ||
- No rejected features currently. | - No rejected features currently. |
rfc/is_json.1660590721.txt.gz · Last modified: 2022/08/15 19:12 by juan_morales