title: Deserializing JSON flat object using immutable classes with Jackson
body: <p>I'm quite new to the Jackson library (version 1.9). I'm using it only since a couple of weeks, and I find it very flexible and time-saving when it's about serializing and deserializing objects in Java.</p>
  
  <p>I'm experiencing troubles, though, into deserializing "flat" JSONs to a class which is a composition of another, when both are meant to be immutable.</p>
  
  <p>My situation is pretty much the following:</p>
  
  <pre><code>class Foo {
  
      private final String var1;
  
      Foo(String var1) {
              this.var1 = var1;
      }
      // getters omitted
  }
  
  class A {
      private final Foo foo;
      private final String var2;
  
      A(/* @JsonUnwrapped doesn't work here */ Foo foo, String var2) {
              this.foo = foo;
              this.var2 = var2;
      }
  
      @JsonUnwrapped
      Foo getFoo() {
          return foo;
      }
  
      String getVar2() {
          return var2;
      }
  }
  
  class B extends Foo {
      private final String var2;
  
      B(String var1, String var2) {
              super(var1);
              this.var2 = var2;
      }
      // getters omitted
  }
  </code></pre>
  
  <p>And the JSON to deserialize is something like this:</p>
  
  <pre><code>{ "var1" : "some_value", "var2" : "some_other_value" }
  </code></pre>
  
  <p>The question is: is there an annotation-based way (so, without the need of using a custom deserializer) to tell Jackson to compose the given JSON to a 'A' instance?
  I've tried using the @JsonUnwrapped attribute for the Foo argument in class 'A' constructor, but it's not supported in multi-argument constructor as it would need a JsonProperty to work (which doesn't make sense, because there is actually no single property for those items).
  Serialization, instead, works perfectly using this pattern.</p>
  
  <p>It would also work with a non-immutable class by using separate setters, but I'd like to know if there's a way to do the same by only using the constructors (or a builder, which would make sense as in reality the fields are much more than the one in the example).</p>
  
  <p>The very same method obviously works with class 'B' which inherits from 'Foo'.</p>
  
  <p>Thanks in advance.</p>