Page Index Toggle Pages: 1 Send TopicPrint
Normal Topic Serialization to JSON (saving and loading Tags) (Read 697 times)
David Long
Full Member
***
Offline


I love YaBB 1G - SP1!

Posts: 153
Location: England
Joined: Oct 23rd, 2006
Serialization to JSON (saving and loading Tags)
Nov 17th, 2025 at 11:23am
Print Post  
Could you provide some example code showing how to Serialize and Deserialize DiagramItem Tags using the events JsonSerializeTag and JsonDeserializeTag?

Thank you.
DavidL
  
Back to top
 
IP Logged
 
Slavcho
YaBB Moderator
*****
Offline


tech.support

Posts: 3435
Joined: Oct 19th, 2005
Re: Serialization to JSON (saving and loading Tags)
Reply #1 - Nov 17th, 2025 at 12:16pm
Print Post  
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:

Code
Select All
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:

Code
Select All
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:

Code
Select All
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:

Code
Select All
"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
  
Back to top
 
IP Logged
 
David Long
Full Member
***
Offline


I love YaBB 1G - SP1!

Posts: 153
Location: England
Joined: Oct 23rd, 2006
Re: Serialization to JSON (saving and loading Tags)
Reply #2 - Nov 17th, 2025 at 9:15pm
Print Post  
That looks good. I will give it a go.
DavidL
  
Back to top
 
IP Logged
 
David Long
Full Member
***
Offline


I love YaBB 1G - SP1!

Posts: 153
Location: England
Joined: Oct 23rd, 2006
Re: Serialization to JSON (saving and loading Tags)
Reply #3 - Nov 18th, 2025 at 5:08pm
Print Post  
How do I handle more complex types in my DiagramItem tags. For instance, I have:

string[]
List<int>
Color
DateTime
List<string[]>
Enumerated types.

DavidL
  
Back to top
 
IP Logged
 
Slavcho
YaBB Moderator
*****
Offline


tech.support

Posts: 3435
Joined: Oct 19th, 2005
Re: Serialization to JSON (saving and loading Tags)
Reply #4 - Nov 19th, 2025 at 8:19am
Print Post  
List and arrays: add JsonValue objects to ArayList, e.g. this is how we the control saves images internally:

Code
Select All
// save
ArrayList images = new ArrayList();
foreach (Image image in imageList)
     images.Add(context.WriteImage(image));
obj.Add("images", new JsonValue(images));

// load
if (obj.GetValue("images") != null)
{
    ArrayList imagesObj = obj["images"].ToArrayList();
    foreach (string data in imagesObj)
    {
        Image image = context.ReadImage(data);
    }
} 



I can see JsonValue has DateTime constructor and operator, so try using it directly.

Colors and enums: call WriteColor, ReadColor, WriteEnum, ReadEnum methods of e.Context.  You'd need to typecast to your enum type when calling the latter.

In any case, our developer will try to add built-in serialization using System.Text.Json's JsonSerializer for Tag objects at least, we should have preview build later this week.

Regards,
Slavcho
Mindfusion
  
Back to top
 
IP Logged
 
David Long
Full Member
***
Offline


I love YaBB 1G - SP1!

Posts: 153
Location: England
Joined: Oct 23rd, 2006
Re: Serialization to JSON (saving and loading Tags)
Reply #5 - Nov 19th, 2025 at 11:09am
Print Post  
Ok, I will hang on until then and see what the new version will do. Also, I forgot to say that I also have a TimeSpan in my DiagramItem tag.

DavidL
  
Back to top
 
IP Logged
 
Slavcho
YaBB Moderator
*****
Offline


tech.support

Posts: 3435
Joined: Oct 19th, 2005
Re: Serialization to JSON (saving and loading Tags)
Reply #6 - Nov 19th, 2025 at 11:27am
Print Post  
For TimeSpan you might try saving TimeSpan.Ticks, and then pass the long-int to TimeSpan constructor when loading.

We have the DateTime support in JsonValue because our Scheduler control's API keeps StartTime and EndTime properties for items, but no TimeSpan / durations at this time.

Regards,
Slavcho
Mindfusion
  
Back to top
 
IP Logged
 
Slavcho
YaBB Moderator
*****
Offline


tech.support

Posts: 3435
Joined: Oct 19th, 2005
Re: Serialization to JSON (saving and loading Tags)
Reply #7 - Nov 21st, 2025 at 2:24pm
Print Post  
dotnet8+ assemblies from this build now serialize tags using JsonSerializer from System.Text.Json:

https://www.nuget.org/packages/MindFusion.Diagramming/7.1.2-beta1

That's not implemented in old 4.X framework build as System.Text.Json is not available out of the box for it. Let us know if you need it there and we could target it through nuget package (https://www.nuget.org/packages/system.text.json/).

Saving diagram to XML file now uses XmlSerializer for Tag values as well, in case someone wants to try that.

Regards,
Slavcho
Mindfusion
  
Back to top
 
IP Logged
 
Slavcho
YaBB Moderator
*****
Offline


tech.support

Posts: 3435
Joined: Oct 19th, 2005
Re: Serialization to JSON (saving and loading Tags)
Reply #8 - Nov 21st, 2025 at 2:33pm
Print Post  
With that you might still have to implement custom serialization code for non-primitive types, using converters:

https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json...
  
Back to top
 
IP Logged
 
David Long
Full Member
***
Offline


I love YaBB 1G - SP1!

Posts: 153
Location: England
Joined: Oct 23rd, 2006
Re: Serialization to JSON (saving and loading Tags)
Reply #9 - Nov 24th, 2025 at 2:23pm
Print Post  
As usual I have been pulled off on to other issues. I hope to get back to this in a few days.
DavidL
  
Back to top
 
IP Logged
 
Page Index Toggle Pages: 1
Send TopicPrint