JsonSerializable::jsonSerialize
Spécifie les données qui doivent être linéarisées en JSON
&reftitle.description;
public mixedJsonSerializable::jsonSerialize
Linéarise l'objet en une valeur qui peut être linéarisé nativement par
la fonction json_encode.
&reftitle.parameters;
&no.function.parameters;
&reftitle.returnvalues;
Retourne les données qui peuvent être linéarisées par la fonction
json_encode, qui peuvent être des valeurs
de n'importe quel type autre qu'une &resource;.
&reftitle.examples;
Exemple avec JsonSerializable::jsonSerialize
retournant un &array;
array = $array;
}
public function jsonSerialize() {
return $this->array;
}
}
$array = [1, 2, 3];
echo json_encode(new ArrayValue($array), JSON_PRETTY_PRINT);
?>
]]>
&example.outputs;
Exemple avec JsonSerializable::jsonSerialize
retournant un &array; associatif
array = $array;
}
public function jsonSerialize() {
return $this->array;
}
}
$array = ['foo' => 'bar', 'quux' => 'baz'];
echo json_encode(new ArrayValue($array), JSON_PRETTY_PRINT);
?>
]]>
&example.outputs;
Exemple avec JsonSerializable::jsonSerialize
retournant un &integer;
number = (integer) $number;
}
public function jsonSerialize() {
return $this->number;
}
}
echo json_encode(new IntegerValue(1), JSON_PRETTY_PRINT);
?>
]]>
&example.outputs;
Exemple avec JsonSerializable::jsonSerialize
retournant une &string;
string = (string) $string;
}
public function jsonSerialize() {
return $this->string;
}
}
echo json_encode(new StringValue('Hello!'), JSON_PRETTY_PRINT);
?>
]]>
&example.outputs;