Transform a JSON error response into a string message
@param $jsonDecodedObject
@return null|string
328899
private function transformErrorToMessage($jsonDecodedObject)
    {
        if (property_exists($jsonDecodedObject,"error"))
        {
            $message = $jsonDecodedObject->error->message;
            if (!is_object($message))
            {
                return $message;
            }
            else
            {
                $messageArray = [];
                foreach ($message as $key => $value)
                {
                    array_push($messageArray,$value);
                }
                return implode(", ",$messageArray);
            }
        }
        return null;
    }
620798
protected function performJsonEncoding($content, array $jsonEncodeOptions = [])
    {
        $jsonEncodeOptions = $this->filterJsonEncodeOptions($jsonEncodeOptions);

        $optionsBitmask = $this->calucateJsonEncodeOptionsBitmask($jsonEncodeOptions);

        if (($encodedString = json_encode($content, $optionsBitmask)) === false) {
            throw new \ErrorException('Error encoding data in JSON format: '.json_last_error());
        }

        return $encodedString;
    }
1272112
public static function decode(string $json)
    {
        $value = json_decode($json, true, 512, JSON_BIGINT_AS_STRING);

        if ($error = json_last_error()) {
            throw new JsonException(json_last_error_msg(), $error);
        }

        return $value;
    }
211040
public static function getJSONErrorAsString()
	{
		switch(json_last_error())
		{
			case JSON_ERROR_NONE:
				$error =  NULL;
			break;
			
			case JSON_ERROR_DEPTH:
				$error = 'The maximum stack depth has been exceeded.';
			break;
				
			case JSON_ERROR_STATE_MISMATCH:
				$error = 'Invalid or malformed JSON.';
			break;

			case JSON_ERROR_CTRL_CHAR:
				$error = 'Control character error, possibly incorrectly encoded.';
			break;

			case JSON_ERROR_SYNTAX:
				$error = 'Syntax error, malformed JSON.';
			break;

			// PHP >= 5.3.3
			case JSON_ERROR_UTF8:
				$error = 'Malformed UTF-8 characters, possibly incorrectly encoded.';
			break;

			// PHP >= 5.5.0
			case JSON_ERROR_RECURSION:
				$error = 'One or more recursive references in the value to be encoded.';
			break;

			// PHP >= 5.5.0
			case JSON_ERROR_INF_OR_NAN:
				$error = 'One or more NAN or INF values in the value to be encoded.';
			break;

			case JSON_ERROR_UNSUPPORTED_TYPE:
				$error = 'A value of a type that cannot be encoded was given.';
			break;

			default:
				$error = NULL;
			break;
		}
		
		return $error;
	}
