exclusion_strategies.rst 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. private $createdAt;
  80. }
  81. You can then tell the serializer which groups to serialize in your controller::
  82. use JMS\Serializer\SerializationContext;
  83. $serializer->serialize(new BlogPost(), 'json', SerializationContext::create()->setGroups(array('list')));
  84. //will output $id, $title and $nbComments.
  85. $serializer->serialize(new BlogPost(), 'json', SerializationContext::create()->setGroups(array('Default', 'list')));
  86. //will output $id, $title, $nbComments and $createdAt.
  87. Limiting serialization depth of some properties
  88. -----------------------------------------------
  89. You can limit the depth of what will be serialized in a property with the
  90. ``@MaxDepth`` annotation.
  91. This exclusion strategy is a bit different from the others, because it will
  92. affect the serialized content of others classes than the one you apply the
  93. annotation to.
  94. .. code-block :: php
  95. use JMS\Serializer\Annotation\MaxDepth;
  96. class User
  97. {
  98. private $username;
  99. /** @MaxDepth(1) */
  100. private $friends;
  101. /** @MaxDepth(2) */
  102. private $posts;
  103. }
  104. class Post
  105. {
  106. private $title;
  107. private $author;
  108. }
  109. In this example, serializing a user, because the max depth of the ``$friends``
  110. property is 1, the user friends would be serialized, but not their friends;
  111. and because the the max depth of the ``$posts`` property is 2, the posts would
  112. be serialized, and their author would also be serialized.
  113. You need to tell the serializer to take into account MaxDepth checks::
  114. use JMS\Serializer\SerializationContext;
  115. $serializer->serialize($data, 'json', SerializationContext::create()->enableMaxDepthChecks());