title: Custom Jackson JSON serialization based on parent context
body: <p>I am trying to conditionally serialize a child bean based on information from the parent. </p>
  
  <pre><code>public class Parent {
    private boolean isSecure;
    private Child child;
  }
  
  public class Child {
    String someValue;
  }
  </code></pre>
  
  <p>Basically, if the <code>parent.isSecure()</code> is true, then I want to serialize the <code>someValue</code> differently than if <code>parent.isSecure()</code> is false.</p>
  
  <p>My actual classes are much more complex than this simple example, so I don't really want to create a full custom serializer for <code>Parent</code> that I would need to maintain separately (unless you can show me how to leverage standard serialization for <code>Parent</code> within the custom serializer without having to iterate through all the properties and deal with JSON annotations, views, and such.  If you can, that might work, as I could set a context attribute based on <code>isSecure()</code>).</p>
  
  <p>I don't mind writing a custom serializer for a child, but I don't know how to check the value from the parent.</p>
  
  <p>I've considered using a 'secure' view and 'insecure' view that I could specify outside the entire serialization process, but then I would have to go through and annotate lots of properties of the parent class to be in both the 'secure' and 'insecure' view (as I have turned off the <code>DEFAULT_VIEW_INCLUSION</code> option due to a number of properties I never want to be serialized).</p>
  
  <p>I tried to access <code>generator.getCurrentValue()</code> within my custom Child serializer, but it is always null, so I'm not sure how it is supposed to work.</p>
  
  <p>I've considered writing a custom serializer for <code>isSecure</code>, and setting a context attribute within it, but how can I be sure that it will be called prior to the custom serializer for a child? (Though I will admit that I'm not sure context attributes really work the way I am assuming.)</p>
  
  <p>Any ideas?</p>