title: Jackson JSON Modify Object Before Serialization
body: <p>I am looking to modify an object right before it gets serialized.  I want to write a custom serializer to parse the object, then pass it to the default object serializer.</p>
  
  <p>This is what I have:</p>
  
  <pre><code>import com.fasterxml.jackson.core.JsonGenerator;
  import com.fasterxml.jackson.core.JsonProcessingException;
  import com.fasterxml.jackson.databind.JsonSerializer;
  import com.fasterxml.jackson.databind.SerializerProvider;
  import java.io.IOException;
  
  /**
   *
   * @author Me
   */
  public class PersonSerializer extends JsonSerializer&lt;Person&gt;{
  
      @Override
      public void serialize(Person value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException {
  
          //This returns a modified clone of Person value.
          Person safePerson = PrivacyService.getSafePerson(value);
  
          provider.defaultSerializeValue(safePerson, jgen);
  
      }
  
  }
  </code></pre>
  
  <p>But that just goes in an infinate loop.  I have also tried:</p>
  
  <pre><code>provider.findTypedValueSerializer(Person.class, true, null).serialize(safePerson, jgen, provider);
  </code></pre>
  
  <p>That works, but it doesn't parse any of the fields in the object.</p>
  
  <p>I also tried using a <code>@JsonFilter</code> but it was extremely heavy and sextupled my load times.</p>
  
  <p>Help! Thanks!</p>