Class GraphSerializer
- Namespace
- FlowGraph.Core.Serialization
- Assembly
- FlowGraph.Core.dll
Serializes and deserializes graphs to/from JSON.
public static class GraphSerializer
- Inheritance
-
GraphSerializer
- Inherited Members
Remarks
Supports two format versions:
- Version 2 (default): Uses polymorphic
elements[]array with "kind" discriminator - Version 1 (legacy): Uses separate
nodes[],edges[],shapes[]arrays
Serialization always outputs Version 2 format. Deserialization supports both formats for backward compatibility with older saved files.
Methods
Deserialize(string, JsonSerializerOptions?)
Deserializes a graph from a JSON string.
public static Graph? Deserialize(string json, JsonSerializerOptions? options = null)
Parameters
jsonstringJSON string to deserialize.
optionsJsonSerializerOptionsOptional JSON serializer options. Uses default if not specified.
Returns
- Graph
Deserialized graph, or null if deserialization fails.
DeserializeFromFileAsync(string, JsonSerializerOptions?)
Deserializes a graph from a JSON file.
public static Task<Graph?> DeserializeFromFileAsync(string filePath, JsonSerializerOptions? options = null)
Parameters
filePathstringPath to the JSON file.
optionsJsonSerializerOptionsOptional JSON serializer options. Uses default if not specified.
Returns
Serialize(Graph, JsonSerializerOptions?)
Serializes a graph to a JSON string.
public static string Serialize(Graph graph, JsonSerializerOptions? options = null)
Parameters
graphGraphThe graph to serialize.
optionsJsonSerializerOptionsOptional JSON serializer options. Uses default if not specified.
Returns
- string
JSON string representation of the graph.
SerializeToFileAsync(Graph, string, JsonSerializerOptions?)
Serializes a graph to a JSON file.
public static Task SerializeToFileAsync(Graph graph, string filePath, JsonSerializerOptions? options = null)
Parameters
graphGraphThe graph to serialize.
filePathstringPath to the output file.
optionsJsonSerializerOptionsOptional JSON serializer options. Uses default if not specified.
Returns
TryValidate(string, out string?)
Validates JSON without fully deserializing.
public static bool TryValidate(string json, out string? errorMessage)
Parameters
jsonstringJSON string to validate.
errorMessagestringError message if validation fails, null otherwise.
Returns
- bool
True if JSON is valid, false otherwise.