This is a migrated thread and some comments may be shown as answers.

Deserialization issue with business object

1 Answer 149 Views
Diagram
This is a migrated thread and some comments may be shown as answers.
Jason
Top achievements
Rank 1
Jason asked on 19 Apr 2014, 12:32 PM
Hi,

I have a problem serializing and deserializing a business object, expanding on your examples:
http://www.telerik.com/help/wpf/raddiagram-howto-drag-custom-toolboxitem.html (1)
http://www.telerik.com/help/wpf/raddiagram-extensions-toolbox.html#PopulateWithCustomData (2)

Your examples work just fine (when I changed the geometry handling, because of localization issues, I think).
But when I try to add more properties, and serialize them, I get an error deserializing the objects.

After searching, I have now found this thread:
http://www.telerik.com/forums/adding-my-own-properties-into-settingspane-of-raddiagram-on-selection-of-shape (3)
In this thread, Zarko writes (in September 13th 2012):
...
"As for your second question - unfortunately at the moment the RadDiagram supports only string serialization."

I now have to ask: Is this still the case? I would expect serialization to have been handled way before now.
And if this is the case, how can I get around this? Can you come with a solution so that I can handle what I am trying in my example (see below)?

Thank you very much!



I have the full sample project as png files attached, but here is the issue from my point of view:

namespace DeserializationProblem
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            SerializationService.Default.ItemSerializing += Default_ItemSerializing;
        }

        void Default_ItemSerializing(object sender, SerializationEventArgs<IDiagramItem> e)
        {
            if (e.Entity is RadDiagramShape)
            {
                Geometry g = (e.Entity as RadDiagramShape).Geometry;
                string g_string = GeometryParser.GetString(g);               
                e.SerializationInfo["MyGeometry"] = g_string;
                if ((e.Entity as RadDiagramShape).DataContext is MyShape)
                {
                    e.SerializationInfo["DataContent"] =
                        ((e.Entity as RadDiagramShape).DataContext as MyShape).Header;

                    e.SerializationInfo["DataContent2"] =
                        ((e.Entity as RadDiagramShape).DataContext as MyShape);
                    var test = (MyShape)e.SerializationInfo["DataContent2"];    // <- Works fine
                }
            }
        }

        private void radDiagram1_ShapeDeserialized(object sender, ShapeSerializationRoutedEventArgs e)
        {
            if (e.Shape as RadDiagramShape != null)
            {
                //SerializationInfo e_info = (SerializationInfo)e.SerializationInfo["MyGeometry"];
                string e_info_string = e.SerializationInfo["MyGeometry"].ToString();
                Geometry g = GeometryParser.GetGeometry(e_info_string);               
                (e.Shape as RadDiagramShape).Geometry = g;

                (e.Shape as RadDiagramShape).Content = e.SerializationInfo["DataContent"].ToString(); // <- Works
                var test = (MyShape)e.SerializationInfo["DataContent2"];    // <- Breaks
                // Error: Unable to cast object of type 'System.String' to type 'DeserializationProblem.MyShape'.
                // e.SerializationInfo["DataContent2"] is now a string "DeserializationProblem.MyShape"
            }
        }
    }
}  
 

1 Answer, 1 is accepted

Sort by
0
Martin Ivanov
Telerik team
answered on 23 Apr 2014, 05:07 PM
Hi Jason,

The RadDiagram still supports only string serialization and we have no plans to extend this in the near future because a full object serialization is not an easy task and there are already two such implementations - the built-in XMLSerializer and if it doesn't work you can try our PersistenceFramework. I want to ask you why do you need to serialize the whole dataContext object in your scenario ? Why don't you save just some of its properties and create a new object on deserialize:
Copy Code
var newDC = new MyShape();
newDC.Id = int.Parse(e.SerializationInfo["MyId"].ToString());
newDC.String1 = e.SerializationInfo["MyString1"].ToString();
newDC.String2 = e.SerializationInfo["MyString2"].ToString();
If you really need to save the MyShape object you can use the XmlSerializer:
Copy Code
void Default_ItemSerializing(object sender, SerializationEventArgs<IDiagramItem> e)
{
    ...
    XmlSerializer serializer = new XmlSerializer(typeof(MyShape));
    using (var writer = new StringWriter())
    {
        serializer.Serialize(writer, item);
        e.SerializationInfo["DataContent2"] = writer.ToString();
    }
    ...
}
and
Copy Code
private void radDiagram1_ShapeDeserialized(object sender, ShapeSerializationRoutedEventArgs e)
{
    ...
    var test = e.SerializationInfo["DataContent2"].ToString();
    XmlSerializer serializer = new XmlSerializer(typeof(MyShape));
    using (var reader = new StringReader(test))
    {
        var dataContext = serializer.Deserialize(reader);
    }
    ...
}
Please note that you'll have to mark the Geometry property with XmlIgnore because the serializer can't serialize StreamGeometries:
Copy Code
[XmlIgnore]
public Geometry Geometry { getset; }
I hope I was able to help you and if you have more questions please feel free to ask.

Regards,
Martin
Telerik
 
Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
 
Tags
Diagram
Asked by
Jason
Top achievements
Rank 1
Answers by
Martin Ivanov
Telerik team
Share this question
or