src/JsonDecoder.php
public function decode($json, $schema = null)
    {
        if (self::ASSOC_ARRAY === $this->objectDecoding && null !== $schema) {
            throw new \InvalidArgumentException(
                'Schema validation is not supported when objects are decoded '.
                'as associative arrays. Call '.
                'JsonDecoder::setObjectDecoding(JsonDecoder::JSON_OBJECT) to fix.'
            );
        }

        $decoded = $this->decodeJson($json);

        if (null !== $schema) {
            $errors = $this->validator->validate($decoded, $schema);

            if (count($errors) > 0) {
                throw ValidationFailedException::fromErrors($errors);
            }
        }

        return $decoded;
    }
public function decode($json, $schema = null)
    {
        if (self::ASSOC_ARRAY === $this->objectDecoding && null !== $schema) {
            throw new \InvalidArgumentException(
                'Schema validation is not supported when objects are decoded '.
                'as associative arrays. Call '.
                'JsonDecoder::setObjectDecoding(JsonDecoder::JSON_OBJECT) to fix.'
            );
        }

        $decoded = $this->decodeJson($json);

        if (null !== $schema) {
            $errors = $this->validator->validate($decoded, $schema);

            if (count($errors) > 0) {
                throw ValidationFailedException::fromErrors($errors);
            }
        }

        return $decoded;
    }
public function decode ( $ json , $ schema = null ) { if ( self :: ASSOC_ARRAY === $ this -> objectDecoding && null !== $ schema ) { throw new \ InvalidArgumentException ( 'Schema validation is not supported when objects are decoded ' . 'as associative arrays. Call ' . 'JsonDecoder::setObjectDecoding(JsonDecoder::JSON_OBJECT) to fix.' ) ; } $ decoded = $ this -> decodeJson ( $ json ) ; if ( null !== $ schema ) { $ errors = $ this -> validator -> validate ( $ decoded , $ schema ) ; if ( count ( $ errors ) > 0 ) { throw ValidationFailedException :: fromErrors ( $ errors ) ; } } return $ decoded ; }
Decodes and validates a JSON string.

If a schema is passed, the decoded object is validated against that
schema. The schema may be passed as file path or as object returned from
`JsonDecoder::decodeFile($schemaFile)`.

You can adjust the decoding with {@link setObjectDecoding()},
{@link setBigIntDecoding()} and {@link setMaxDepth()}.

Schema validation is not supported when objects are decoded as
associative arrays.

@param string        $json   The JSON string
@param string|object $schema The schema file or object

@return mixed The decoded value

@throws DecodingFailedException   If the JSON string could not be decoded
@throws ValidationFailedException If the decoded string fails schema
validation
@throws InvalidSchemaException    If the schema is invalid
Decodes and validates a JSON string .