Skip to content

[Serializer] Ability to add custom annotations #38993

@d42ohpaz

Description

@d42ohpaz

Description
Similar how we can add custom validator annotations (https://symfony.com/doc/current/validation/custom_constraint.html), it would be beneficial to be able to create custom serialization annotations to augment existing functionality on a per-property/method basis.

Right now, Groups, SerializedName, and Ignore are all hard-coded into the serialization process via the AttributeMetadaInterface, AnnotationLoader, and the ClassMetadataInterface. There is no easy or intuitive way to create and use custom annotations currently that I have found (If I'm mistaken, I'd love to hear more).

Example
Two use cases:

  1. Encryption: we sometimes serialize personally-identifiable-information (PII) as part of a data transfer process from an API to a consumer endpoint. However, for obvious security reasons, we encrypt this PII so if it is intercepted during transmission then the data is still secure. When calling the serialize process, we have to leak our implementation by designating which fields of our models and entities need to be encrypted (typically in the controller). Instead, it would be nice to write an @Encrypt() annotation that can decorate the properties inside of the entities themselves and automatically encrypt - similar to how @Ignore or @SerializedName works.
  2. Dates: While we can globally set a specific date format for our DateTime instances, there are some circumstances where we need to arbitrarily send back a date in a different format (e.g., just a date or a date with a time or a date/time with timezone information).

Thank you.

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

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy