Ensures a standard json value and escapes values

@param mixed $result
@param boolean $is_key
@return string
960369
public function _prep_args($result, $is_key=FALSE) {
		if (is_null($result)) {
			return 'null';
		} elseif (is_bool($result)) {
			return ($result===TRUE) ? 'true' : 'false';
		} elseif (is_string($result)||$is_key) {
			return '"'.str_replace(array (
					'\\',"\t","\n","\r",'"','/'
			), array (
					'\\\\','\\t','\\n',"\\r",'\"','\/'
			), $result).'"';
		} elseif (is_scalar($result)) {
			return $result;
		}
	}
610991
public function validAuthenticationResponse($request, $result)
    {
        if (is_bool($result)) {
            return json_encode($result);
        }

        $channelName = $this->normalizeChannelName($request->channel_name);

        return json_encode(['channel_data' => [
            'user_id' => $this->retrieveUser($request, $channelName)->getAuthIdentifier(),
            'user_info' => $result,
        ]]);
    }
528154
public static Map<String, String> convertResultStringToMap(String result) {
		Map<String, String> map = null;

		if (StringUtils.isNotBlank(result)) {
			if (result.startsWith("{") && result.endsWith("}")) {
				result = result.substring(1, result.length() - 1);
			}
			map = parseQString(result);
		}

		return map;
	}
1283967
public static function json_decode($json, $assoc=false) {
		//replace single quoted values
		$json = preg_replace_callback('/:\s*\'(([^\']|\\\\\')*)\'\s*([},])/', function ($m) {
			return ':' . json_encode(stripslashes($m[1])) . $m[3];
		}, $json);

		//replace single quoted keys
		$json = preg_replace_callback('/\'(([^\']|\\\\\')*)\'\s*:/', function($m){
			return json_encode(stripslashes($m[1])) . ':';
		}, $json);
		//remove any line breaks in the code
		$json = str_replace(array("\n","\r"),"",$json);

		//replace non-quoted keys with double quoted keys
		$json = preg_replace('#(?<pre>\{|\[|,)\s*(?<key>(?:\w|_)+)\s*:#im', '$1"$2":', $json);

		//remove trailing comma
		$json = preg_replace('/,\s*\}/','}',$json);

		$return = json_decode($json, $assoc);
		$error = json_last_error_msg();
		return $return;
	}
325230
protected void readKeyValues(Map<String, Object> result) {
		while (hasMoreProperties()) {
			String name = readPropertyName();
			log.debug("property: {}", name);
			Object property = Deserializer.deserialize(this, Object.class);
			log.debug("val: {}", property);
			result.put(name, property);
			if (hasMoreProperties()) {
				skipPropertySeparator();
			} else {
				break;
			}
		}
	}
1360154
public function isJson($value, $isReturnMap = false)
    {
        if (is_array($value) || is_object($value)) {
            return false;
        }
        
        $data = json_decode($value, true);

        if (json_last_error() !== JSON_ERROR_NONE) {
            return false;
        }
        
        return $isReturnMap ? $data : true;
    }
1145434
public function as_json($encoded = true) {
    $result = $this->api_friendly_values();
    return $encoded ? json_encode($result) : $result;
  }
767230
private function _decode($result, $field = null)
    {
        $this->data = json_decode($result, true);
        if (!empty($this->data['errcode'])) {
            $this->errCode = $this->data['errcode'];
            $this->errMsg = $this->data['errmsg'];
            return false;
        }
        if ($this->data && !is_null($field)) {
            if (isset($this->data[$field])) {
                return $this->data[$field];
            } else {
                return false;
            }
        }
        return $this->data;
    }
307323
def jsonify(result, format=False):
    '''  '''

    result2 = result.copy()
    if format:
        return json.dumps(result2, sort_keys=True, indent=4)
    else:
        return json.dumps(result2, sort_keys=True)
