Page Index Toggle Pages: 1 Send TopicPrint
Hot Topic (More than 10 Replies) Serialize diagramitemcollection to JSON (Read 5920 times)
alfrye
YaBB Newbies
*
Offline


I love YaBB 1G - SP1!

Posts: 16
Joined: Sep 30th, 2009
Serialize diagramitemcollection to JSON
Jun 30th, 2010 at 4:33pm
Print Post  
We have implemented our own copy selected items routine that save the selected items to a diagramitemcollection. I am trying to serialize the diagramitemcollection to a JSON  to put on the windows clipboard and then deserialize the JSON string from the clipboard back into a diagramitemcollection.


I am able to get the diagramitemcollection to serialize but I am unable to get the json string to deserialize back to the object.

any ideas !!

Alan
  
Back to top
AIM  
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: Serialize diagramitemcollection to JSON
Reply #1 - Jun 30th, 2010 at 4:47pm
Print Post  
Hi,

Is there any reason not to use the CopyToClipboard and PasteFromClipboard methods?

Stoyan
  
Back to top
 
IP Logged
 
alfrye
YaBB Newbies
*
Offline


I love YaBB 1G - SP1!

Posts: 16
Joined: Sep 30th, 2009
Re: Serialize diagramitemcollection to JSON
Reply #2 - Jun 30th, 2010 at 6:51pm
Print Post  
We do custom validation of the items that are selected from the diagram to ensure everything that is needed is copied.
  
Back to top
AIM  
IP Logged
 
alfrye
YaBB Newbies
*
Offline


I love YaBB 1G - SP1!

Posts: 16
Joined: Sep 30th, 2009
Re: Serialize diagramitemcollection to JSON
Reply #3 - Jun 30th, 2010 at 6:58pm
Print Post  
Also when using the diagram.CopytoClipbord I am getting stackoverflow error.
  
Back to top
AIM  
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: Serialize diagramitemcollection to JSON
Reply #4 - Jun 30th, 2010 at 7:38pm
Print Post  
What method is shown at the top of the stack?
  
Back to top
 
IP Logged
 
alfrye
YaBB Newbies
*
Offline


I love YaBB 1G - SP1!

Posts: 16
Joined: Sep 30th, 2009
Re: Serialize diagramitemcollection to JSON
Reply #5 - Jun 30th, 2010 at 8:04pm
Print Post  
>System.dll!System.ComponentModel.ReflectPropertyDescriptor.ShouldSerializeValue
(object component = {System.Windows.Controls.Image}) + 0x99 bytes
  
Back to top
AIM  
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: Serialize diagramitemcollection to JSON
Reply #6 - Jun 30th, 2010 at 9:53pm
Print Post  
Is this image hosted inside a DiagramNodeAdapter, or it's a part of a user control? I suppose you might get such exception if there are circular references between hosted user controls; in such case try handling the SerializeControl event to implement your own serialization.

As for JSON serialization - are you using the .NET's JavaScriptSerializer, or custom serialization code?
  
Back to top
 
IP Logged
 
alfrye
YaBB Newbies
*
Offline


I love YaBB 1G - SP1!

Posts: 16
Joined: Sep 30th, 2009
Re: Serialize diagramitemcollection to JSON
Reply #7 - Jun 30th, 2010 at 9:57pm
Print Post  
I am using JSON.NET for my JSON serializer
  
Back to top
AIM  
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: Serialize diagramitemcollection to JSON
Reply #8 - Jul 1st, 2010 at 2:57pm
Print Post  
Is this the library you are using?
http://json.codeplex.com/releases/view/43775

Could you copy here your serialization and deserialization code?
  
Back to top
 
IP Logged
 
alfrye
YaBB Newbies
*
Offline


I love YaBB 1G - SP1!

Posts: 16
Joined: Sep 30th, 2009
Re: Serialize diagramitemcollection to JSON
Reply #9 - Jul 1st, 2010 at 3:19pm
Print Post  
Yes that is the library I am using.

Here is serialization and deserialization code.

Code
Select All
public void copyItemsToSerialize(DiagramItemCollection itemsCopied)
{
    JsonSerializerSettings jss = new      JsonSerializerSettings{ ReferenceLoopHandling=ReferenceLoopHandling.Ignore};
            string jsonString =   JsonConvert.SerializeObject(itemsCopied,Newtonsoft.Json.Formatting.Indented, jss);
}
 



Code
Select All
      public DiagramItemCollection getItems()
             {
                 DiagramItemCollection items;
                 JsonSerializerSettings jss = new JsonSerializerSettings {  ReferenceLoopHandling = ReferenceLoopHandling.Ignore };
                 string clpData = Clipboard.GetData(DataFormats.StringFormat) as String;
                 items = JsonConvert.DeserializeObject<DiagramItemCollection>(clpData, jss);
              return items;

             }

 



  
Back to top
AIM  
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: Serialize diagramitemcollection to JSON
Reply #10 - Jul 2nd, 2010 at 8:42am
Print Post  
What versions of Json.NET and WpfDiagram are you using? I can't get even SerializeObject to work, it throws a "property Parent already exists" exception. I've added an Error handler to the settings to ignore the error, but then SerializeObject returns an empty Json string "[]" ...
  
Back to top
 
IP Logged
 
alfrye
YaBB Newbies
*
Offline


I love YaBB 1G - SP1!

Posts: 16
Joined: Sep 30th, 2009
Re: Serialize diagramitemcollection to JSON
Reply #11 - Jul 2nd, 2010 at 12:00pm
Print Post  
I am using Mindfusion.Diagramming.wpf.dll 2.2.0.2560p and json.net 3.5.0.

When you serialize are using using JSON.convert or the jsonserializer class.  Also what object are you trying to serialize.


  
Back to top
AIM  
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: Serialize diagramitemcollection to JSON
Reply #12 - Jul 2nd, 2010 at 1:22pm
Print Post  
I'm using v2.3.3 of the diagram control, and SerializeObject in the code below does not work. I will try this with v2.2 next week.

Code
Select All
public void copyItemsToSerialize(DiagramItemCollection itemsCopied)
{
	var jss = new JsonSerializerSettings
	{
		Error = OnJsonError,
		ReferenceLoopHandling = ReferenceLoopHandling.Ignore
	};
	string jsonString = JsonConvert.SerializeObject(itemsCopied, Newtonsoft.Json.Formatting.Indented, jss);
	var items = JsonConvert.DeserializeObject<DiagramItemCollection>(jsonString, jss);
	Debug.WriteLine(items.Count);
}

private void OnJsonError(object sender, ErrorEventArgs e)
{
	e.ErrorContext.Handled = true;
}

private void OnClipboardJson(object sender, RoutedEventArgs e)
{
	copyItemsToSerialize(diagram.Selection.Items);
} 

  
Back to top
 
IP Logged
 
alfrye
YaBB Newbies
*
Offline


I love YaBB 1G - SP1!

Posts: 16
Joined: Sep 30th, 2009
Re: Serialize diagramitemcollection to JSON
Reply #13 - Jul 2nd, 2010 at 1:45pm
Print Post  
In your example is jsonString empty.
  
Back to top
AIM  
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: Serialize diagramitemcollection to JSON
Reply #14 - Jul 2nd, 2010 at 2:33pm
Print Post  
It returns "[]", and there are several items in the collection - the OnError handler is called for each of them.
  
Back to top
 
IP Logged
 
Page Index Toggle Pages: 1
Send TopicPrint