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

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...

+ Inheritance diagram for Wikimedia\JsonCodec\JsonStaticClassCodec:

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.
 

Detailed Description

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.

Member Function Documentation

◆ getInstance()

static Wikimedia\JsonCodec\JsonStaticClassCodec::getInstance ( )
static

Return a singleton instance of this class codec.

Returns
JsonStaticClassCodec a singleton instance of this class

◆ jsonClassHintFor()

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.

Parameters
class-string<T>$className
string$keyName
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.

Implements Wikimedia\JsonCodec\JsonClassCodec.

◆ newFromJsonArray()

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.

Parameters
class-string<T>$className
array$json
Returns
T @inheritDoc @phan-template T
See also
JsonCodecableTrait

Implements Wikimedia\JsonCodec\JsonClassCodec.

◆ toJsonArray()

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.

Parameters
object$objAn object of the type handled by this JsonClassCodec
Returns
array A Json representation of the object. @inheritDoc
See also
JsonCodecableTrait

Implements Wikimedia\JsonCodec\JsonClassCodec.


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