Skip to content

[Serializer] Allow AbstractObjectNormalizer to normalize Scalar values too #58056

Open
@Hanmac

Description

@Hanmac

Description

Before I was using JsonSerializableNormalizer, and to alter some of my scalar values I needed some extra ScalarNormalizer (removing line breaks and other stuff)

That worked so far.

But then Ii wanted to add some extra features that depended on the properties that didn't work with JsonSerializable

    // what characters are invalid
   public const REGEX_NAME = '/[^a-zA-Z\ \-,\.\x{00C0}-\x{017F}]*/mu';

    #[Context([ScalarNormalizer::INVALID_REGEXP_KEY => self::REGEX_NAME])]
    private ?string $name = null;

But it turns out that didn't work either because AbstractObjectNormalizer doesn't allow for scalar values to be extra normalized like the JsonSerializable would.

if (null === $attributeValue || \is_scalar($attributeValue)) {
$data = $this->updateData($data, $attribute, $attributeValue, $class, $format, $attributeContext, $attributesMetadata, $classMetadata);
continue;
}

Would be great if I could have an extra Flag to allow scalar values to be extra normalized while having access to their Property SerializationContext

Edit:
Right now it can be done using NormalizableInterface, but that defeats the whole purpose of using Property SerializationContext

Example

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions