2

I want to minimize my JSON being produced by the java Jackson (de)serializer. It is being read only in java.

I know, I can use mapper.enable(SerializationFeature.INDENT_OUTPUT) to enable or disable debug indentation, making my JSON more human readable. But can I also (safely) avoid spaces, to strip my JSON?

E.g. in most 'human readable format it is:

{
  "a": "b",
  "c": "d"
}

Without indentation it is:

{ "a": "b", "c": "d" }

But I do want to achieve is:

{"a":"b","c":"d"}

How can I strip these spaces and is it safe at all? Thanks!

Netherwire
  • 2,669
  • 3
  • 31
  • 54
  • Check this other question : https://stackoverflow.com/questions/6852213/can-jackson-be-configured-to-trim-leading-trailing-whitespace-from-all-string-pr/24077444 – Guilherme Mussi Jun 07 '18 at 09:08

1 Answers1

3

The default output of the Jackson framework is minimized.

public class MinimizeJsonClient {
    public static void main(String[] args) throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        User user = new User();
        user.setAge(30);
        user.setName("HenryXi");
        System.out.println(objectMapper.writeValueAsString(user));
    }
}

The output is like following.

{"name":"HenryXi","age":30}
xxy
  • 1,058
  • 8
  • 14