295076
protected function processExportResult(& $result, $format)
    {
        if ($format == 'php') {
            $phpResult = json_decode($result, true); // true => return as array instead of object
                
            $jsonError = json_last_error();
                
            switch ($jsonError) {
                case JSON_ERROR_NONE:
                    $result = $phpResult;
                    break;
                default:
                    $message =  "JSON error (" . $jsonError . ") \"" . json_last_error_msg()
                        ."\" in REDCap API output."
                        ."\nThe first 1,000 characters of output returned from REDCap are:\n"
                        .substr($result, 0, 1000);
                    $code = ErrorHandlerInterface::JSON_ERROR;
                    $this->errorHandler->throwException($message, $code);
                    break; // @codeCoverageIgnore
            }
                
            if (array_key_exists('error', $result)) {
                $this->errorHandler->throwException($result ['error'], ErrorHandlerInterface::REDCAP_API_ERROR);
            } // @codeCoverageIgnore
        } else {
            // If this is a format other than 'php', look for a JSON error, because
            // all formats return errors as JSON
            $matches = array();
            $hasMatch = preg_match(self::JSON_RESULT_ERROR_PATTERN, $result, $matches);
            if ($hasMatch === 1) {
                // note: $matches[0] is the complete string that matched
                //       $matches[1] is just the error message part
                $message = $matches[1];
                $this->errorHandler->throwException($message, ErrorHandlerInterface::REDCAP_API_ERROR);
            } // @codeCoverageIgnore
        }
        
        return $result;
    }
996959
protected function getParam($str_key, $str_default = null, $check_json_body = false)
    {
        $str_query = $this->getQuery($str_key);
        if (null !== $str_query) {
            return $str_query;
        }
        $str_post = $this->getPost($str_key);
        if (NULL !== $str_post) {
            return $str_post;
        }
        // Optionally check Json in Body
        if ($check_json_body && isset($this->getJson()->$str_key)) {
            if (null !== $this->getJson()->$str_key) {
                return $this->getJson()->$str_key;
            }
        }
        return $str_default;
    }
755016
protected function success($results = null)
    {
        $this->trigger('json-success', $this, $results);
        $this->trigger('response-success', $this, $results);

        $json = array('error' => false);

        if($results) {
            $json['results'] = $results;
        }

        $body = json_encode($json, JSON_PRETTY_PRINT);

        $this->response
            ->set('headers', 'Content-Type', 'text/json')
            ->set('body', $body);

        return $body;
    }
320596
public function verifySign($result, $key)
    {
        $sign_text = $result['sign'];
        $sign = $this->createSign($result, $key);
        return ($sign == $sign_text);
    }
1363802
protected function getKeysFromResults(array $results, $key = null)
    {
        if (is_null($key)) {
            $key = $this->parentMap->getKeyName();
        }

        return array_unique(array_values(array_map(function ($value) use ($key) {
            return $value[$key];
        }, $results)));
    }
747051
private function transformResult($result)
    {
        if ($this->actualDriver() === self::DRIVER_PREDIS && $result instanceof Status) {
            $result = $result->getPayload() === 'OK';
        }
        return $result;
    }
426630
public function build(ResultSpec $result): string
    {
        $response = [];
        $units = $result->getResults();

        foreach ($units as $unit) {
            /** @var AbstractResult $unit */
            if ($unit instanceof Result) {
                /** @var Result $unit */
                $response[] = [
                    'jsonrpc' => '2.0',
                    'result' => $this->preBuild($unit->getResult()),
                    'id'     => $unit->getId()
                ];
            } elseif ($unit instanceof Error) {
                /** @var Error $unit */
                $baseException = $unit->getBaseException();
                $response[] = [
                    'jsonrpc' => '2.0',
                    'error' => [
                        'code'    => $baseException->getJsonRpcCode(),
                        'message' => $this->getErrorMessage($baseException->getJsonRpcCode()),
                        'data'    => $baseException->getJsonRpcData()
                    ],
                    'id' => $unit->getId()
                ];
            }
        }

        if (empty($response)) {
            return '';
        }

        if ($result->isSingleResult()) {
            return json_encode($response[0]);
        }

        return json_encode($response);
    }
453399
def blazer_json_escape(s)
      if Rails::VERSION::STRING < "4.1"
        result = s.to_s.gsub(JSON_ESCAPE_REGEXP, JSON_ESCAPE)
        s.html_safe? ? result.html_safe : result
      else
        json_escape(s)
      end
    end