JSON serialization for Quillon AST nodes.
Converts between Elixir AST tuples and JSON-friendly maps.
Uses String.to_existing_atom/1 for security when deserializing.
Summary
Functions
Convert a JSON map to an AST node.
Convert a JSON map to an AST node, raising on error.
Convert an AST node to a JSON-friendly map.
Functions
Convert a JSON map to an AST node.
Uses String.to_existing_atom/1 for node types and marks to prevent
atom table exhaustion attacks. Only atoms defined in Quillon.Types
are valid.
Examples
iex> Quillon.JSON.from_json(%{"type" => "paragraph", "attrs" => %{}, "children" => []})
{:ok, {:paragraph, %{}, []}}
iex> Quillon.JSON.from_json(%{"type" => "unknown", "attrs" => %{}, "children" => []})
{:error, "Unknown node type: unknown"}
Convert a JSON map to an AST node, raising on error.
Examples
iex> Quillon.JSON.from_json!(%{"type" => "paragraph", "attrs" => %{}, "children" => []})
{:paragraph, %{}, []}
Convert an AST node to a JSON-friendly map.
Examples
iex> Quillon.JSON.to_json({:paragraph, %{}, [{:text, %{text: "Hi", marks: []}, []}]})
%{
"type" => "paragraph",
"attrs" => %{},
"children" => [
%{"type" => "text", "attrs" => %{"text" => "Hi", "marks" => []}, "children" => []}
]
}
iex> Quillon.JSON.to_json({:text, %{text: "Bold", marks: [:bold, {:link, %{href: "/"}}]}, []})
%{
"type" => "text",
"attrs" => %{
"text" => "Bold",
"marks" => ["bold", %{"type" => "link", "attrs" => %{"href" => "/"}}]
},
"children" => []
}