src/JsonDecoder.php
public function decodeFile($path, $schema = null)
    {
        if (!file_exists($path)) {
            throw new FileNotFoundException(sprintf(
                'The file %s does not exist.',
                $path
            ));
        }

        $errorMessage = null;
        $errorCode = 0;

        set_error_handler(function ($errno, $errstr) use (&$errorMessage, &$errorCode) {
            $errorMessage = $errstr;
            $errorCode = $errno;
        });

        $content = file_get_contents($path);

        restore_error_handler();

        if (null !== $errorMessage) {
            if (false !== $pos = strpos($errorMessage, '): ')) {
                // cut "file_get_contents(%path%):" to make message more readable
                $errorMessage = substr($errorMessage, $pos + 3);
            }

            throw new IOException(sprintf(
                'Could not read %s: %s (%s)',
                $path,
                $errorMessage,
                $errorCode
            ), $errorCode);
        }

        try {
            return $this->decode($content, $schema);
        } catch (DecodingFailedException $e) {
            // Add the file name to the exception
            throw new DecodingFailedException(sprintf(
                'An error happened while decoding %s: %s',
                $path,
                $e->getMessage()
            ), $e->getCode(), $e);
        } catch (ValidationFailedException $e) {
            // Add the file name to the exception
            throw new ValidationFailedException(sprintf(
                "Validation of %s failed:\n%s",
                $path,
                $e->getErrorsAsString()
            ), $e->getErrors(), $e->getCode(), $e);
        } catch (InvalidSchemaException $e) {
            // Add the file name to the exception
            throw new InvalidSchemaException(sprintf(
                'An error happened while decoding %s: %s',
                $path,
                $e->getMessage()
            ), $e->getCode(), $e);
        }
    }
public function decodeFile($path, $schema = null)
    {
        if (!file_exists($path)) {
            throw new FileNotFoundException(sprintf(
                'The file %s does not exist.',
                $path
            ));
        }

        $errorMessage = null;
        $errorCode = 0;

        set_error_handler(function ($errno, $errstr) use (&$errorMessage, &$errorCode) {
            $errorMessage = $errstr;
            $errorCode = $errno;
        });

        $content = file_get_contents($path);

        restore_error_handler();

        if (null !== $errorMessage) {
            if (false !== $pos = strpos($errorMessage, '): ')) {
                // cut "file_get_contents(%path%):" to make message more readable
                $errorMessage = substr($errorMessage, $pos + 3);
            }

            throw new IOException(sprintf(
                'Could not read %s: %s (%s)',
                $path,
                $errorMessage,
                $errorCode
            ), $errorCode);
        }

        try {
            return $this->decode($content, $schema);
        } catch (DecodingFailedException $e) {
            // Add the file name to the exception
            throw new DecodingFailedException(sprintf(
                'An error happened while decoding %s: %s',
                $path,
                $e->getMessage()
            ), $e->getCode(), $e);
        } catch (ValidationFailedException $e) {
            // Add the file name to the exception
            throw new ValidationFailedException(sprintf(
                "Validation of %s failed:\n%s",
                $path,
                $e->getErrorsAsString()
            ), $e->getErrors(), $e->getCode(), $e);
        } catch (InvalidSchemaException $e) {
            // Add the file name to the exception
            throw new InvalidSchemaException(sprintf(
                'An error happened while decoding %s: %s',
                $path,
                $e->getMessage()
            ), $e->getCode(), $e);
        }
    }
public function decodeFile ( $ path , $ schema = null ) { if ( ! file_exists ( $ path ) ) { throw new FileNotFoundException ( sprintf ( 'The file %s does not exist.' , $ path ) ) ; } $ errorMessage = null ; $ errorCode = 0 ; set_error_handler ( function ( $ errno , $ errstr ) use ( & $ errorMessage , & $ errorCode ) { $ errorMessage = $ errstr ; $ errorCode = $ errno ; } ) ; $ content = file_get_contents ( $ path ) ; restore_error_handler ( ) ; if ( null !== $ errorMessage ) { if ( false !== $ pos = strpos ( $ errorMessage , '): ' ) ) { // cut "file_get_contents(%path%):" to make message more readable $ errorMessage = substr ( $ errorMessage , $ pos + 3 ) ; } throw new IOException ( sprintf ( 'Could not read %s: %s (%s)' , $ path , $ errorMessage , $ errorCode ) , $ errorCode ) ; } try { return $ this -> decode ( $ content , $ schema ) ; } catch ( DecodingFailedException $ e ) { // Add the file name to the exception throw new DecodingFailedException ( sprintf ( 'An error happened while decoding %s: %s' , $ path , $ e -> getMessage ( ) ) , $ e -> getCode ( ) , $ e ) ; } catch ( ValidationFailedException $ e ) { // Add the file name to the exception throw new ValidationFailedException ( sprintf ( "Validation of %s failed:\n%s" , $ path , $ e -> getErrorsAsString ( ) ) , $ e -> getErrors ( ) , $ e -> getCode ( ) , $ e ) ; } catch ( InvalidSchemaException $ e ) { // Add the file name to the exception throw new InvalidSchemaException ( sprintf ( 'An error happened while decoding %s: %s' , $ path , $ e -> getMessage ( ) ) , $ e -> getCode ( ) , $ e ) ; } }
Decodes and validates a JSON file.

@param string        $path   The path to the JSON file
@param string|object $schema The schema file or object

@return mixed The decoded file

@throws FileNotFoundException     If the file was not found
@throws DecodingFailedException   If the file could not be decoded
@throws ValidationFailedException If the decoded file fails schema
validation
@throws InvalidSchemaException    If the schema is invalid

@see decode
Decodes and validates a JSON file .