JsonSerializable::jsonSerialize
Specify data which should be serialized to JSON
&reftitle.description;
public mixedJsonSerializable::jsonSerialize
Serializes the object to a value that can be serialized natively by
json_encode.
&reftitle.parameters;
&no.function.parameters;
&reftitle.returnvalues;
Returns data which can be serialized by json_encode,
which is a value of any type other than a &resource;.
&reftitle.examples;
JsonSerializable::jsonSerialize example
returning an &array;
array = $array;
}
public function jsonSerialize() {
return $this->array;
}
}
$array = [1, 2, 3];
echo json_encode(new ArrayValue($array), JSON_PRETTY_PRINT);
?>
]]>
&example.outputs;
JsonSerializable::jsonSerialize example
returning an associative &array;
array = $array;
}
public function jsonSerialize() {
return $this->array;
}
}
$array = ['foo' => 'bar', 'quux' => 'baz'];
echo json_encode(new ArrayValue($array), JSON_PRETTY_PRINT);
?>
]]>
&example.outputs;
JsonSerializable::jsonSerialize example
returning an &integer;
number = (integer) $number;
}
public function jsonSerialize() {
return $this->number;
}
}
echo json_encode(new IntegerValue(1), JSON_PRETTY_PRINT);
?>
]]>
&example.outputs;
JsonSerializable::jsonSerialize example
returning a &string;
string = (string) $string;
}
public function jsonSerialize() {
return $this->string;
}
}
echo json_encode(new StringValue('Hello!'), JSON_PRETTY_PRINT);
?>
]]>
&example.outputs;