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:41] – 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 91: | 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.1660592491.txt.gz · Last modified: 2022/08/15 19:41 by juan_morales