MediaWiki  master
MediaWiki\Json\JsonCodec Class Reference
Inheritance diagram for MediaWiki\Json\JsonCodec:
Collaboration diagram for MediaWiki\Json\JsonCodec:

Public Member Functions

 detectNonSerializableData ( $value, bool $expectUnserialize=false)
 Checks if the $value is JSON-serializable (contains only scalar values) and returns a JSON-path to the first non-serializable property encountered. More...
 
 serialize ( $value)
 Encode $value as JSON with an intent to use JsonUnserializer::unserialize to decode it back. More...
 
 unserialize ( $json, string $expectedClass=null)
 Restore an instance of simple type or JsonUnserializable subclass from the JSON serialization. More...
 
 unserializeArray (array $array)
 Helper to unserialize an array of JsonUnserializable instances or simple types. More...
 

Private Member Functions

 canMakeNewFromValue ( $json)
 Is it likely possible to make a new instance from $json serialization? More...
 
 detectNonSerializableDataInternal ( $value, bool $expectUnserialize, string $accumulatedPath)
 Recursive check for ability to serialize $value to JSON via FormatJson::encode(). More...
 

Detailed Description

Definition at line 36 of file JsonCodec.php.

Member Function Documentation

◆ canMakeNewFromValue()

MediaWiki\Json\JsonCodec::canMakeNewFromValue (   $json)
private

Is it likely possible to make a new instance from $json serialization?

Parameters
mixed$json
Returns
bool

Definition at line 122 of file JsonCodec.php.

References MediaWiki\Json\JsonConstants\TYPE_ANNOTATION.

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

◆ detectNonSerializableData()

MediaWiki\Json\JsonCodec::detectNonSerializableData (   $value,
bool  $expectUnserialize = 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$expectUnserializewhether to expect the $value to be unserializable with JsonUnserializer.
Returns
string|null JSON path to first encountered non-serializable property or null.
See also
JsonUnserializer
Since
1.36

Definition at line 181 of file JsonCodec.php.

References MediaWiki\Json\JsonCodec\detectNonSerializableDataInternal().

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

◆ detectNonSerializableDataInternal()

MediaWiki\Json\JsonCodec::detectNonSerializableDataInternal (   $value,
bool  $expectUnserialize,
string  $accumulatedPath 
)
private

Recursive check for ability to serialize $value to JSON via FormatJson::encode().

Parameters
mixed$value
bool$expectUnserialize
string$accumulatedPath
Returns
string|null JSON path to first encountered non-serializable property or null.

Definition at line 142 of file JsonCodec.php.

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

◆ serialize()

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

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

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

Implements MediaWiki\Json\JsonSerializer.

Definition at line 89 of file JsonCodec.php.

References FormatJson\ALL_OK, MediaWiki\Json\JsonCodec\detectNonSerializableData(), and FormatJson\encode().

◆ unserialize()

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

Restore an instance of simple type or JsonUnserializable 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 unserialized 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 unserialization. If null, no expectation. Must be a descendant of JsonUnserializable.
Exceptions
InvalidArgumentExceptionif the passed $json can't be unserialized.
Returns
mixed

Implements MediaWiki\Json\JsonUnserializer.

Definition at line 38 of file JsonCodec.php.

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

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

◆ unserializeArray()

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

Helper to unserialize an array of JsonUnserializable instances or simple types.

Parameters
array$array
Returns
array

Implements MediaWiki\Json\JsonUnserializer.

Definition at line 77 of file JsonCodec.php.

References MediaWiki\Json\JsonCodec\canMakeNewFromValue(), and MediaWiki\Json\JsonCodec\unserialize().


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