Ways to serialize/deserialize schema spec


Hey, I am wondering about how to serialize/deserialize the schema spec. I want to save it separately in a database in the form of JSON. There seem to be two things stopping from doing that right now:

let spec = view.state.schema.spec
let specCopy = JSON.parse(JSON.stringify(spec))
  1. specCopy is missing the toDOM methods. (I think i can live with this as I need to use it on the server.)
  2. spec.nodes and spec.marks are OrderedMaps, whereas specCopy.nodes and specCopy.marks are arrays.

I see that new Schema({nodes, marks}) accepts Objects instead of OrderedMaps for nodes and marks. I wonder if it could not also be made to accept these types of arrays that OrderedMaps are being serialized into, or if there is some other way of storing a spec in pure JSON data that I have not thought about?


Schema specs contain function values, and thus can’t be serialized as JSON. And no, making new Schema accept the gook you get when you JSON-serialize an OrderedMap is not something I’m interested in.


well, there is always

JSON.stringify(spec, (k,v) => typeof v === "function" ? "" + v : v);

which does serialize it.

For others who might want to save the spec or move it around, this seems to be the solution to reload it:

    (k,v) => 
       typeof v === "string" && v.startsWith('function ') ? 
           eval(`(${v})`) : 
       ['nodes', 'marks'].includes(k) && v.content ? 
           new OrderedMap(v.content) :