get json encoded string in virtual data format

@param	$nErrorId	int
@param	$sErrorDesc	string
@param	$arrVData	array
@param	$sVersion	string
@param	$bCached	boolean
@param	$arrExtra	array
@return	string
494499
public function GetVDataString
	(
		$nErrorId,
		$sErrorDesc	= '',
		$arrVData	= [],
		$sVersion	= '1.0',
		$bCached	= null,
		$arrExtra	= []
	)
	{
		//
		//	nErrorId	- [in] int		error id
		//	sErrorDesc	- [in/opt] string	error description
		//	arrVData	- [in/opt] array	virtual data
		//	sVersion	- [in/opt] string	service version, default is '1.0'
		//	bCached		- [in/opt] bool		if the data come from cache
		//	arrExtra	- [in/opt] array	extra data by key-value pairs
		//	RETURN		- encoded json string
		//
		$sRet = '';

		//	...
		$arrJson = $this->GetVDataArray( $nErrorId, $sErrorDesc, $arrVData, $sVersion, $bCached, $arrExtra );
		if ( $this->IsValidVData( $arrJson ) )
		{
			$sRet = @ json_encode( $arrJson );
		}
		else
		{
			$sRet = @ json_encode( $this->GetDefaultVDataJson() );
		}

		return $sRet;
	}
420957
public static function decodeData($data)
    {
        if (\is_array($data)) {
            foreach ($data as $key => $value) {
                $data[$key] = \urldecode($value);
            }

            return $data;
        }

        return \urldecode($data);
    }
958362
function jsonSerialize(): \stdClass {
    $map = new \stdClass;

    if (mb_strlen($repoToken = $this->getRepoToken())) $map->repo_token = $repoToken;
    if (mb_strlen($serviceName = $this->getServiceName())) $map->service_name = $serviceName;
    if (mb_strlen($serviceNumber = $this->getServiceNumber())) $map->service_number = $serviceNumber;
    if (mb_strlen($serviceJobId = $this->getServiceJobId())) $map->service_job_id = $serviceJobId;
    if (mb_strlen($servicePullRequest = $this->getServicePullRequest())) $map->service_pull_request = $servicePullRequest;

    $map->source_files = array_map(function(SourceFile $item) {
      return $item->jsonSerialize();
    }, $this->getSourceFiles()->getArrayCopy());

    if ($this->isParallel()) $map->parallel = true;
    if ($git = $this->getGit()) $map->git = $git->jsonSerialize();
    if (mb_strlen($commitSha = $this->getCommitSha())) $map->commit_sha = $commitSha;
    if ($runAt = $this->getRunAt()) $map->run_at = $runAt->format('c');

    return $map;
  }
800361
public static function json_encode(array $data)
    {
        $json = @json_encode($data);
        if (json_last_error() !== JSON_ERROR_NONE) {
            throw new \Exception(
                sprintf('An error occurred while trying to encode data to JSON [code "%s"]!', json_last_error())
            );
        } else {
            $data = $json;
        }
        return $data;
    }
538636
static function json_encode($arr) {
        if (count($arr) == 0) return "[]";
        $parts = array ();
        $is_list = false;
        //Find out if the given array is a numerical array
        $keys = array_keys ( $arr );
        $max_length = count ( $arr ) - 1;
        if (($keys [0] === 0) && ($keys [$max_length] === $max_length )) { //See if the first key is 0 and last key is length - 1
            $is_list = true;
            for($i = 0; $i < count ( $keys ); $i ++) { //See if each key correspondes to its position
                if ($i != $keys [$i]) { //A key fails at position check.
                    $is_list = false; //It is an associative array.
                    break;
                }
            }
        }
        foreach ( $arr as $key => $value ) {
            if (is_array ( $value )) { //Custom handling for arrays
                if ($is_list)
                    $parts [] = self::json_encode ( $value ); /* :RECURSION: */
                    else
                        $parts [] = '"' . $key . '":' . self::json_encode ( $value ); /* :RECURSION: */
            } else {
                $str = '';
                if (! $is_list)
                    $str = '"' . $key . '":';
                    //Custom handling for multiple data types
                    if (!is_string ( $value ) && is_numeric ( $value ) && $value<2000000000)
                        $str .= $value; //Numbers
                        elseif ($value === false)
                        $str .= 'false'; //The booleans
                        elseif ($value === true)
                        $str .= 'true';
                        else
                            $str .= '"' . addslashes ( $value ) . '"'; //All other things
                            // :TODO: Is there any more datatype we should be in the lookout for? (Object?)
                            $parts [] = $str;
            }
        }
        $json = implode ( ',', $parts );
        if ($is_list)
            return '[' . $json . ']'; //Return numerical JSON
            return '{' . $json . '}'; //Return associative JSON
    }
568641
public function urlencode_array(array $ar) {
		$datastring = '';
		foreach ($ar as $key => $val) {
			if (!empty($datastring)) {
				$datastring .= '&';
			}
			$datastring .= urlencode($key).'='.urlencode($val);
		}
		return $datastring;
	}
