Our JsonObject derives from Dictionary<string, JsonValue>, where JsonValue is just a boxing class with a few typecasting operators for primitive types and JsonObject itself (so you can nest objects). Say you have a data class like this used for tags:
public class MyTag
{
public string Name { get; set; }
public double Value { get; set; }
public bool Flag { get; set; }
}
you can serialize by writing to / reading from the dictionary:
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"];
}
}
and handle the events:
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;
}
}
and now you should see tags in this format in the file:
"tag":{"Name":"test","Value":0.5,"Flag":true}
I think we implemented our JSON API very shortly before System.Text.Json became available in standard dotnet, we'll be migrating to the latter when time allows.
Regards,
Slavcho
Mindfusion