Skip to main content

serialize

Function serialize 

Source
pub fn serialize(diagram: &Diagram) -> Result<String, SerializeError>
Expand description

Serializes diagram into formatted JSON matching the description format read by parse.

The output is 2-space pretty-printed JSON ending in a newline, with next_id and the shapes array in drawing order from back to front.

§Errors

SerializeError when the description could not be converted to JSON.

§Examples

let diagram = parse(
    r#"{
        "next_id": 2,
        "shapes": [ { "kind": "box", "id": 1, "at": { "x": 0, "y": 0 },
                      "size": { "width": 4, "height": 3 }, "stroke": "light" } ]
    }"#,
)
.expect("a well-formed description");

let json = serialize(&diagram).expect("serializes to JSON");
assert!(json.contains("\"next_id\": 2"));