466936
function decode($json, $assoc = true, $depth = 512, $options = 0) {
        if (array_contains(['', null], $json)) {
            return null;
        }

        $data = @json_decode($json, $assoc, $depth, $options);
        
        if ($data === null && json_last_error() !== JSON_ERROR_NONE) {
            throw new JsonDecodeException(json_last_error_msg(), $json);
        }

        return $data;
    }
501579
public function json($arr, $out = true)
    {
        $this->setHeader('Content-type', 'application/json');
        $res = json_encode($arr, JSON_UNESCAPED_UNICODE);
        if ($out) {
            $this->send($res);
            return "";
        }
        return $res;
    }
820707
public static function decode($string, $asArray = false)
    {
        $decoded = self::safeDecode($string, $asArray);
        if (is_null($decoded) == false) {
            return $decoded;
        }
        $code = self::lastError();
        if ($code === JSON_ERROR_NONE) {
            return $decoded;
        }
        throw new JsonDecodeException(self::lastErrorMessage(), $code);
    }
894568
public function encode($data): string
    {
        $encoded = @json_encode($data, $this->getEncodeOptions());

        if ($encoded === false && json_last_error() !== JSON_ERROR_NONE) {
            throw new \RuntimeException('Encoding JSON failed: ' . json_last_error_msg());
        }

        return $encoded;
    }
734765
public function requestServerData($uri, $postData = [])
    {
        $result = Http::post($this->createServerUrl($uri), function ($http) use ($postData) {
            $this->applyHttpAttributes($http, $postData);
        });

        if ($result->code == 404) {
            throw new ApplicationException(Lang::get('system::lang.server.response_not_found'));
        }

        if ($result->code != 200) {
            throw new ApplicationException(
                strlen($result->body)
                ? $result->body
                : Lang::get('system::lang.server.response_empty')
            );
        }

        $resultData = false;

        try {
            $resultData = @json_decode($result->body, true);
        }
        catch (Exception $ex) {
            throw new ApplicationException(Lang::get('system::lang.server.response_invalid'));
        }

        if ($resultData === false || (is_string($resultData) && !strlen($resultData))) {
            throw new ApplicationException(Lang::get('system::lang.server.response_invalid'));
        }

        return $resultData;
    }
1198510
public static function jsonValidate($jsonString, $asArray)
    {
        $json = json_decode($jsonString, $asArray);

        if (json_last_error() != JSON_ERROR_NONE) {
            throw new InvalidJsonException(json_last_error_msg(), json_last_error());
        }

        return $json;
    }
597154
private function jsonDecode($serialized_data)
    {
        $data = json_decode($serialized_data, true);

        if (json_last_error()) {
            $error_message = 'Failed to parse JSON';

            if (function_exists('json_last_error_msg')) {
                $error_message .= '. Reason: ' . json_last_error_msg();
            }

            throw new RuntimeException($error_message);
        }

        return $data;
    }
309369
static public function toJsRaw($data, $stripslashes=true, $level=0) 
    {
        $str = $data;
        
        if (is_array($data)) {
            $space = $level ? str_pad(' ', $level * 4) : '';
            $bstr  = $space . '{';
            $estr  = $space . '}';
            $str   = '';
            
            if($stripslashes) {
                $data = static::stripslashes($data);
            }
            
            foreach ($data as $key=>$val) {
                if (is_numeric($key)) {
                    $bstr = $space . '[';
                    $estr = $space . ']';
                } else {
                    $str .= $space . "    \"$key\":";
                }
                
                if (is_numeric($val)) {
                    $str .= $val;
                } elseif (is_array($val)) {
                    $str .= static::toJsRaw($val, false, $level+1);
                } else {
                    $str .= "\"$val\"";
                }
                
                $str .= ',' . PHP_EOL;
            }
            
            $str = $bstr . PHP_EOL . rtrim($str, ',' . PHP_EOL) . PHP_EOL . $estr;
        }
        
        return $str;
    }
830840
public static function jsonEncode($data)
    {
        $json = json_encode($data);
        // if json_encode failed
        if ($json === false) {
            self::jsonLastError();
        }
        return $json;
    }
1038732
protected function encodeData($serializedData)
    {
        $result = json_encode($serializedData);
        if ($result === false) {
            $errorCode = json_last_error();
            throw new JsonException("Failed serialize json.", $errorCode);
        }

        return $result;
    }
205491
public static function getRawData($data_str)
    {
        if (is_string($data_str) && ($data_str=="''" || $data_str=='""')) {
            $data = '';
        } else {
            $data = $data_str;
        }

        if (!empty($data) && ($data{0} == '{' || $data{0} == '[')) {
            try {
                $data = self::json_decode($data);
            } catch (\Exception $e) {
                throw $e;
            }
        }

        return $data;
    }