0

I'm searching for a way to convert just a partial object (not all attrbiutes of it) via jackson to json. At the moment I'm using this solution:

User userServed = User.find.byId(id);
ObjectMapper mapper = new ObjectMapper();
JsonNode node = null;
node = mapper.convertValue(userServed, JsonNode.class);

It works fine so far.

My Problem is that I have a lot of attributes in the Object I don't want to expose to the json node like lists. Is there a way maybe to select just the needed attributes?

Erik Wolf
  • 144
  • 1
  • 12

1 Answers1

1

Can you annotate the field with JsonIgnore?

From http://wiki.fasterxml.com/JacksonAnnotations

@JsonIgnore (method/field): annotation used to completely disregard annotated method, regardless of auto-detection or other annotations

EDIT: Or use the annotation JsonIgnoreProperty.

(Jackson 1.4+) @JsonIgnoreProperties (class) can be used to indicate that certain properties are to be ignored for serialization and/or deserialization (handling differs a bit depending on which operation is affected):

◦String[] value() defines logical property names to ignore (names derived from getter/setter names, or by explicit annotations)

◦boolean ignoreUnknown() defines whether "unknown" JSON properties can be silently ignored during deserialization or not; does not affect serialization.

Jose Martinez
  • 11,452
  • 7
  • 53
  • 68