class Serializer (View source)

Serializer class.

Methods

static mixed
decode(string $value)

Decodes the given value using JSON standard.

static string
encode(mixed $value)

Encodes the given value using JSON standard.

static string
serialize(mixed $variable)

Returns a string representation of the given variable.

static mixed
unserialize(string $variable, string|null $cast = null)

Returns the passed variable and applies the given cast to it if specified.

Details

static mixed decode(string $value)

Decodes the given value using JSON standard.

Parameters

string $value

The value to decode.

Return Value

mixed

static string encode(mixed $value)

Encodes the given value using JSON standard.

Parameters

mixed $value

The value to encode.

Return Value

string

static string serialize(mixed $variable)

Returns a string representation of the given variable.

The variable will be stringified as follows:

  • null -> As JSON null (null).
  • boolean -> As JSON boolean (true or false).
  • integer -> As JSON number (123).
  • float -> As JSON number (1.5).
  • string -> As JSON string ("string").
  • array -> As JSON array or object ([1, "2", "Three"] or {"one": 1, "two": "2", "three": "Three"}).
  • object -> As JSON object ({"property": "value"}) or the result of stringifing if the object implements __toString().

NOTE: This method works in conjunction with self::unserialize().

Parameters

mixed $variable

The variable to stringify.

Return Value

string

static mixed unserialize(string $variable, string|null $cast = null)

Returns the passed variable and applies the given cast to it if specified.

Available casts are:

  • null
  • boolean or bool
  • integer or int
  • float or double
  • string
  • array
  • object

NOTE: Unknown casts will return the passed variable.

NOTE: This method works in conjunction with self::serialize().

Parameters

string $variable

The variable to cast.

string|null $cast

The type to cast into. If not specifed the type will be inferred from the variable (using JSON rules).

Return Value

mixed