Class FlexibleJsonModel


  • public abstract class FlexibleJsonModel
    extends Object
    This class can be used as a base class for situations where there may be additional/unknown properties in a JSON string that don't have a corresponding property in the class being de-serialized into. It uses Jackson's JsonAnyGetter and JsonAnySetter feature to get and set these "extra fields" in a property named extraFields.

    The general use case for this "extra fields" is when a service or application proxies data from a canonical source where we don't want or need to know about every individual field, but where we still want to capture those fields so we can store, display, etc. them. For example, a remote service that has a large number of properties and we only care about a few of them, but we might want to store all of them for analytics, debugging, etc.

    This implementation intentionally does not define equals or hashCode methods, which means the extra fields will not be included in equality checks unless subclasses choose to include them. It does, however, include a default toString method that will print out the extra fields. Whether to include extra fields in equality checks depends heavily on the use case, so subclasses must decide for themselves. In addition, many times simple data holder objects don't need to be compared so don't need equals or hashCode.

    • Constructor Detail

      • FlexibleJsonModel

        public FlexibleJsonModel()
    • Method Detail

      • getExtraFields

        public Map<String,​Object> getExtraFields()
        Returns the "extra" fields that were not explicitly defined as properties when this object was de-serialized from JSON.
        Returns:
        the extra fields
      • setExtraFields

        public void setExtraFields​(String key,
                                   Object value)
        Add an "extra" field when de-serializing from JSON.
        Parameters:
        key - the extra field name
        value - the value of the extra field