Page Index Toggle Pages: 1 Send TopicPrint
Normal Topic Reading from JSON file - duplicate key (Read 342 times)
David Long
Full Member
***
Offline


I love YaBB 1G - SP1!

Posts: 167
Location: England
Joined: Oct 23rd, 2006
Reading from JSON file - duplicate key
Aug 3rd, 2026 at 11:34am
Print Post  
When reading a Diagram from from two of my test files, I get the error "An item with the same key has already been added. key: visible

No fields with the name 'visible' are used in any of my box Tags, but on looking at the JSON text, I see that 'visible' is used many times in the Diagram. On a quick inspection, I can't spot any duplicates.

Can you help?  I can provide the JSON file if required.

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


tech.support

Posts: 3510
Joined: Oct 19th, 2005
Re: Reading from JSON file - duplicate key
Reply #1 - Aug 3rd, 2026 at 12:41pm
Print Post  
Please attach one of the files that shows the error.

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


I love YaBB 1G - SP1!

Posts: 167
Location: England
Joined: Oct 23rd, 2006
Re: Reading from JSON file - duplicate key
Reply #2 - Aug 3rd, 2026 at 12:54pm
Print Post  
Can I email the file to keep the file more private?
DavidL
  
Back to top
 
IP Logged
 
Slavcho
YaBB Moderator
*****
Offline


tech.support

Posts: 3510
Joined: Oct 19th, 2005
Re: Reading from JSON file - duplicate key
Reply #3 - Aug 3rd, 2026 at 12:58pm
Print Post  
Sure, email it to support@mindfusion.eu, and / or replace all Text values with empty strings.

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


I love YaBB 1G - SP1!

Posts: 167
Location: England
Joined: Oct 23rd, 2006
Re: Reading from JSON file - duplicate key
Reply #4 - Aug 5th, 2026 at 10:18am
Print Post  
Conversation continued via email.

Solution was to remove any duplicate nodes from Diagram.Nodes when reading the binary format file.  Something like this...

            if (flowChart.Nodes.Count != flowChart.Nodes.Distinct().Count())
            {
                var nodes = flowChart.Nodes.Distinct().ToList();

                flowChart.Nodes.Clear();
                foreach (var node in nodes)
                    flowChart.Nodes.Add(node);
            }


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


tech.support

Posts: 3510
Joined: Oct 19th, 2005
Re: Reading from JSON file - duplicate key
Reply #5 - Aug 6th, 2026 at 8:35am
Print Post  
Note that this cleanup code works for the matrix-like diagram we investigated yesterday, but if a diagram contain links, it will also delete them due to AutoDeleteLinks. If your application allows drawing links too, you'd need to disable AutoDeleteLinks, and possibly reconnect them to the remaining node.

Have you actually found ValidityChecks mentioned in the application's code, which is our main suspect for the duplicated reference, or our developers should look for other ways that could have happened?

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


tech.support

Posts: 3510
Joined: Oct 19th, 2005
Re: Reading from JSON file - duplicate key
Reply #6 - Aug 10th, 2026 at 2:29pm
Print Post  
This build adds following method to Diagram class to make duplicate cleanup easier:

https://www.nuget.org/packages/MindFusion.Diagramming/7.2.1-beta4

Code
Select All
[EditorBrowsable(EditorBrowsableState.Never)]
public void RemoveDuplicateItems()
{
	zOrder.RemoveDuplicates();
	nodes.RemoveDuplicates();
	links.RemoveDuplicates();
	groups.RemoveDuplicates();
} 



Our test code is:

Code
Select All
diagram.ClearAll();

List<ShapeNode> nodes = new List<ShapeNode>();
for (int i = 0; i < 100; i++)
	nodes.Add(diagram.Factory.CreateShapeNode(i, i, 10, 10));

List<DiagramLink> links = new List<DiagramLink>();
for (int i = 0; i < 10; i++)
	links.Add(diagram.Factory.CreateDiagramLink(nodes[i], nodes[i + 1]));

// Add duplicates at random indices
diagram.ValidityChecks = false;
Random rnd = new Random();
for (int i = 0; i < 10; i++)
{
	int nodeIdx = rnd.Next(nodes.Count);
	int insertIdx = rnd.Next(diagram.Nodes.Count);
	diagram.Nodes.Insert(insertIdx, nodes[nodeIdx]);
}
for (int i = 0; i < 5; i++)
{
	int linkIdx = rnd.Next(links.Count);
	int insertIdx = rnd.Next(diagram.Links.Count);
	diagram.Links.Insert(insertIdx, links[linkIdx]);
}

int nodeCountWithDuplicates = diagram.Nodes.Count;
int linkCountWithDuplicates = diagram.Links.Count;

diagram.RemoveDuplicateItems();

int nodeCountAfterRepair = diagram.Nodes.Count;
int linkCountAfterRepair = diagram.Links.Count;

// This would crash without RemoveDuplicateItems call above
diagram.SaveToJson("test.json");
diagram.LoadFromJson("test.json");
 



Note that there are some JSON API changes for upcoming release, to make it more consistent with XML API:

SaveToJson -> SaveToString(SaveToStringFormat.Json);
LoadFromJson -> LoadFromString
SaveToJsonFile -> SaveToJson(filePath)
LoadFromJsonFile -> LoadFromJson(filePath)

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


I love YaBB 1G - SP1!

Posts: 167
Location: England
Joined: Oct 23rd, 2006
Re: Reading from JSON file - duplicate key
Reply #7 - Aug 12th, 2026 at 11:32am
Print Post  
Sorry, I have not looked at this thread for a while.

I do have ValidityChecks turned off to increase execution speed, so that is how the issue probably occurred. Should I turn it on?

I will try out your new duplicate cleanup function and have noted the new function names.

One more issue: I use a HatchBrush pen for some of the ShapeNodes in my diagram and these do not seem to be saved and restored correctly in JSON files...

MindFusion.Drawing.Pen OmittedTaskPen   = new(new HatchBrush(System.Drawing.Drawing2D.HatchStyle.DarkDownwardDiagonal,
                                                                                               Color.Black,  Color.White), 1.5f);

See attachments for how it should look and how it does look.

DavidL
  

Should_look_like_this.png ( 2 KB | 15 Downloads )
Should_look_like_this.png
On_loading_JSON_file.png ( 2 KB | 15 Downloads )
On_loading_JSON_file.png
Back to top
 
IP Logged
 
Slavcho
YaBB Moderator
*****
Offline


tech.support

Posts: 3510
Joined: Oct 19th, 2005
Re: Reading from JSON file - duplicate key
Reply #8 - Aug 12th, 2026 at 12:08pm
Print Post  
I think better to disable ValidityChecks only before code that mass-populates the diagram, e.g. before importing a large number of DB records as nodes, and re-enable it immediately afterwards to avoid incidental duplication in subsequent code.

We'll try to fix hatched pens serialization for the official 7.2.1 release.

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


I love YaBB 1G - SP1!

Posts: 167
Location: England
Joined: Oct 23rd, 2006
Re: Reading from JSON file - duplicate key
Reply #9 - Aug 12th, 2026 at 4:00pm
Print Post  
OK, thank you.
DavidL
  
Back to top
 
IP Logged
 
Page Index Toggle Pages: 1
Send TopicPrint