There appears to be an oddity with json_encode and/or json_decode when attempting decode a string that was produced by json_encode:
$object = new stdClass;
$object->namespace = 'myCompany\package\subpackage';
$jsonEncodedString = json_encode($object);
echo $jsonEncodedString;
// Result of echo being:
// {"namespace":"myCompany\\package\\subpackage"}
$anotherObject = json_decode($jsonEncodedString);
echo $anotherObject->namespace;
// Result of echo being:
// myCompany\package\subpackage
$yetAnotherObject = json_decode('{"namespace":"myCompany\\package\\subpackage"}');
// Result should be:
// myCompany\package\subpackage
// But returns an invalid string sequence error instead...
echo json_last_error_msg();
I've never experienced a problem previous to this, as I've never had to capture a string with backslashes in it until today. In reviewing the code above, I can encode/decode utilizing PHP's built in components. However if I attempt to decode a string that was produced by this encoder I get an error message. I've read in documentation items like "Predefined Constants" and other stack questions like "how remove the backslash (“\”) in the json response using php?". But I can't seem to find a reason WHY I can't decode a string that was produced by json_encode. The PHP version I'm using to test this code is 5.5.9.
I know I could be totally clueless by missing something, but should I be handling my string that was produced by json_encode differently if I attempt to use it elsewhere?