exclusion_strategies.rst 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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\Serializer\Annotation\ExclusionPolicy;
  17. use JMS\Serializer\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. use JMS\Serializer\SerializationContext;
  60. $serializer->serialize(new VersionObject(), 'json', SerializationContext::create()->setVersion(1));
  61. Creating Different Views of Your Objects
  62. ----------------------------------------
  63. Another default exclusion strategy is to create different views of your objects.
  64. Let's say you would like to serialize your object in a different view depending
  65. whether it is displayed in a list view or in a details view.
  66. You can achieve that by using the ``@Groups`` annotation on your properties.
  67. .. code-block :: php
  68. use JMS\Serializer\Annotation\Groups;
  69. class BlogPost
  70. {
  71. /** @Groups({"list", "details"}) */
  72. private $id;
  73. /** @Groups({"list", "details"}) */
  74. private $title;
  75. /** @Groups({"list"}) */
  76. private $nbComments;
  77. /** @Groups({"details"}) */
  78. private $comments;
  79. }
  80. You can then tell the serializer which groups to serialize in your controller::
  81. use JMS\Serializer\SerializationContext;
  82. $serializer->serialize(new BlogPost(), 'json', SerializationContext::create()->setGroups(array('list')));
  83. Limiting serialization depth of some properties
  84. -----------------------------------------------
  85. You can limit the depth of what will be serialized in a property with the
  86. ``@MaxDepth`` annotation.
  87. This exclusion strategy is a bit different from the others, because it will
  88. affect the serialized content of others classes than the one you apply the
  89. annotation to.
  90. .. code-block :: php
  91. use JMS\Serializer\Annotation\MaxDepth;
  92. class User
  93. {
  94. private $username;
  95. /** @MaxDepth(1) */
  96. private $friends;
  97. /** @MaxDepth(2) */
  98. private $posts;
  99. }
  100. class Post
  101. {
  102. private $title;
  103. private $author;
  104. }
  105. In this example, serializing a user, because the max depth of the ``$friends``
  106. property is 1, the user friends would be serialized, but not their friends;
  107. and because the the max depth of the ``$posts`` property is 2, the posts would
  108. be serialized, and their author would also be serialized.
  109. You need to tell the serializer to take into account MaxDepth checks::
  110. use JMS\Serializer\SerializationContext;
  111. $serializer->serialize($data, 'json', SerializationContext::create()->enableMaxDepthChecks());