Page Index Toggle Pages: 1 Send TopicPrint
Normal Topic JSON Serialization (Read 3057 times)
Bubba Ho-tep
YaBB Newbies
*
Offline


I Love MindFusion!

Posts: 2
Joined: Jun 25th, 2012
JSON Serialization
Jun 25th, 2012 at 9:10pm
Print Post  
Hello, I'm testing out the JS library, and after running through the Tutorials, I realized that I couldn't see how to dump the diagrams back into JSON. 

Is there a method that basically returns a JSON object with all the nodes and links (which could then be used to reconstruct the diagram at a later time)?

Thanks!
  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: JSON Serialization
Reply #1 - Jun 26th, 2012 at 8:16am
Print Post  
Hi,

The Diagram class has built-in string toJson() and fromJson(string) methods, used for the implementation of Canvas mode in our ASP.NET control. Their output might be a bit heavy-weight though; if you prefer JSON objects in a lighter format similar to the one used in tutorial 1, it is easy to generate them like this:

Code
Select All
function toJsonString(diagram)
{
    var graph = { nodes:[], links: [] };
    var nodeCounter = 0;

    diagram.nodes.forEach(function (node)
    {
        node.jsonId = nodeCounter;
        graph.nodes.push({
            id: nodeCounter++,
            name: node.getText()
        });
    });

    diagram.links.forEach(function (link)
    {
        graph.links.push({
            origin: link.getOrigin().jsonId,
            target: link.getDestination().jsonId
        });
    });

    return Sys.Serialization.JavaScriptSerializer.serialize(graph);
} 



I hope that helps,
Stoyan
  
Back to top
 
IP Logged
 
Bubba Ho-tep
YaBB Newbies
*
Offline


I Love MindFusion!

Posts: 2
Joined: Jun 25th, 2012
Re: JSON Serialization
Reply #2 - Jun 27th, 2012 at 4:39pm
Print Post  
Awesome, that's exactly what I needed.

Thanks!
  
Back to top
 
IP Logged
 
Page Index Toggle Pages: 1
Send TopicPrint