JsonCodec
Interfaces to serialize and deserialize PHP objects to/from JSON
|
Classes implementing this interface support round-trip JSON serialization/deserialization for certain class types. More...
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. | |
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
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.
class-string<T> | $className The class we're looking for a hint for | |
string | $keyName | The name of the array key we'd like a hint on |
[]
can also be returned, but that is deprecated. Implemented in Wikimedia\JsonCodec\JsonStaticClassCodec, and Wikimedia\JsonCodec\JsonStdClassCodec.
Wikimedia\JsonCodec\JsonClassCodec::newFromJsonArray | ( | string | $className, |
array | $json ) |
Creates a new instance of the given class and initializes it from the $json array.
class-string<T> | $className | |
array | $json |
Implemented in Wikimedia\JsonCodec\JsonStaticClassCodec, and Wikimedia\JsonCodec\JsonStdClassCodec.
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.
T | $obj | An object of the type handled by this JsonClassCodec |
Implemented in Wikimedia\JsonCodec\JsonStaticClassCodec, and Wikimedia\JsonCodec\JsonStdClassCodec.