JsonCodec
Interfaces to serialize and deserialize PHP objects to/from JSON
|
This is a simple class codec which proxies to methods on the object for serialization and a static method on the class for deserialization. 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, using a ::toJsonArray() method on the object itself. | |
newFromJsonArray (string $className, array $json) | |
Creates a new instance of the given class and initializes it from the $json array, using a static method on $className. | |
jsonClassHintFor (string $className, string $keyName) | |
Return an optional type hint for the given array key in the result of ::toJsonArray() / input to ::newFromJsonArray. | |
Static Public Member Functions | |
static | getInstance () |
Return a singleton instance of this class codec. | |
This is a simple class codec which proxies to methods on the object for serialization and a static method on the class for deserialization.
It is intended for use as a singleton helper to JsonCodecableTrait.
|
static |
Return a singleton instance of this class codec.
Wikimedia\JsonCodec\JsonStaticClassCodec::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 | |
string | $keyName |
[]
can also be returned, but that is deprecated. Implements Wikimedia\JsonCodec\JsonClassCodec.
Wikimedia\JsonCodec\JsonStaticClassCodec::newFromJsonArray | ( | string | $className, |
array | $json ) |
Creates a new instance of the given class and initializes it from the $json array, using a static method on $className.
class-string<T> | $className | |
array | $json |
Implements Wikimedia\JsonCodec\JsonClassCodec.
Wikimedia\JsonCodec\JsonStaticClassCodec::toJsonArray | ( | $obj | ) |
Returns a JSON array representing the contents of the given object, that can be deserialized with the corresponding newFromJsonArray() method, using a ::toJsonArray() method on the object itself.
object | $obj | An object of the type handled by this JsonClassCodec |
Implements Wikimedia\JsonCodec\JsonClassCodec.