1

I'm using @JSONCreator and @JsonCreator to convert a response bean to JSON in Lagom Framework. But, the JSON is not formatted. How can I pretty print the JSON using the annotations (not ObjectMapper)? Here's my sample response bean:

@Immutable
@JsonDeserialize
public class foo {

  private final List<Result> _result;

  private final MetadataBean _meta;

  @JsonCreator
  public foo (List<Result> _result, MetadataBean _meta) {
    this._result= _result;
    this._meta = _meta;
  }

}
Henrik Aasted Sørensen
  • 6,966
  • 11
  • 51
  • 60

1 Answers1

1

It seems that pretty printing is controlled by the ObjectMapper and cannot be influenced by annotations. The Lagom documentation for negotiated serializers has this example:

public class JsonTextSerializer implements MessageSerializer.NegotiatedSerializer<String, ByteString> {
    private final ObjectMapper mapper = new ObjectMapper();

    @Override
    public MessageProtocol protocol() {
        return new MessageProtocol(Optional.of("application/json"), Optional.empty(), Optional.empty());
    }

    @Override
    public ByteString serialize(String s) throws SerializationException {
        try {
            return ByteString.fromArray(mapper.writeValueAsBytes(s));
        } catch (JsonProcessingException e) {
            throw new SerializationException(e);
        }
    }
}

Pretty printing can then be enabled on the mapper (probably in a constructor):

mapper.enable(SerializationFeature.INDENT_OUTPUT);
Community
  • 1
  • 1
Alex Taylor
  • 8,343
  • 4
  • 25
  • 40