Jackson deserialization: Custom Object factory
<p>How do you implement in Jackson a conversion from json to Java objects, based on class types specified in the json.</p>

<p>Example Java Types:</p>

<pre><code>public class Car{
    public String make;
    public String model;
}

public class Spoon {
    public String material;
}

public class Ownership {
    public List&lt;Object&gt; items;
    public User owner;
}
</code></pre>

<p>Example Json:</p>

<pre><code>{
    "items": [
        {
            "@class": "com.example.Car",
            "make": "Mercedes-Benz",
            "model": "S500"
        },
        {
            "@class": "com.example.Spoon",
            "material": "silver"
        }            
    ],
    "owner": {
        "name": "John"
    }
}
</code></pre>

<p>Since the number of classes is unknown (users can add any class) it is not possible to use the annotation @JsonSubTypes.</p>

<p>In addition, the json may contain known strongly types classes, like the object User in the example which is serialized using the standard Jackson implementation.</p>

<p>Most of the examples I can find, such as <a href="http://www.baeldung.com/jackson-inheritance" rel="nofollow">http://www.baeldung.com/jackson-inheritance</a> assume the number of subclasses is known, but in my case it is not, users of the framework will add their own.</p>

<p>Ideally the implementation will just resolve types and let Jackson do the rest of the serialization without repeating that code.</p>