title: Jackson Deserializer delegate to next applicable deserializer
body: <p>I have an external service which I use to query some data. The data will be in one of two formats (first of which is kind of "legacy", but needs to be supported):</p>
  
  <pre><code>{
      "foo": "John Smith"
  }
  </code></pre>
  
  <p>or </p>
  
  <pre><code>{
      "foo": {
          "name": "John Smith",
          "bar": "baz"
      }
  }
  </code></pre>
  
  <p>which I want to map to the following POJO:</p>
  
  <pre><code>@Data
  @AllArgsConstructor
  @NoArgsConstructor
  public class Outer {
  
      private Foo foo;
  
      @Data
      @AllArgsConstructor
      @NoArgsConstructor
      public static class Foo {
  
          String name;
          String bar;
  
      }
  
  }
  </code></pre>
  
  <p>Data in the second format (<code>foo</code> is an object) should be deserialized just like any other POJO, but given data in the first format (<code>foo</code> is string), to turn it into an instance of <code>Foo</code>, I want to call <code>new Foo(&lt;foo&gt;, null)</code>. To do this, I have created a custom deserializer (<code>@JsonComponent</code> means that this deserializer will be registered with a kinda-global <code>ObjectMapper</code> by spring via Jackson <code>Module</code> interface):</p>
  
  <pre><code>@JsonComponent
  public class FooDeserializer extends JsonDeserializer&lt;Outer.Foo&gt; {
  
      @Override
      public Outer.Foo deserialize(JsonParser parser, DeserializationContext context)
              throws IOException {
          JsonNode node = parser.getCodec().readTree(parser);
          if (node.isTextual()) {
              return new Foo(node.asText(), null);
          }
          return &lt;delegate to next applicable deserializer&gt;;
      }
  
  }
  </code></pre>
  
  <p>I'm having trouble figuring out how to do the "delegate to next applicable deserializer" part, as every solution I've tried (for example <code>parser.getCodec().treeToValue(node, Outer.Foo.class)</code>) ends up using the same custom deserializer again, causing infinite recursion. Is this even possible?</p>