title: ObjectMapper readValue invoked multiple times until a StackOverflowException is thrown
body: <p>I am trying to build a custom deserializer class for my value class. But the ObjectMapper .readValue is being invoked multiple times until a StackOverflowException is being thrown. The following is an example only:</p>
  
  <pre><code>@JsonDeserialize(using = UserDeserializer.class)
  @Getter
  public class User {
      private String name;
  }
  
  public class UserDeserializer extends StdDeserializer&lt;User&gt; {
      public UserDeserializer() {
          super(User.class);
      }
  
      @Override
      public User deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
          return ctxt.readValue(p, User.class);
      }
  }
  
  public class UserDeserializerTest {
      @Test
      public void whenUserIsDeserializedFromJsonThenNameShouldEqualBob() {
          String = "{ \"name\" : \"bob\" }";
          ObjectMapper mapper = new ObjectMapper();
          User user = mapper.readValue(json, User.class);
          assertThat(user.getName).isEqualTo("bob");
      }
  }
  </code></pre>
  
  <p>Maven Dependencies:</p>
  
  <pre><code>&lt;dependencies&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;com.fasterxml.jackson.core&lt;/groupId&gt;
          &lt;artifactId&gt;jackson-databind&lt;/artifactId&gt;
          &lt;version&gt;2.6.1&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.assertj&lt;/groupId&gt;
          &lt;artifactId&gt;assertj-core&lt;/artifactId&gt;
          &lt;version&gt;1.7.1&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.projectlombok&lt;/groupId&gt;
          &lt;artifactId&gt;lombok&lt;/artifactId&gt;
          &lt;version&gt;1.16.2&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;junit&lt;/groupId&gt;
          &lt;artifactId&gt;junit&lt;/artifactId&gt;
          &lt;version&gt;4.12&lt;/version&gt;
          &lt;scope&gt;test&lt;/scope&gt;
      &lt;/dependency&gt;
  &lt;/dependencies&gt;
  </code></pre>
  
  <p>The following StackOverflowException is thrown:</p>
  
  <pre><code>Exception in thread "main" java.lang.StackOverflowError
  ...
  
  // The following lines are repeated.
  
      at com.fasterxml.jackson.databind.DeserializationContext.readValue(DeserializationContext.java:754)
      at com.fasterxml.jackson.databind.DeserializationContext.readValue(DeserializationContext.java:746)
      at example.UserDeserializer.deserialize(UserDeserializer.java:25)
      at example.UserDeserializer.deserialize(UserDeserializer.java:15)
  
  Process finished with exit code 1
  </code></pre>
  
  <p>I have tried registering the deserializer via a module and removing the @JsonAnnotation and still throws the same exception:</p>
  
  <pre><code>SimpleModule module = new SimpleModule("User Simple Module");
  module.addDeserializer(User.class, new UserDeserializer());
  ObjectMapper mapper = new ObjectMapper();
  mapper.addModule(module);
  mapper.readValue(json, User.class);
  </code></pre>
  
  <p>Then I tried creating a new ObjectMapper within my UserDeserializer and remove @JsonDeserialize from my User class and my test passes.</p>
  
  <pre><code>@Override
  public User deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
      return new ObjectMapper().readValue(p, User.class);
  }
  </code></pre>
  
  <p>Whilst doing the above works, I want to register the deserializer on a per class basis using the @JsonDeserialize annotation rather than registering a global custom object mapper. There are reasons for me wanting it that way.</p>
  
  <p>I tried other combinations of code, for example:</p>
  
  <pre><code>// Add the 'as' keyword to the annotation
  @JsonDeserialise(using = UserDeserialize.class, as = User.class)
  
  // Locally assigning a mapper from the JsonParser inside the UserDeserializer class
  ObjectMapper mapper = (ObjectMapper) p.getCodec();
  mapper.readValue(p, User.class);
  
  // Constructing a new JSON Parser inside the deserializer
  ObjectMapper mapper = (ObjectMapper) p.getCodec();
  JsonFactory factory = mapper.getFactory();
  JsonParser parser = factory.createParser(p.getText());
  return (User) parser.readValueAs(handledType());
  
  // Reading the Json into a Tree
  ObjectMapper mapper = (ObjectMapper) p.getCodec();
  ObjectNode root = (ObjectNode) p.readValueAsTree();
  return mapper.readValue(root.traverse(), User.class);
  </code></pre>
  
  <p>But reading the JSON into a Tree throws:</p>
  
  <pre><code>Exception in thread "main" java.lang.RuntimeException: java.lang.IllegalStateException: No ObjectCodec defined for parser, needed for deserialization
      at example.User.main(User.java:46)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:606)
      at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
  Caused by: java.lang.IllegalStateException: No ObjectCodec defined for parser, needed for deserialization
      at com.fasterxml.jackson.core.JsonParser._codec(JsonParser.java:1565)
      at com.fasterxml.jackson.core.JsonParser.readValueAsTree(JsonParser.java:1559)
      at example.UserDeserializer.deserialize(UserDeserializer.java:37)
      at example.UserDeserializer.deserialize(UserDeserializer.java:17)
      at com.fasterxml.jackson.databind.ObjectMapper._readValue(ObjectMapper.java:3674)
      at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:1996)
      at example.UserDeserializer.deserialize(UserDeserializer.java:38)
      at example.UserDeserializer.deserialize(UserDeserializer.java:17)
      at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3702)
      at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2714)
      at example.User.main(User.java:36)
      ... 5 more
  </code></pre>
  
  <p>I also tried implementing the ContextualDeserializer inside my UserDeserializer but still get the same StackOverflowException as before:</p>
  
  <pre><code>private UserDeserializer(Class&lt;?&gt; vc) {
      super(vc);
  }
  
  public JsonDeserializer&lt;User&gt; createContextual(DeserializationContext ctxt, BeanProperty property) throws JsonMappingException {
      return new UserDeserializer(ctxt.getContextualType().getRawClass());
  }
  
  @Override
  public User deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
      return (User) new ObjectMapper().readValue(p, handledType());
  }
  </code></pre>
  
  <p>I hope this is just a bad case of a PEBKAC error and a solution is readily available with your help/guidance.</p>
  
  <p>Kind regards</p>