2

So I have a strange issue where jackson's ObjectMapper has a valueToTree(someObject) method that takes a POJO and outputs a JsonNode. However, the object I have has a bajillion fields on it that should not be serialized when doing this, and typically when I want to serialize the object to bytes or to a string this is easily accomplished with something like this:

mapper.writerWithView(User.WithRoles.class).writeValueAsBytes(myUser)

However, it doesn't appear that I can use a JsonView with this valueToTree method, so my only options appear to be to either create the json manually (ugh) or do some nightmare like this:

final JsonNode userJson = mapper.readTree(mapper.writerWithView(User.class).writeValueAsBytes(userAndRoles.getKey()));

Where I double parse the json. Surely there has to be a better way, right?

There is a similar question here, but interestingly the author seems to have just wanted a JsonNode because Play Framework's ok(content), unauthorized(content) etc methods only accepted JsonNodes back then or something? I don't know. Anyway the accepted answer there does not help me, I actually need a JsonNode and I need for that JsonNode to NOT contain fields that don't belong to the JsonView I want.

K. M
  • 867
  • 1
  • 8
  • 17

0 Answers0