The use of json_decode()
on data that is expected to be scalar (non-iterable) might seem mostly redundant because strings, integers, and floats in most cases will be unaffected.
There are some fringe cases like:
- Strings containing escaped forward slashes
- Strings containing multibyte characters
- Strings containing boolean-expressions as strings that will be used as boolean type
- Strings containing numeric-interpreted values that will be used as integer/float type
- ...there may well be more cases that I am not considering
Here is a non-exhaustive Demo:
$tests = [
'true',
null,
'123',
123,
json_encode('1/2'),
json_encode('База данни грешка')
];
foreach ($tests as $test) {
echo var_export(json_decode($test, true)) . "\n---\n";
}
Output:
true #now boolean type
---
NULL
---
123 #now integer type
---
123
---
'1/2' #previously had escaped slash in storage
---
'База данни грешка' #previously mutated as unicode expressions in storage
---
So in some cases, encoding-storing-decoding creates stability across environments that might otherwise experience "hiccups". In other cases, it may be a workaround to render a strings value as a boolean when the environment does not allow the storage of a boolean type value.
All this said, allowing json type data in customizable fields affords Joomla and its users greater utility when handling complex/unexpected data structures / values.
p.s. the condition:
if ($data->profile[$k] === null)
{
$data->profile[$k] = $v[1];
}
is just a matter of Joomla trying to do some mopping up when the expected outcome failed because the value was not appropriately json_encoded before going into storage (...accommodating bad development/data or perhaps prior versions of Joomla that were not json_encoding).