Overview

Namespaces

  • Webmozart
    • Json

Classes

  • JsonDecoder
  • JsonEncoder
  • JsonError
  • JsonValidator

Exceptions

  • DecodingFailedException
  • EncodingFailedException
  • FileNotFoundException
  • InvalidSchemaException
  • ValidationFailedException
  • Overview
  • Namespace
  • Class

Class JsonDecoder

Decodes JSON strings/files and validates against a JSON schema.

Namespace: Webmozart\Json
Author: Bernhard Schussek bschussek@gmail.com
Since: 1.0
Located at JsonDecoder.php
Methods summary
public
# __construct( )

Creates a new decoder.

Creates a new decoder.

public mixed
# decode( string $json, string|object $schema = null )

Decodes and validates a JSON string.

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 Webmozart\Json\JsonDecoder::setObjectDecoding(), Webmozart\Json\JsonDecoder::setBigIntDecoding() and Webmozart\Json\JsonDecoder::setMaxDepth().

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

Parameters

$json
The JSON string.
$schema
The schema file or object.

Returns

mixed
The decoded value.

Throws

Webmozart\Json\DecodingFailedException
If the JSON string could not be decoded.
Webmozart\Json\ValidationFailedException

If the decoded string fails schema validation.


Webmozart\Json\InvalidSchemaException
If the schema is invalid.
public mixed
# decodeFile( string $file, string|object $schema = null )

Decodes and validates a JSON file.

Decodes and validates a JSON file.

Parameters

$file
The path to the JSON file.
$schema
The schema file or object.

Returns

mixed
The decoded file.

Throws

Webmozart\Json\FileNotFoundException
If the file was not found.
Webmozart\Json\DecodingFailedException
If the file could not be decoded.
Webmozart\Json\ValidationFailedException

If the decoded file fails schema validation.


Webmozart\Json\InvalidSchemaException
If the schema is invalid.

See

Webmozart\Json\JsonDecoder::decode()
public integer
# getMaxDepth( )

Returns the maximum recursion depth.

Returns the maximum recursion depth.

A depth of zero means that objects are not allowed. A depth of one means only one level of objects or arrays is allowed.

Returns

integer
The maximum recursion depth.
public
# setMaxDepth( integer $maxDepth )

Sets the maximum recursion depth.

Sets the maximum recursion depth.

If the depth is exceeded during decoding, an DecodingnFailedException will be thrown.

A depth of zero means that objects are not allowed. A depth of one means only one level of objects or arrays is allowed.

Parameters

$maxDepth
The maximum recursion depth.

Throws

InvalidArgumentException

If the depth is not an integer greater than or equal to zero.

public integer
# getObjectDecoding( )

Returns the decoding of JSON objects.

Returns the decoding of JSON objects.

Returns

integer
One of the constants JSON_OBJECT and Webmozart\Json\JsonDecoder::ASSOC_ARRAY.
public
# setObjectDecoding( integer $decoding )

Sets the decoding of JSON objects.

Sets the decoding of JSON objects.

By default, JSON objects are decoded as instances of \stdClass.

Parameters

$decoding
One of the constants JSON_OBJECT and Webmozart\Json\JsonDecoder::ASSOC_ARRAY.

Throws

InvalidArgumentException
If the passed decoding is invalid.
public integer
# getBigIntDecoding( )

Returns the decoding of big integers.

Returns the decoding of big integers.

Returns

integer
One of the constants Webmozart\Json\JsonDecoder::FLOAT and JSON_STRING.
public
# setBigIntDecoding( integer $decoding )

Sets the decoding of big integers.

Sets the decoding of big integers.

By default, big integers are decoded as floats.

Parameters

$decoding
One of the constants Webmozart\Json\JsonDecoder::FLOAT and JSON_STRING.

Throws

InvalidArgumentException
If the passed decoding is invalid.
Constants summary
integer OBJECT

Decode a JSON value as PHP object.

Decode a JSON value as PHP object.

# 0
integer ASSOC_ARRAY

Decode a JSON value as associative array.

Decode a JSON value as associative array.

# 1
integer FLOAT

Decode a JSON value as float.

Decode a JSON value as float.

# 2
integer STRING

Decode a JSON value as string.

Decode a JSON value as string.

# 3
Webmozart JSON API API documentation generated by ApiGen