-
-
Notifications
You must be signed in to change notification settings - Fork 52
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Skip serialization / deserialization #32
Conversation
This allows custom serializers to conditionally skip setting properties. Can be useful for creating schema's where fields should be settable from incoming JSON, but the fields should not be saved. Or maybe fields should be computed client-side and server-side data should conditionally be ignored.
I've also got a bit more code that passes the property name and the parent object to the Commit is: https://github.com/nathanstitt/serializr/commit/eb079fbcb460ab1fe8855d3c0401ed30077b2bdb I'll make some specs and a follow up PR with it once this one clears (assuming it does?) |
I am wondering whether this change doesn't skip properties to aggresively. It doesn't seem possible to serialize |
Passing parent / propname sounds like useful change indeed. Can you elaborate / demonstrate your case a bit? But it sounds like a sane change |
@mweststrate Thanks for taking a look at the change. The SKIP value idea is also good and would work great for me. That's essentially what I'm doing except using What I'm attempting to do with passing along the propname is to be able to conditionally set or merge values. Currently custom serializers don't have enough information to be able to find the existing value so they can apply smart updates. For some type of models, if the property has quite a bit of extra information that is calculated, it can be expensive to throw it out and start over, it's better to pass it the new values from serializr and let it update itself. |
@mweststrate I've made the changes and added the property name commit as well. Looks like coverall's isn't happy even though I've added a few specs :( Happy to rework if you've got any other suggestions. Thanks! |
function mrFactory() { | ||
// Indicate properties should be skipped | ||
var SKIP = Symbol('SKIP') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note that Symbol is not standard ES5, so something like typeof Symbol !== "undefined" ? Symbol('SKIP') : { SKIP: true }
would be better. I'll try to merge later today, so I might process that change myself
Released as part of 1.1.11 |
This allows custom serializers/deserializers to conditionally skip setting properties.
My use case for this is that I'm migrating a large app that used ampersand-state to Mobx and Serializr.
As part of the migration I've extracted some decorators I found useful to mobx-decorated-models. It's a bunch of decorators that make using Serializr with Mobx a bit easier for my use case. However
ampersand-state
has the concept ofsession
attributes, which are fields that behave just like other attributes in that they can be optionally set from the server, but are not serialized for saving.They're pretty useful for allowing the server to send pre-computed values for things that are difficult/impossible to generate client-side. But it makes no sense for the client to save the values back to the server.
This change will allow me to support that type of functionality. I'm happy to rework in a different fashion if you think something else would be a better way to support this.