Skip to content

JSON Features

Tatu Saloranta edited this page Jun 2, 2015 · 8 revisions

Jackson jr: Features

Following is a list of JSON.Feature values available; these can be enabled/disabled by calls to JSON; result is a new JSON object and typical usage is by chaining calls like:

byte[] bytes = JSON.std
    .with(Feature.PRETTY_PRINT_OUTPUT)
    .without(Feature.WRITE_NULL_PROPERTIES)
    .asBytes(bean);

but it is also possible to store and reuse previously configured JSON instances:

JSON configuredJson = JSON.std
    .with(Feature.PRETTY_PRINT_OUTPUT)
    .without(Feature.WRITE_NULL_PROPERTIES);
byte[] bytes = configuredJson.asBytes(bean);

Note that cost of creating new JSON instances is marginal. Also worth noting is that JSON instances are fully immutable, so with() and without() calls never change state of existing instances but create new instances. This means that JSON is fully and completely thread-safe, and usage never needs to be synchronized.

TODO: write descriptions !!!

Writer features

  • WRITE_NULL_PROPERTIES (default: false)
    • Feature that defines what to do with java.util.Map entries and Java Bean properties that have null as value: if enabled, they will be written out normally; if disabled, such entries and properties will be ignored.
  • WRITE_READONLY_BEAN_PROPERTIES (default: true)
    • Feature that determines whether "read-only" properties of Beans (properties that only have a getter but no matching setter) are to be included in Bean serialization or not; if disabled,only properties have have both setter and getter are serialized. Note that feature is only used if Feature.HANDLE_JAVA_BEANS is also enabled.
  • WRITE_ENUMS_USING_INDEX (default: false)
    • Feature that determines whether Enum values are written using numeric index (true), or String representation from calling Enum.toString() (false)
  • PRETTY_PRINT_OUTPUT (default: false)
    • Feature that can be enabled to use "pretty-printing", basic indentation to make resulting JSON easier to read by humans by adding white space such as line feeds and indentation.
    • NOTE: details of pretty-printing are handled by underlying JsonGenerator
  • FLUSH_AFTER_WRITE_VALUE (default: true)
    • Feature that determines whether JsonGenerator.flush() is called after write() method that takes JsonGenerator as an argument completes (that is, does NOT affect methods that use other destinations).
    • Flushing usually makes sense; but there are cases where flushing should not be forced: for example when underlying stream is compressing and flush() causes compression state to be flushed (which occurs with some compression codecs).
  • FAIL_ON_UNKNOWN_TYPE_WRITE (default: false)
    • Feature that determines what happens when we encounter a value of unrecognized type for which we do not have a standard handler: if enabled, will throw a JSONObjectException; if disabled simply calls Object.toString() and uses that JSON String as serialization.
    • NOTE: if HANDLE_JAVA_BEANS is enabled, this setting typically has no effect, since otherwise unknown types are recognized as Bean types and handled appropriately.

Reader features

  • USE_BIG_DECIMAL_FOR_FLOATS (default: false) *
  • READ_JSON_ARRAYS_AS_JAVA_ARRAYS (default: false) *
  • READ_ONLY (default: false) *
  • PRESERVE_FIELD_ORDERING (default: true) *
  • USE_DEFERRED_MAPS (default: true) *
  • USE_IS_SETTERS (default: true) *
  • FAIL_ON_DUPLICATE_MAP_KEYS (default: true) *
  • FAIL_ON_UNKNOWN_BEAN_PROPERTY (default: false) *

Other features

  • HANDLE_JAVA_BEANS (default: true) *
  • FORCE_REFLECTION_ACCESS (default: true) *
Clone this wiki locally