JsonEncoder.php 870 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. <?php
  2. namespace Symfony\Component\Serializer\Encoder;
  3. use Symfony\Component\Serializer\SerializerInterface;
  4. /*
  5. * This file is part of the Symfony framework.
  6. *
  7. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  8. *
  9. * This source file is subject to the MIT license that is bundled
  10. * with this source code in the file LICENSE.
  11. */
  12. /**
  13. * Encodes JSON data
  14. *
  15. * @author Jordi Boggiano <j.boggiano@seld.be>
  16. */
  17. class JsonEncoder extends AbstractEncoder implements EncoderInterface
  18. {
  19. /**
  20. * {@inheritdoc}
  21. */
  22. public function encode($data, $format)
  23. {
  24. if (!is_scalar($data)) {
  25. $data = $this->serializer->normalize($data, $format);
  26. }
  27. return json_encode($data);
  28. }
  29. /**
  30. * {@inheritdoc}
  31. */
  32. public function decode($data, $format)
  33. {
  34. return json_decode($data, true);
  35. }
  36. }