json-dry icon indicating copy to clipboard operation
json-dry copied to clipboard

Rehydrating setters & getters

Open nevf opened this issue 7 years ago • 6 comments

I have Javascript classes which use setters & getters for property access. These are serialized to a database. Upon retrieval I'd like to rehydrate the setters/getters. I assume json-dry can do this. If so could you kindly add an example to the docs - thanks.

nevf avatar Jan 21 '18 22:01 nevf

@nevf: Technically the Person class example should tell you all you need.

I only added a method to my Person example class, but setters and getters would also work, naturally.

Keep in mind: the class itself is not added to the stringified result. That's why you have to do Dry.registerClass(Person); after creating the class, and add a toDry and unDry method to the class.

Is this different to how you interpreted the documentation? Do tell, I'd love to make it more clear.

skerit avatar Jan 22 '18 11:01 skerit

@skerit Thanks. I assumed there wouldn't be an issue with setters/getters but wanted to check. I realy need to try out json-dry to get a better understanding of how it works. The docs are good.

Another question. Am I correct that toDry() needs to include all the properties you wish to serialize? If so and you simply want serialize all properties can toDry() somehow be dispensed with. My concern is code duplication with the constructor and keeping the two in sync.

nevf avatar Jan 22 '18 19:01 nevf

Yes, only the value property of the object you return in toDry will be passed to unDry.

In theory you could do something like

Person.prototype.toDry = function toDry() {
    return {
        value: Object.assign({}, this)
    };
};

Then you would serialize ALL enumerable properties, though I'd recommend against it :)

About code duplication: most of my toDry implementations just serialize the original arguments for the constructor, and the unDry implementation then just calls the constructor with these values.

In my SeededRng class for example I added the original constructor argument + a few extra states that could have been overridden.

skerit avatar Jan 22 '18 19:01 skerit

@skerit Thanks, understood.

I've just been reading about Decorators and wonder whether they could be used to control which properties in the constructor get serialized. If so that would remove the property duplication and toDry could just add any extra properties as per your SeededRng class. I haven't used decorators, so this could well be a non-starter.

nevf avatar Jan 22 '18 21:01 nevf

In theory yes, but then you would have to write some kind of decorator that would handle all that logic (also adding the toDry method). Not at all impossible, but just to be clear: that's not something for the core of json-dry :)

And then another caveat: there currently are no decorators in JavaScript as most people know them. No fancy @enableDry() or something.

skerit avatar Jan 23 '18 18:01 skerit

I noticed that Slim.js was using decorators which piqued my interest, but you are right that they aren't supported yet. That said https://babeljs.io/docs/plugins/transform-decorators/ is available.

nevf avatar Jan 24 '18 22:01 nevf