Search
DiagramBase.JsonSerializeTag Event
See Also
 





Raised when the Tag of a diagram item must be serialized to JSON.

Namespace: MindFusion.Diagramming
Assembly: MindFusion.Diagramming

 Syntax

C#  Copy Code

public event EventHandler<JsonSerializeTagEventArgs> JsonSerializeTag

Visual Basic  Copy Code

Public Event JsonSerializeTag As EventHandler(Of JsonSerializeTagEventArgs)

 Remarks

You can handle this event to serialize non-primitive Id or Tag objects. If not handled, the component uses System.Text.Json.JsonSerializer to get JSON representation of current object.

 Example

The following code shows how to serialize a simple structure as a JsonObject. JsonObject derives from Dictionary<string, JsonValue>, where JsonValue is a boxing class containing typecasting operators for primitive types and JsonObject itself (so you could nest objects).

C#  Copy Code

public class MyTag
{
    public string Name { get; set; }
    public double Value { get; set; }
    public bool Flag { get; set; }
}

static public class MyTagExtensions
{
    static public JsonObject WriteJson(this MyTag tag)
    {
        return new JsonObject
        {
            { "Name", tag.Name },
            { "Value", tag.Value},
            { "Flag", tag.Flag }
        };
    }

    static public void ReadJson(this MyTag tag, JsonObject json)
    {
        tag.Name = json["Name"];
        tag.Value = json["Value"];
        tag.Flag = json["Flag"];
    }
}

void OnJsonSerializeTag(object sender, JsonSerializeTagEventArgs e)
{
    if (e.PropertyName == "Tag" && e.Tag is MyTag myTag)
    {
        e.Representation = myTag.WriteJson();
        e.Handled = true;
    }
}

void OnJsonDeserializeTag(object sender, JsonSerializeTagEventArgs e)
{
    if (e.PropertyName == "Tag" && e.Object is DiagramItem item)
    {
        var myTag = new MyTag();
        myTag.ReadJson(e.Representation);
        e.Tag = myTag;
        e.Handled = true;
    }
}

 See Also