JsonCodec
Interfaces to serialize and deserialize PHP objects to/from JSON
|
Class hints with modifiers. More...
Public Member Functions | |
__construct ( $parent, int $modifier=0) | |
Create a new serialization class type hint. | |
__toString () | |
Static Public Member Functions | |
static | build (string $className, int ... $modifiers) |
Helper function to create nested hints. | |
Public Attributes | |
const | DEFAULT = 0 |
The default class hint behavior: an exact match for class name, and the serialization for an object will always use curly braces {} but the return value from ::toJsonArray() will always be an array. | |
const | LIST = 1 |
A list of the hinted type. | |
const | STDCLASS = 2 |
A map of the hinted type. | |
const | USE_SQUARE = 3 |
Prefer to use square brackets to serialize this object, when possible. | |
const | ALLOW_OBJECT = 4 |
Tweak the return type of JsonCodec::toJsonArray() to return a stdClass object instead of array where that makes it possible to generate curly braces instead of adding an extra JsonCodec::TYPE_ANNOTATION value. | |
const | INHERITED = 5 |
The value is an instanceof the hinted type, and the JsonClassCodec for the hinted type will be able to deserialize the object. | |
$parent | |
int | $modifier |
Class hints with modifiers.
@template T
Wikimedia\JsonCodec\Hint::__construct | ( | $parent, | |
int | $modifier = 0 ) |
Create a new serialization class type hint.
class-string<T>|Hint<T> | $parent | |
int | $modifier | A hint modifier |
|
static |
Helper function to create nested hints.
For example, the Foo[][]
type can be created as Hint::build(Foo::class, Hint:LIST, Hint::LIST)
.
Note that, in the grand (?) tradition of C-like types, modifiers are read right-to-left. That is, a "stdClass containing
values which are lists of Foo" is written 'backwards' as: Hint::build(Foo::class, Hint::LIST, Hint::STDCLASS)
.
@phan-template T
class-string<T> | $className |
int | ...$modifiers |
const Wikimedia\JsonCodec\Hint::ALLOW_OBJECT = 4 |
Tweak the return type of JsonCodec::toJsonArray()
to return a stdClass
object instead of array where that makes it possible to generate curly braces instead of adding an extra JsonCodec::TYPE_ANNOTATION
value.
Not compatible with USE_SQUARE
.
const Wikimedia\JsonCodec\Hint::DEFAULT = 0 |
The default class hint behavior: an exact match for class name, and the serialization for an object will always use curly braces {}
but the return value from ::toJsonArray()
will always be an array.
This requires adding an explicit JsonCodec::TYPE_ANNOTATION
element to lists even if proper type hints are supplied.
const Wikimedia\JsonCodec\Hint::INHERITED = 5 |
The value is an instanceof
the hinted type, and the JsonClassCodec
for the hinted type will be able to deserialize the object.
This is useful for tagged objects of various kinds, where a superclass can look at the json data to determine which of its subclasses to instantiate. Note that in this case hints will be taken from the superclass's codec.
const Wikimedia\JsonCodec\Hint::STDCLASS = 2 |
A map of the hinted type.
The value is a stdClass object with string keys and property values of the specified type.
const Wikimedia\JsonCodec\Hint::USE_SQUARE = 3 |
Prefer to use square brackets to serialize this object, when possible.
Not compatible with ALLOW_OBJECT
.