super_editor
super_editor copied to clipboard
Transform document to Json
User Behavior
- Save document into database
- Convert document to JSON
Feature Value
- Save the state of a document including content and styles
- Recover a document saved from a JSON
Additional context Its interesting a simple way to transform document state to Map<String, dynamic> for store and recover. The opposite transformation is useful tool, like Document.fromMap(Map<String, dynamic> map), for recover a saved document.
I would like to use this feature do save my work in a database and recover after.
I didn't find any way to do that in the documentation, wiki and example
OBS: I would like to contribute to this project. It's an incredible project.
There are two high level ways that JSON serialization might make it's way into SuperEditor
.
The first option is to write a serializer/deserializer around the existing Document
and DocumentNode
variations. The serializer would query every property of every node and put those properties into maps within the overall JSON object. The deserializer would read from that JSON and then recreate the Document
and the DocumentNode
s.
The second option is to force every DocumentNode
to have a fromJson()
and toJson()
method. The serializer and deserializer would then call those methods.
JSON is one of an infinite variety of serialization protocols. XML and Protobufs are a couple other possible serialization protocols. As such, we shouldn't bake any special preference into SuperEditor
. The first option above would remain agnostic, but the second option above would give special treatment to JSON.
The first option above, in theory, wouldn't require any changes to existing SuperEditor
DocumentNode
s, but the second option would require changes to all of them.
Therefore, if JSON serialization is desired, I think it should be added in the same way we've provided Markdown serialization support, which is to define top-level serializer and deserializer functions. Those functions can read everything in a Document
and produce desired JSON, as well as the reverse.
Does that make sense? Have you looked at the Markdown support that we already have?
@talisonfc do you have any followup based on my comment above?
Yes. I'm thinking about using markdown for now. I've started writing parsers for Nodes, but I'm still evaluating.
I'm writing an application where some parts of the text are analyzed by an api. For me, JSON provides a better approach to parsing objects and following the Slate structure.
Top level JSON serializer and deserializer would be perfect for preserving all the styling not supported by markdown like background color, text color etc.