0

Is ObjectMapper supposed to work with a class which only has private members, no constructor and no getter/setters ?

I tried this, but it does not solve the problem.

mapper.setVisibility(JsonMethod.FIELD, JsonAutoDetect.Visibility.ANY);
Andy897
  • 6,915
  • 11
  • 51
  • 86

1 Answers1

0

http://www.baeldung.com/jackson-field-serializable-deserializable-or-not

static class MyDtoAccessLevel {
    private String stringValue = "hidden";
}

public static void main(String[] args) throws JsonProcessingException {
    ObjectMapper mapper = new ObjectMapper();
    mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
    MyDtoAccessLevel dtoObject = new MyDtoAccessLevel();
    System.out.println(mapper.writeValueAsString(dtoObject));
    //prints {"stringValue":"hidden"}
}
PJ Fanning
  • 953
  • 5
  • 13
  • Thanks a lot for writing. But I am using org.codehaus.jackson.map.ObjectMapper, which is sort of older version of com.fasterxml.jackson.core. I do not see an option to set PropertyAccessor in older version. :( Thoughts ? – Andy897 Jan 08 '17 at 05:10
  • And I have no clues as to who down voted your answer. – Andy897 Jan 08 '17 at 05:10
  • My code sample is for Jackson 2.x. Jackson 1.x is no longer maintained. Would it be feasible to switch? – PJ Fanning Jan 08 '17 at 05:12
  • No, it is a big big code base, where 100s of dependencies are using 1.x :( – Andy897 Jan 08 '17 at 05:14
  • You can have both jackson 1.x and 2.x jars on your classpath because they use different package names. – PJ Fanning Jan 08 '17 at 05:30