Adding custom JSON serialization for nodes and marks

So this has been asked before Custom toJSON and fromJSON for a node but I just wondered whether toJSON and fromJSON could be included in the NodeSpec and MarkSpec? Seems quite intuitive since toDOM and parseDOM exist already.

I think it’s quite frequent to have UI-specific attributes so being able to tidy up the JSON would be nice. Especially with the redundant default attributes. No need to add your own hacky wrappers around the specs.

I’m not sure what UI-specific attributes are.

But I don’t really see much appeal in complicating the JSON serialization format by allowing custom structure. You can of course, if you’re doing the serialization and deserialization, run your own custom logic. But ProseMirror defines a schema, that 3rd party code also relies on, and I don’t feel like changing that is a good idea.

I have some random attributes that eg mark the last adjacent mark. Which I can’t otherwise select with CSS and decorations add another complexity that you have to keep track of.

I see. Not even a switch to turn off the default attributes from the JSON? They tend to generate a quite bit of noise and are not strictly necessary. But anyway, I suppose I can deal with it.

We have a use-case here, where the ProseMirror document is a child of a larger non-ProseMirror document schema. All nodes of this JSON document need the ability to append some “instructions” for manipulation in a separate service. The service requires these instructions to be in a key at the root of the node to be manipulated.

Without the ability for us to add this data to the node in question within ProseMirror forces us to create some kind of transformation in and out of the app layer, which is less idea. We are exploring other avenues, but this would provide quite a bit of flexibility.

Speaking of custom serialization/deserialization of ProseMirror. @marijn do you have any examples of such a thing?

It’s not supported. You’ll have to write your own transformation code. This should be pretty easy.