title: Symfony: How to make JMS Serializer works with strict types?
body: <p>This is my situation:</p>
  
  <p>I'm trying to write a Symfony REST API that works with "strict" types (integer, boolean and float), because default Symfony behaviour doesn't support it and I want to avoid force cast types (e.g: <a href="https://stackoverflow.com/questions/35502472/how-to-make-jms-serializer-throw-an-exception-on-deserializing-json-instead-of-c">JMS Serializer converts string value into integer field type</a>)</p>
  
  <p>To do this, I have created a custom handler which implements the <code>JMS\Serializer\Handler\SubscribingHandlerInterface</code>
  (e.g a <code>StrictIntegerHandler</code>):</p>
  
  <pre><code>&lt;?php
  
  namespace AppBundle\Serializer;
  
  use JMS\Serializer\Context;
  use JMS\Serializer\GraphNavigator;
  use JMS\Serializer\Handler\SubscribingHandlerInterface;
  use JMS\Serializer\JsonDeserializationVisitor;
  use JMS\Serializer\JsonSerializationVisitor;
  use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
  
  class StrictIntegerHandler implements SubscribingHandlerInterface
  {
      public static function getSubscribingMethods()
      {
          return [
              [
                  'direction' =&gt; GraphNavigator::DIRECTION_DESERIALIZATION,
                  'format' =&gt; 'json',
                  'type' =&gt; 'strict_integer',
                  'method' =&gt; 'deserializeStrictIntegerFromJSON',
              ],
              [
                  'direction' =&gt; GraphNavigator::DIRECTION_SERIALIZATION,
                  'format' =&gt; 'json',
                  'type' =&gt; 'strict_integer',
                  'method' =&gt; 'serializeStrictIntegerToJSON',
              ],
          ];
      }
  
      public function deserializeStrictIntegerFromJSON(
          JsonDeserializationVisitor $visitor, $data, array $type)
      {
          return $data;
      }
  
      public function serializeStrictIntegerToJSON(
          JsonSerializationVisitor $visitor, $data, array $type, Context $context)
      {
          return $visitor-&gt;visitInteger($data, $type, $context);
      }
  }
  </code></pre>
  
  <p>My entity looks:</p>
  
  <pre><code>&lt;?php
  
  namespace AppBundle\Entity;
  
  use Doctrine\ORM\Mapping as ORM;
  use JMS\Serializer\Annotation as Serializer;
  use Symfony\Component\Validator\Constraints as Validator;
  
  /**
   * Person
   *
   * @ORM\Table(name="persons")
   * @ORM\Entity(repositoryClass="AppBundle\Repository\PersonRepository")
   */
  class Person
  {
      /**
       * @var int age
       *
       * @ORM\Column(name="age", type="integer")
       *
       * @Serializer\Type("strict_integer")
       * @Serializer\Groups({"Person"})
       *
       * @Validator\Type(type="integer", message="Age field has wrong type")
       */
      private $age;
  
      public function getAge()
      {
          return $this-&gt;age;
      }
  
      public function setAge(int $age)
      {
          $this-&gt;age = $age;
      }
  }
  </code></pre>
  
  <p>When I throw the following POST actions, the JMS Serializer returns the correct results:</p>
  
  <ol>
  <li><code>{ "age" : 12 }</code> will result in <code>int(12)</code></li>
  <li><code>{ "age" : "asdf" }</code> will result in <code>"Age field has wrong type"</code></li>
  </ol>
  
  <p>In both cases my method <code>deserializeStrictIntegerFromJSON</code> is called, so deserialize process works perfectly as I want.</p>
  
  <p>Problems comes with serialize process: 
  When I launch GET action (<code>/person/id_person</code>) I get the following exception:</p>
  
  <blockquote>
    <p>Expected object but got integer. Do you have the wrong @Type mapping
    or could this be a Doctrine many-to-many relation?
    (JMS\Serializer\Exception\LogicException)</p>
  </blockquote>
  
  <p>Debug stack trace shows me that method <code>serializeStrictIntegerToJSON</code> is never called..</p>
  
  <p>How can I solve it? Thanks.</p>