Netherwire
Netherwire

Reputation: 2787

Jackson JSON skip spaces at serializing

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!

Upvotes: 2

Views: 4246

Answers (1)

xxy
xxy

Reputation: 1076

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}

Upvotes: 4

Related Questions