title: Jackson: How to add custom property to the JSON without modifying the POJO
body: <p>I am developing a REST interface for my app using Jackson to serialize my POJO domain objects to JSON representation. I want to customize the serialization for some types to add additional properties to the JSON representation that do not exist in POJOs (e.g. add some metadata, reference data, etc). I know how to write my own <code>JsonSerializer</code>, but in that case I would need to explicitly call <code>JsonGenerator.writeXXX(..)</code> methods for <strong>each</strong> property of my object while all I need is just to <strong>add</strong> an additional property. In other words I would like to be able to write something like:</p>
  
  <pre><code>@Override
  public void serialize(TaxonomyNode value, JsonGenerator jgen, SerializerProvider provider) {
      jgen.writeStartObject();
      jgen.writeAllFields(value); // &lt;-- The method I'd like to have
      jgen.writeObjectField("my_extra_field", "some data");
      jgen.writeEndObject();
  }
  </code></pre>
  
  <p>or (even better) to somehow intercept the serialization before the <code>jgen.writeEndObject()</code> call, e.g.:</p>
  
  <pre><code>@Override void beforeEndObject(....) {
      jgen.writeObjectField("my_extra_field", "some data");
  }
  </code></pre>
  
  <p>I thought I could extend <code>BeanSerializer</code> and override its <code>serialize(..)</code> method but it's declared <code>final</code> and also I couldn't find an easy way to create a new instance of <code>BeanSerializer</code> without providing it with all the type metadata details practically duplicating a good portion of Jackson. So I've given up on doing that.</p>
  
  <p><strong>My question is</strong> - how to customize Jackson's serialization to add additional stuff to the JSON output for particular POJOs without introducing too much of the boilerplate code and reusing as much as possible of the default Jackson behaviour.</p>