725681
function JSON($object = null) {
		header("Expires: Mon, 26 Jul 1997 05:00:00 GMT" ); 
		header("Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . "GMT" ); 
		header("Cache-Control: no-cache, must-revalidate" ); 
		header("Pragma: no-cache" );
		header('Content-type: application/json');

		if (is_array($object)) {
			echo json_encode($object, $this->JSONOptions);
		} else if (is_bool($object)) {
			echo 0;
		} else if (is_string($object)) {
			echo $object;
		} else if (is_null($object)) {
			echo 'null';
		} else {
			die('Unknown object type to convert into JSON: ' . gettype($object));
		}

		if ($this->fatal)
			exit;
	}
786851
public function getErrorMessage()
    {
        if (is_string($this->jsonError)) {
            return $this->jsonError;
        } elseif (isset($this->jsonError['message'])) {
            return $this->jsonError['message'];
        }
        return '';
    }
946984
public static function decodeHttpResponse($response) {
    $code = $response->getResponseHttpCode();
    $body = $response->getResponseBody();
    $decoded = null;
    
    if ((intVal($code)) >= 300) {
      $decoded = json_decode($body, true);
      $err = 'Error calling ' . $response->getRequestMethod() . ' ' . $response->getUrl();
      if ($decoded != null && isset($decoded['error']['message'])  && isset($decoded['error']['code'])) {
        // if we're getting a json encoded error definition, use that instead of the raw response
        // body for improved readability
        $err .= ": ({$decoded['error']['code']}) {$decoded['error']['message']}";
      } else {
        $err .= ": ($code) $body";
      }

      throw new ServiceException($err, $code, null, $decoded['error']['errors']);
    }
    
    // Only attempt to decode the response, if the response code wasn't (204) 'no content'
    if ($code != '204') {
      $decoded = json_decode($body, true);
      if ($decoded === null || $decoded === "") {
        throw new ServiceException("Invalid json in service response: $body");
      }
    }
    return $decoded;
  }
583612
private function decodeResponse(ResponseInterface $response)
    {
        $string = (string) $response->getBody();
        $data = json_decode($string, true);

        if (JSON_ERROR_NONE !== json_last_error()) {
            throw new JsonDecodeException($string);
        }

        return $data;
    }
1292541
protected function decodeData(ResponseObject $response)
    {
        $resultArray = json_decode($response->getBody(), true);
        if ($resultArray === null) {
            throw new JsonException('Failed to decode response', json_last_error());
        }

        return $resultArray;
    }
703314
private static function getErrorMessage($response)
    {
        $body = $response->getBody();

        $message = json_decode($body, true);
        $errors = [];

        if (is_array($message) && isset($message['errors'])) {
            $errors = $message['errors'];
        }

        if ($body == self::RATE_LIMIT_ERROR_MESSAGE) {
            $errors = [self::RATE_LIMIT_ERROR_MESSAGE];
        }

        return $errors;
    }
383034
public function jsonEncode($Object, $skipObjectEncode = false)
    {
        if (!$skipObjectEncode) {
            $Object = $this->encodeObject($Object);
        }
        
        if (function_exists('json_encode')
           && $this->options['useNativeJsonEncode']!=false) {
    
            return json_encode($Object);
        } else {
            return $this->json_encode($Object);
        }
    }
969880
public function jsonDecode()
    {
        $json = json_decode($this->body, true);
        if ($json===null){
            throw new JsonFormatException($this->body);
        }
        return $json;
    }
802749
protected function buildErrorResponse($error, $code = null)
    {
        if ($code !== null && (!is_int($code) || $code < 400)) {
            $code = null;
        }
        if (is_object($error)) {
            if ($error instanceof AccessDeniedException) {
                $error = $error->getMessage();
                if ($code === null) {
                    $code = Response::HTTP_UNAUTHORIZED;
                }
            } elseif ($error instanceof UserMessageException) {
                if ($error->getCode() >= 400) {
                    $code = $error->getCode();
                }
                $error = $error->getMessage();
            } elseif ($error instanceof Exception || $error instanceof Throwable) {
                $error = t('An unexpected error occurred');
            } elseif (is_callable([$error, '__toString'])) {
                $error = (string) $error;
            } else {
                $error = get_class($error);
            }
        }
        if ($code === null) {
            $code = Response::HTTP_INTERNAL_SERVER_ERROR;
        }

        return $this->getResponseFactory()->create(
            $error,
            $code,
            [
                'Content-Type' => 'text/plain; charset=UTF-8',
            ]
        );
    }
1159557
public static function jsonEncode( $obj )
    {
        if ( self::$nativeJsonEncode === null )
            self::$nativeJsonEncode = function_exists( 'json_encode' );

        if ( self::$nativeJsonEncode === true )
            return json_encode( $obj );

        $inst = self::getInstance();
        return $inst->phpJsonEncode( $obj );    
    }
682420
public static JSONObject processBadRespStr(int responseCode, String responseMsg) throws JSONException {

        JSONObject response = new JSONObject();
        response.put("responseCode", responseCode);
        if (responseMsg.equalsIgnoreCase("")) { // good response is empty string
            response.put("responseMsg", "");
        } else { // bad response is json string
            JSONObject errorObject = new JSONObject(responseMsg).optJSONObject("odata.error");

            String errorCode = errorObject.optString("code");
            String errorMsg = errorObject.optJSONObject("message").optString("value");
            response.put("responseCode", responseCode);
            response.put("errorCode", errorCode);
            response.put("errorMsg", errorMsg);
        }

        return response;
    }
702370
public function getErrorMessage()
    {
        if (array_key_exists('error', $this->responseData?: [])) {
            if (is_string($this->responseData['error'])) {
                return json_encode($this->responseData['error']);
            } elseif(array_key_exists('error', $this->responseData['error'])) {
                return json_encode($this->responseData['error']['error']);
            } else {
                return json_encode($this->responseData['error']);
            }
        } else {
            return null;
        }
    }
488575
public function getJsonResponse()
    {
        $json = json_decode($this->httpResponse);
        if (is_null($json) && json_last_error() != JSON_ERROR_NONE) {
            return null; // decode error
        } else {
            return is_object($json) ? $json : null;
        }
    }