exclusion_strategies.rst 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. Exclusion Strategies
  2. ====================
  3. Introduction
  4. ------------
  5. The serializer supports different exclusion strategies. Each strategy allows
  6. you to define which properties of your objects should be serialized.
  7. General Exclusion Strategies
  8. ----------------------------
  9. If you would like to always expose, or exclude certain properties. Then, you can
  10. do this with the annotations ``@ExclusionPolicy``, ``@Exclude``, and ``@Expose``.
  11. The default exclusion policy is to exclude nothing. That is, all properties of the
  12. object will be serialized. If you only want to expose a few of the properties,
  13. then it is easier to change the exclusion policy, and only mark these few properties:
  14. .. code-block :: php
  15. <?php
  16. use JMS\SerializerBundle\Annotation\ExclusionPolicy;
  17. use JMS\SerializerBundle\Annotation\Expose;
  18. /**
  19. * The following annotations tells the serializer to skip all properties which
  20. * have not marked with @Expose.
  21. *
  22. * @ExclusionPolicy("all")
  23. */
  24. class MyObject
  25. {
  26. private $foo;
  27. private $bar;
  28. /**
  29. * @Expose
  30. */
  31. private $name;
  32. }
  33. .. note ::
  34. A property that is excluded by ``@Exclude`` cannot be exposed anymore by any
  35. of the following strategies, but is always hidden.
  36. Versioning Objects
  37. ------------------
  38. JMSSerializerBundle comes by default with a very neat feature which allows
  39. you to add versioning support to your objects, e.g. if you want to
  40. expose them via an API that is consumed by a third-party:
  41. .. code-block :: php
  42. <?php
  43. class VersionedObject
  44. {
  45. /**
  46. * @Until("1.0.x")
  47. */
  48. private $name;
  49. /**
  50. * @Since("1.1")
  51. * @SerializedName("name")
  52. */
  53. private $name2;
  54. }
  55. .. note ::
  56. ``@Until``, and ``@Since`` both accept a standardized PHP version number.
  57. If you have annotated your objects like above, you can serializing different
  58. versions like this:
  59. .. code-block :: php
  60. <?php
  61. $serializer->setVersion('1.0');
  62. $serializer->serialize(new VersionObject(), 'json');
  63. Creating Different Views of Your Objects
  64. ----------------------------------------
  65. Another default exclusion strategy is to create different views of your objects.
  66. Let's say you would like to serialize your object in a different view depending
  67. whether it is displayed in a list view or in a details view.
  68. You can achieve that by using the ``@Groups`` annotation on your properties.
  69. .. code-block :: php
  70. <?php
  71. use JMS\SerializerBundle\Annotation\Groups;
  72. class BlogPost
  73. {
  74. /** @Groups({"list", "details"}) */
  75. private $id;
  76. /** @Groups({"list", "details"}) */
  77. private $title;
  78. /** @Groups({"list"}) */
  79. private $nbComments;
  80. /** @Groups({"details"}) */
  81. private $comments;
  82. }
  83. You can then tell the serializer which groups to serialize in your controller:
  84. .. code-block :: php
  85. <?php
  86. $serializer->setGroups(array('list'));
  87. $serializer->serialize(new BlogPost(), 'json');