3 namespace Drupal\serialization\Normalizer;
5 use Drupal\Core\TypedData\PrimitiveInterface;
8 * Converts primitive data objects to their casted values.
10 class PrimitiveDataNormalizer extends NormalizerBase {
13 * The interface or class that this Normalizer supports.
17 protected $supportedInterfaceOrClass = PrimitiveInterface::class;
22 public function normalize($object, $format = NULL, array $context = []) {
23 // Typed data casts NULL objects to their empty variants, so for example
24 // the empty string ('') for string type data, or 0 for integer typed data.
25 // In a better world with typed data implementing algebraic data types,
26 // getCastedValue would return NULL, but as typed data is not aware of real
27 // optional values on the primitive level, we implement our own optional
28 // value normalization here.
29 return $object->getValue() === NULL ? NULL : $object->getCastedValue();