'Display Jackson ObjectMapper configuration

Before I invest energy building a utility to interrogate an ObjectMapper to output its configuration via methods like:

objectMapper.getRegisteredModuleIds();
objectMapper.getDeserializationConfig().hasDeserializationFeatures(...);
objectMapper.getSerializationConfig().hasSerializationFeatures(...);

I'd like to know if there's already some way (in Jackson itself or an existing utility) to output the configuration of a given ObjectMapper? For example, if an ObjectMapper is setup like so:

ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JavaTimeModule());
mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
mapper.enable(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL);
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);

I'd like to, after the fact, examine the ObjectMapper and know exactly how it was configured.



Solution 1:[1]

I ended up creating a simple utility method to return a String showing an ObjectMapper's modules and features:

public static String getConfigDetails(ObjectMapper mapper) {
  StringBuilder sb = new StringBuilder();

  sb.append("Modules:\n");
  if (mapper.getRegisteredModuleIds().isEmpty()) {
    sb.append("\t").append("-none-").append("\n");
  }
  for (Object m : mapper.getRegisteredModuleIds()) {
    sb.append("  ").append(m).append("\n");
  }

  sb.append("\nSerialization Features:\n");
  for (SerializationFeature f : SerializationFeature.values()) {
    sb.append("\t").append(f).append(" -> ").append(mapper.getSerializationConfig().hasSerializationFeatures(f.getMask()));
    if (f.enabledByDefault()) {
      sb.append(" (enabled by default)");
    }
    sb.append("\n");
  }

  sb.append("\nDeserialization Features:\n");
  for (DeserializationFeature f : DeserializationFeature.values()) {
    sb.append("\t").append(f).append(" -> ").append(mapper.getDeserializationConfig().hasDeserializationFeatures(f.getMask()));
    if (f.enabledByDefault()) {
      sb.append(" (enabled by default)");
    }
    sb.append("\n");
  }

  return sb.toString();
}

Here's the output on a new ObjectMapper() (Jackson 2.13.2):

Modules:
  -none-

Serialization Features:
  WRAP_ROOT_VALUE -> false
  INDENT_OUTPUT -> false
  FAIL_ON_EMPTY_BEANS -> true (enabled by default)
  FAIL_ON_SELF_REFERENCES -> true (enabled by default)
  WRAP_EXCEPTIONS -> true (enabled by default)
  FAIL_ON_UNWRAPPED_TYPE_IDENTIFIERS -> true (enabled by default)
  WRITE_SELF_REFERENCES_AS_NULL -> false
  CLOSE_CLOSEABLE -> false
  FLUSH_AFTER_WRITE_VALUE -> true (enabled by default)
  WRITE_DATES_AS_TIMESTAMPS -> true (enabled by default)
  WRITE_DATE_KEYS_AS_TIMESTAMPS -> false
  WRITE_DATES_WITH_ZONE_ID -> false
  WRITE_DATES_WITH_CONTEXT_TIME_ZONE -> true (enabled by default)
  WRITE_DURATIONS_AS_TIMESTAMPS -> true (enabled by default)
  WRITE_CHAR_ARRAYS_AS_JSON_ARRAYS -> false
  WRITE_ENUMS_USING_TO_STRING -> false
  WRITE_ENUMS_USING_INDEX -> false
  WRITE_ENUM_KEYS_USING_INDEX -> false
  WRITE_NULL_MAP_VALUES -> true (enabled by default)
  WRITE_EMPTY_JSON_ARRAYS -> true (enabled by default)
  WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED -> false
  WRITE_BIGDECIMAL_AS_PLAIN -> false
  WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS -> true (enabled by default)
  ORDER_MAP_ENTRIES_BY_KEYS -> false
  EAGER_SERIALIZER_FETCH -> true (enabled by default)
  USE_EQUALITY_FOR_OBJECT_ID -> false

Deserialization Features:
  USE_BIG_DECIMAL_FOR_FLOATS -> false
  USE_BIG_INTEGER_FOR_INTS -> false
  USE_LONG_FOR_INTS -> false
  USE_JAVA_ARRAY_FOR_JSON_ARRAY -> false
  FAIL_ON_UNKNOWN_PROPERTIES -> true (enabled by default)
  FAIL_ON_NULL_FOR_PRIMITIVES -> false
  FAIL_ON_NUMBERS_FOR_ENUMS -> false
  FAIL_ON_INVALID_SUBTYPE -> true (enabled by default)
  FAIL_ON_READING_DUP_TREE_KEY -> false
  FAIL_ON_IGNORED_PROPERTIES -> false
  FAIL_ON_UNRESOLVED_OBJECT_IDS -> true (enabled by default)
  FAIL_ON_MISSING_CREATOR_PROPERTIES -> false
  FAIL_ON_NULL_CREATOR_PROPERTIES -> false
  FAIL_ON_MISSING_EXTERNAL_TYPE_ID_PROPERTY -> true (enabled by default)
  FAIL_ON_TRAILING_TOKENS -> false
  WRAP_EXCEPTIONS -> true (enabled by default)
  ACCEPT_SINGLE_VALUE_AS_ARRAY -> false
  UNWRAP_SINGLE_VALUE_ARRAYS -> false
  UNWRAP_ROOT_VALUE -> false
  ACCEPT_EMPTY_STRING_AS_NULL_OBJECT -> false
  ACCEPT_EMPTY_ARRAY_AS_NULL_OBJECT -> false
  ACCEPT_FLOAT_AS_INT -> true (enabled by default)
  READ_ENUMS_USING_TO_STRING -> false
  READ_UNKNOWN_ENUM_VALUES_AS_NULL -> false
  READ_UNKNOWN_ENUM_VALUES_USING_DEFAULT_VALUE -> false
  READ_DATE_TIMESTAMPS_AS_NANOSECONDS -> true (enabled by default)
  ADJUST_DATES_TO_CONTEXT_TIME_ZONE -> true (enabled by default)
  EAGER_DESERIALIZER_FETCH -> true (enabled by default)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Brice Roncace