MediaWiki master
MediaWiki\Json\JsonCodec Class Reference

Inherits MediaWiki\Json\JsonDeserializer, and MediaWiki\Json\JsonSerializer.

Collaboration diagram for MediaWiki\Json\JsonCodec:

Public Member Functions

 deserialize ( $json, string $expectedClass=null)
 Restore an instance of simple type or JsonDeserializable subclass from the JSON serialization.
 
 deserializeArray (array $array)
 Helper to deserialize an array of JsonDeserializable instances or simple types.
 
 detectNonSerializableData ( $value, bool $expectDeserialize=false)
 Checks if the $value is JSON-serializable (contains only scalar values) and returns a JSON-path to the first non-serializable property encountered.
 
 serialize ( $value)
 Encode $value as JSON with an intent to use JsonDeserializer::unserialize to decode it back.
 
 unserialize ( $json, ?string $expectedClass=null)
 
 unserializeArray (array $array)
 
- Public Member Functions inherited from MediaWiki\Json\JsonDeserializer
 unserialize ( $json, string $expectedClass=null)
 Backwards-compatibility alias for deserialize()
 

Detailed Description

Definition at line 37 of file JsonCodec.php.

Member Function Documentation

◆ deserialize()

MediaWiki\Json\JsonCodec::deserialize ( $json,
string $expectedClass = null )

Restore an instance of simple type or JsonDeserializable subclass from the JSON serialization.

It supports passing array/object to allow manual decoding of the JSON string if needed.

Note
JSON objects are unconditionally deserialized as PHP associative arrays, and not as instances of \stdClass.

@phpcs:ignore MediaWiki.Commenting.FunctionComment.ObjectTypeHintParam

Parameters
array | string | object$json
string | null$expectedClassWhat class to expect in deserialization. If null, no expectation. Must be a descendant of JsonDeserializable.
Exceptions
InvalidArgumentExceptionif the passed $json can't be deserialized.
Returns
mixed

Implements MediaWiki\Json\JsonDeserializer.

Definition at line 44 of file JsonCodec.php.

References MediaWiki\Json\JsonCodec\deserializeArray(), MediaWiki\Json\FormatJson\FORCE_ASSOC, MediaWiki\Json\FormatJson\parse(), and MediaWiki\Json\JsonConstants\TYPE_ANNOTATION.

Referenced by BenchmarkJsonCodec\execute(), and MediaWiki\Json\JsonCodec\unserialize().

◆ deserializeArray()

MediaWiki\Json\JsonCodec::deserializeArray ( array $array)

Helper to deserialize an array of JsonDeserializable instances or simple types.

Parameters
array$array
Returns
array

Implements MediaWiki\Json\JsonDeserializer.

Definition at line 102 of file JsonCodec.php.

References MediaWiki\Json\JsonConstants\COMPLEX_ANNOTATION.

Referenced by MediaWiki\Json\JsonCodec\deserialize().

◆ detectNonSerializableData()

MediaWiki\Json\JsonCodec::detectNonSerializableData ( $value,
bool $expectDeserialize = false )

Checks if the $value is JSON-serializable (contains only scalar values) and returns a JSON-path to the first non-serializable property encountered.

Parameters
mixed$value
bool$expectDeserializewhether to expect the $value to be deserializable with JsonDeserializer.
Returns
string|null JSON path to first encountered non-serializable property or null.
See also
JsonDeserializer
Since
1.36

Definition at line 305 of file JsonCodec.php.

Referenced by BenchmarkJsonCodec\execute().

◆ serialize()

MediaWiki\Json\JsonCodec::serialize ( $value)

Encode $value as JSON with an intent to use JsonDeserializer::unserialize to decode it back.

Parameters
mixed | JsonDeserializable$valueA value to encode. Can be any scalar, array, stdClass, JsonDeserializable or any combination of them.
Exceptions
InvalidArgumentExceptionif the value can not be serialized.
Returns
string

Implements MediaWiki\Json\JsonSerializer.

Definition at line 161 of file JsonCodec.php.

Referenced by BenchmarkJsonCodec\execute().

◆ unserialize()

MediaWiki\Json\JsonCodec::unserialize ( $json,
?string $expectedClass = null )
Deprecated
since 1.43; use ::deserialize()

Definition at line 40 of file JsonCodec.php.

References MediaWiki\Json\JsonCodec\deserialize().

◆ unserializeArray()

MediaWiki\Json\JsonCodec::unserializeArray ( array $array)
Deprecated
since 1.43; use ::deserializeArray()

Implements MediaWiki\Json\JsonDeserializer.

Definition at line 98 of file JsonCodec.php.


The documentation for this class was generated from the following file: