Strip nils from Model serialization for more compact representation #175
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
We want to mostly keep unnecessary nils out of serializations, to keep them more compact. This will become even more relevant if we move to a full
AttrJson::Attributes
implementation -- or something semantically like it -- where by default all attributes are included in serialized hash, even if never set. We don't really need to serialize them all in postgres.AttrJson::Model#serializable_hash
(and #as_json) now have a new optionalstrip_attributes
keyword argument. Default isfalse
, do same as traditional.true
leaves keys for all nil values out of the hash -- but that causes problems for trying to save anil
written on top of another default. So:safely
strips nils only for attributes without defaults.The
AttrJson::Model::Type
type class will by default serialize with:safely
-- stripping unnecessary nils. But you can change this with a paramter, that you can also pass toAttrJson::Model#to_type
. So for instance, when embedding a model somewhere:If set to to
strip_nils: true
, the effect is when deserializing a model with an attribute with a default, the default ends up re-applied on top of previously nil values. Kind of making the default applied not just on original creation, but on every de-serialization too.