title: Serialize/Deserialize using JsonTypeInfo
body: <p>My goal is to convert a JSON string field to the right class using Jackson.</p>
  <p>I have the following class:</p>
  <pre><code>public class AnimalRecord {
  
      private String id;
      private String source;
      @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = &quot;source&quot;, include = JsonTypeInfo.As.EXTERNAL_PROPERTY)
      @JsonSubTypes(value = {
              @JsonSubTypes.Type(value = CatProbeMetadata.class, name 
   = &quot;catProbeMetadata&quot;),
      @JsonSubTypes.Type(value = DogProbeMetadata.class, name = &quot;dogProbeMetadata&quot;),
              })
     private AnimalMetadata metadata;
  </code></pre>
  <p>In addition to this class, I have a DB table where I store records of AnimalRecord (AnimalRecord = row). The AnimalMetadata is a different JSON string based on the <code>source</code> of this class. Each source has it's own <code>metadata</code> and class definition. In this example, CatProbeMetadata class will be the output when doing de-serialization from the string when the source is &quot;cat&quot;.</p>
  <p>The issue is that I'm not sure what to do when reading the rows from the DB. I have the following method:</p>
  <pre><code>private class ActiveProbeWrapper implements RowMapper&lt;ActiveProbeRecord&gt; {
  
          @Override
          public ActiveProbeRecord mapRow(ResultSet rs, int rowNum) throws SQLException {
              String id= rs.getString(&quot;id&quot;);
              String source= rs.getString(&quot;source&quot;);
              Animalmetadata metadata = // NOT SURE WHAT TO DO HERE;
              ActiveProbeRecord record = new ActiveProbeRecord(deviceId,segment, source, metadata);
              return record;
          }
  
      }
  </code></pre>
  <p>I need to convert the string in the DB to the right class instance, but my metadata string will NOT include the source (since it's outside the metadata JSON).</p>
  <p><strong>The question: Will I have to add the &quot;source&quot; field to the metadata itself or is there any better way of doing this that I missed?</strong></p>
  <p><strong>Updated example:</strong>
  Example of DB rows:
  id | source | metadata
  1 | catSource | {&quot;catName&quot;: &quot;Mewy&quot;}
  2 | dogSource | {&quot;dogName&quot;: &quot;Barky&quot;}</p>
  <p>When I read the rows from the DB I want to use the <code>source</code> field to de-serialize <code>metadata</code> to the right class - String --&gt; <code>CatMetadata</code></p>