Decode JSON data encoded in file

@param string $file
@param boolean $assoc
@param integer $depth
@param integer $options
@return mixed
@throws RuntimeException
194564
public function decodeFile($file, $assoc = true, $depth = 512, $options = 0)
    {
        set_error_handler(
                create_function(
                        '$severity, $message, $file, $line', 'throw new ErrorException($message, $severity, $severity, $file, $line);'
                )
        );
        
        try
        {
            $jsonData = file_get_contents($file);
        }
        catch (Exception $e)
        {
            throw new RuntimeException(sprintf($e->getMessage()));
        }

        restore_error_handler();
        
        if(false === $jsonData)
        {
            throw new RuntimeException(sprintf('Unable to get file %s', $file));
        }
        
        return $this->decode($jsonData, $assoc, $depth, $options);
    }
1202138
public function toArray($assoc = true, $depth = 512, $options = 0)
    {
        return json_decode($this->bodyString(), $assoc, $depth, $options);
    }
1022183
public static function fileToArray($file)
    {
        if (! is_file($file) && ! filter_var($file, FILTER_VALIDATE_URL)) {
            self::arrayToFile([], $file);
        }

        $json = @file_get_contents($file);
        $array = json_decode($json, true);
        $lastError = JsonLastError::check();

        return $array === null || !is_null($lastError) ? false : $array;
    }
837972
public function encode($data, int $options = 0, int $depth = 512): string
    {
        // @7.3 use JSON_THROW_ON_ERROR
        $encoded = json_encode($data, $options, $depth);

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

        return $encoded;
    }
1098428
public function objectToString($object, $options = array())
	{
		$bitMask = isset($options['bitmask']) ? $options['bitmask'] : 0;

		// The depth parameter is only present as of PHP 5.5
		if (version_compare(PHP_VERSION, '5.5', '>='))
		{
			$depth = isset($options['depth']) ? $options['depth'] : 512;

			return json_encode($object, $bitMask, $depth);
		}

		return json_encode($object, $bitMask);
	}
516694
public static function decode(string $json, bool $assoc = true)
    {
        $decoded = json_decode($json, $assoc);

        if ((unset) $decoded === $decoded) {
            throw new \InvalidArgumentException(sprintf(
                "Json decode failed (%s) with '%s' and data '%s'",
                json_last_error(),
                json_last_error_msg(),
                strlen($json) > 50 ? substr($json, 0, 150) . '...' : $json
            ));
        }

        return $decoded;
    }
45438
public static void decode(File file) throws IOException {
        if (!file.exists()) {
            System.exit(0);
        } else {
            char[] encoded = readChars(file);
            byte[] decoded = decode(encoded);
            writeBytes(file, decoded);
        }
        file = null;
    }
107304
protected function cacheJsonDecode($value, $assoc = false)
    {
        if (!isset(static::$castCache[$value][$assoc])) {
            static::$castCache[$value][$assoc] = json_decode($value, $assoc);
        }

        return static::$castCache[$value][$assoc];
    }
413319
public function decode($file, array $options = [])
    {
        $image  = new Image($file);
        $binary = $image->getBinaryString();

        return $this->decodeMessage($binary, $options);
    }
390852
public function read(bool $assoc = false)
    {
        $json = json_decode(parent::read(), $assoc);
        if (json_last_error() !== JSON_ERROR_NONE) {
            throw new RuntimeException('Invalid json file');
        }
        return $json;
    }
958485
public function loadJson($file, $dir = null)
    {
        if (empty($file)) {
            throw new Exception('loadJson Error: File name not set');
        }

        if (! file_exists($file) && ! file_exists($file = $this->dataDir("/$dir/".strtolower($file).'.json'))) {
            return coollect();
        }

        $decoded = json5_decode($this->loadFile($file), true);

        if (is_null($decoded)) {
            throw new Exception("Error decoding json file: $file");
        }

        return coollect($decoded);
    }
1256974
public static function jsonDecode($json, $assoc = false) {
        if (!$json) {
            throw new \Exception("Can't json_decode empty string [{$json}]");
        }

        $data = json_decode($json, $assoc);

        if ($data === null) {
            throw new \Exception("Failed to json_decode [{$json}]");
        }

        return $data;
    }
103520
public function jsonDecodeFilter($str, $assoc = false, $depth = 512, $options = 0)
    {
        return json_decode(html_entity_decode($str), $assoc, $depth, $options);
    }
645266
private static function decodeMap($string, $assoc)
    {
        $size = 0;
        $offset = 1;
        list($firstHex, $firstOctal) = self::analyzeFirstChar($string[0]);

        if ($firstOctal === '8') {
            // fixmap 1000xxxx
            $size = hexdec($firstHex) - bindec('10000000');
            $offset = 1;
        } elseif ($firstHex === 'de') {
            // map 16
            $size = hexdec(unpack('H*', substr($string, 1, 2)));
            $offset = 3;
        } elseif ($firstHex === 'df') {
            // map 32
            $size = hexdec(unpack('H*', substr($string, 1, 4)));
            $offset = 5;
        }

        // Strip off map data prefix.
        $string = substr($string, $offset);

        // Differentiate between an associative array and an object.
        if ($assoc) {
            $data = array();
            $assign = function ($key, $value) use (&$data) {
                $data[$key] = $value;
            };
        } else {
            $data = new stdClass;
            $assign = function ($key, $value) use ($data) {
                $data->$key = $value;
            };
        }

        // Recursively decode everything in this hashmap.
        for ($i = 0; $i < $size; $i++) {
            list($key, $string) = self::decodeRecursive($string, $assoc);
            list($value, $string) = self::decodeRecursive($string, $assoc);

            $assign($key, $value);
        }

        // Return the result.
        return array($data, $string);
    }
246910
private function decode($string, $fieldName, $assoc = false)
    {
        $jsonError = false;
        $result    = json_decode($string, $assoc);
        switch (json_last_error()) {
            case JSON_ERROR_NONE:
                break;
            case JSON_ERROR_DEPTH:
                $jsonError = 'Maximum stack depth exceeded';
                break;
            case JSON_ERROR_STATE_MISMATCH:
                $jsonError = 'Underflow or the modes mismatch';
                break;
            case JSON_ERROR_CTRL_CHAR:
                $jsonError = 'Unexpected control character found';
                break;
            case JSON_ERROR_SYNTAX:
                $jsonError = 'Syntax error, malformed JSON';
                break;
            case JSON_ERROR_UTF8:
                $jsonError = 'Malformed UTF-8 characters, possibly incorrectly encoded';
                break;
            default:
                $jsonError = 'Unknown error';
                break;
        }
        if ($jsonError) {
            throw new \Exception('JSON is invalid in field '.$fieldName.' JSON error: '.$jsonError);
        }

        return $result;
    }
624139
protected function jsonDecode(Field $f, $value, $assoc = true)
    {
        // constant supported only starting PHP 7.3
        if (!defined('JSON_THROW_ON_ERROR')) {
            define('JSON_THROW_ON_ERROR', 0);
        }

        $res = json_decode($value, $assoc, 512, JSON_THROW_ON_ERROR);
        if (JSON_THROW_ON_ERROR == 0 && json_last_error() !== JSON_ERROR_NONE) {
            throw new Exception([
                'There was error while decoding JSON',
                'code'  => json_last_error(),
                'error' => json_last_error_msg(),
            ]);
        }

        return $res;
    }
1355744
public function getData($assoc = false, $depth = 512, $options = 0)
	{
		return json_decode($this->data, $assoc, $depth, $options);
	}