I had a code review and got told by the senior programmer that it was very bad programming practice to use the json_decode() function of php to determine if a string was not in JSON format.
My argument was that the json_decode function has a fail case (returning NULL) and this exists for reasons such as the purpose I was using it for.
I got sat down one on one and told to never pass data to a function that was not intended for the function (in this case referring to passing a hex string to the json_decode() function).
Is what I did bad coding practice? How should I have written the code?
Here is the php code of what I basically did:
//example JSON Data
/*
$ExampleData = '
{
"test": "48656c6c6f20576f726c64"
}
';
*/
//the most common case is that a hex string sent to the php script
$ExampleData = "48656c6c6f20576f726c64";
$jsonData = json_decode($ExampleData);
//one in every 1000 responses is JSON data
if($jsonData)
{
//extract varaibles from the json
print ("JSON data recieved");
}
else
{
//response is hex so carry out normal function
print ("Hex Data recieved");
}