+ $result = json_decode($js);
+ if ($throwException && $result === NULL && $js !== 'null') {
+ throw new CRM_Core_Exception(json_last_error_msg());
+ }
+ return $result;
+ }
+
+ /**
+ * @param string $str
+ * @param bool $throwException
+ * @return string|null
+ * @throws CRM_Core_Exception
+ */
+ public static function convertSingleQuoteString(string $str, $throwException) {
+ // json_decode can only handle double quotes around strings, so convert single-quoted strings
+ $backslash = chr(0) . 'backslash' . chr(0);
+ $str = str_replace(['\\\\', '\\"', '"', '\\&', '\\/', $backslash], [$backslash, '"', '\\"', '&', '/', '\\'], substr($str, 1, -1));
+ // Ensure the string doesn't terminate early by checking that all single quotes are escaped
+ $pos = -1;
+ while (($pos = strpos($str, "'", $pos + 1)) !== FALSE) {
+ if (($pos - strlen(rtrim(substr($str, 0, $pos)))) % 2) {
+ if ($throwException) {
+ throw new CRM_Core_Exception('Invalid string passed to CRM_Utils_JS::decode');
+ }
+ return NULL;
+ }
+ }
+ return '"' . $str . '"';