MediaWiki master
MediaWiki\Json\JsonCodec Class Reference

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

Collaboration diagram for MediaWiki\Json\JsonCodec:

Public Member Functions

 __construct (?ContainerInterface $services=null)
 Create a new JsonCodec, with optional access to the provided services.
 
 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)
 

Protected Member Functions

 codecFor (string $className)
 Support the JsonCodecable interface by maintaining a mapping of class names to codecs.
 
 isArrayMarked (array $value)
 
 markArray (array &$value, string $className, ?string $classHint)
 
 unmarkArray (array &$value, ?string $classHint)
 

Detailed Description

Definition at line 42 of file JsonCodec.php.

Constructor & Destructor Documentation

◆ __construct()

MediaWiki\Json\JsonCodec::__construct ( ?ContainerInterface $services = null)

Create a new JsonCodec, with optional access to the provided services.

Definition at line 57 of file JsonCodec.php.

Member Function Documentation

◆ codecFor()

MediaWiki\Json\JsonCodec::codecFor ( string $className)
protected

Support the JsonCodecable interface by maintaining a mapping of class names to codecs.

Parameters
class-string$className
Returns
?JsonClassCodec

Definition at line 67 of file JsonCodec.php.

◆ 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 145 of file JsonCodec.php.

Referenced by BenchmarkJsonCodec\execute().

◆ 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 196 of file JsonCodec.php.

◆ 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 the first encountered non-serializable property or null.
See also
JsonDeserializer
Since
1.36

Definition at line 327 of file JsonCodec.php.

Referenced by BenchmarkJsonCodec\execute().

◆ isArrayMarked()

MediaWiki\Json\JsonCodec::isArrayMarked ( array $value)
protected

Definition at line 108 of file JsonCodec.php.

◆ markArray()

MediaWiki\Json\JsonCodec::markArray ( array & $value,
string $className,
?string $classHint )
protected

◆ 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 206 of file JsonCodec.php.

Referenced by BenchmarkJsonCodec\execute().

◆ unmarkArray()

MediaWiki\Json\JsonCodec::unmarkArray ( array & $value,
?string $classHint )
protected

Definition at line 121 of file JsonCodec.php.

◆ unserialize()

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

Implements MediaWiki\Json\JsonDeserializer.

Definition at line 141 of file JsonCodec.php.

◆ unserializeArray()

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

Implements MediaWiki\Json\JsonDeserializer.

Definition at line 192 of file JsonCodec.php.


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