Skip to content

JSON.stringify ignores "internal fields" #319

@tvillaren

Description

@tvillaren

Following update introducing the toJSON method in the Model class (see #255), the JSON.Stringify method (which uses the Object-defined toJSON if it exists, as explained on MDN), now ignores the internal fields introduced by Vuex ORM and its plugin.

In versions before 0.31.3, I had:

const user = User.find(1);
console.log(JSON.stringify(user)) \\ output => { "$id": 1, "Id": 1, "name": "John Doe" }

now:

const user = User.find(1);
console.log(JSON.stringify(user)) \\ output => { "Id": 1, "name": "John Doe" }

While this might not be an issue for basic use, digging into the Serializer code, it seems that only fields declared under the static fields() method on the class definition will be serialized.

In means for instance that field defined outside of the fields() method on the class definition will be ignored when stringifying.
I bumped into this while updating my app to last version on Vuex ORM and using my plugin (https://github.com/vuex-orm/plugin-change-flags): the change flags are not serialized anymore (which I need to send to the server...).

I will probably update the plugin to include the flags into the fields() method. I'm not sure what is the best generic behavior, at least having this issue opened can help others find out about this.

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingreleasedThe issue was implemented and it is released publicly

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions