JsonCodec
Interfaces to serialize and deserialize PHP objects to/from JSON
Loading...
Searching...
No Matches
Wikimedia\JsonCodec\JsonClassCodec Interface Reference

Classes implementing this interface support round-trip JSON serialization/deserialization for certain class types. More...

+ Inheritance diagram for Wikimedia\JsonCodec\JsonClassCodec:

Public Member Functions

 toJsonArray ( $obj)
 Returns a JSON array representing the contents of the given object, that can be deserialized with the corresponding newFromJsonArray() method.
 
 newFromJsonArray (string $className, array $json)
 Creates a new instance of the given class and initializes it from the $json array.
 
 jsonClassHintFor (string $className, string $keyName)
 Return an optional type hint for the given array key in the result of ::toJsonArray() / input to ::newFromJsonArray.
 

Detailed Description

Classes implementing this interface support round-trip JSON serialization/deserialization for certain class types.

They may maintain state and/or consult service objects which are stored in the codec object.

@template T

Member Function Documentation

◆ jsonClassHintFor()

Wikimedia\JsonCodec\JsonClassCodec::jsonClassHintFor ( string $className,
string $keyName )

Return an optional type hint for the given array key in the result of ::toJsonArray() / input to ::newFromJsonArray.

If a class name is returned here and it matches the runtime type of the value of that array key, then type information will be omitted from the generated JSON which can save space. The class name can be suffixed with [] to indicate an array or list containing objects of the given class name.

Parameters
class-string<T>$className The class we're looking for a hint for
string$keyNameThe name of the array key we'd like a hint on
Returns
class-string|string|Hint|null A class string, Hint or null. For backward compatibility, a class string suffixed with [] can also be returned, but that is deprecated.

Implemented in Wikimedia\JsonCodec\JsonStaticClassCodec, and Wikimedia\JsonCodec\JsonStdClassCodec.

◆ newFromJsonArray()

Wikimedia\JsonCodec\JsonClassCodec::newFromJsonArray ( string $className,
array $json )

Creates a new instance of the given class and initializes it from the $json array.

Parameters
class-string<T>$className
array$json
Returns
T

Implemented in Wikimedia\JsonCodec\JsonStaticClassCodec, and Wikimedia\JsonCodec\JsonStdClassCodec.

◆ toJsonArray()

Wikimedia\JsonCodec\JsonClassCodec::toJsonArray ( $obj)

Returns a JSON array representing the contents of the given object, that can be deserialized with the corresponding newFromJsonArray() method.

The returned array can contain other JsonCodecables as values; the JsonCodec class will take care of encoding values in the array as needed, as well as annotating the returned array with the class information needed to locate the correct ::newFromJsonArray() method during deserialization.

Only objects of the types registered to this JsonClassCodec will be provided to this method.

Parameters
T$objAn object of the type handled by this JsonClassCodec
Returns
array A Json representation of the object.

Implemented in Wikimedia\JsonCodec\JsonStaticClassCodec, and Wikimedia\JsonCodec\JsonStdClassCodec.


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