usage.rst 1017 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. Usage
  2. =====
  3. Serializing Objects
  4. -------------------
  5. Most common usage is probably to serialize objects. This can be achieved
  6. very easily:
  7. .. configuration-block ::
  8. .. code-block :: php
  9. <?php
  10. $serializer = JMS\Serializer\SerializerBuilder::create()->build();
  11. $serializer->serialize($object, 'json');
  12. $serializer->serialize($object, 'xml');
  13. $serializer->serialize($object, 'yml');
  14. .. code-block :: jinja
  15. {{ object | serialize }} {# uses JSON #}
  16. {{ object | serialize('json') }}
  17. {{ object | serialize('xml') }}
  18. {{ object | serialize('yml') }}
  19. Deserializing Objects
  20. ---------------------
  21. You can also deserialize objects from their XML, or JSON representation. For
  22. example, when accepting data via an API.
  23. .. code-block :: php
  24. <?php
  25. $serializer = JMS\Serializer\SerializerBuilder::create()->build();
  26. $object = $serializer->deserialize($jsonData, 'MyNamespace\MyObject', 'json');