devalue icon indicating copy to clipboard operation
devalue copied to clipboard

try to use "toJSON" method before converting named objects to string

Open vl-skripkin opened this issue 5 years ago • 1 comments

Nuxt uses devalue to pass vuex state from server- to client-side. And at the moment devalue converts simple objects to "json format" as is, key by key. But sometimes it is usefull to make some changes in a data before this conversion, "toJSON" method should be used for this.

For example, I create data models with default empty values on server-side, but due to some logic of my app the models were left as is, with default values, so there is no need to pass all keys of empty models to client-side, because I can create new empty models on client-side. So in this case the data model should have "toJSON" method, and devalue should call it. The method may look like this:

toJSON ()
{
	return is_new(this) ? {} : this;
}

vl-skripkin avatar Sep 09 '19 19:09 vl-skripkin

Thanks for PR. Please make a proper description of this change. An example of the edge case it is fixing, etc. Also would be a good idea to attempt fixing this issue at mainstream: https://github.com/Rich-Harris/devalue

If it not works for everyone (a nuxt users requirement) we can then add patch here.

pi0 avatar Sep 10 '19 09:09 pi0