Back to Devexpress

ICustomObjectConverter Interface

corelibraries-devexpress-dot-utils-dot-serializing-dot-helpers-12b504b3.md

latest9.9 KB
Original Source

ICustomObjectConverter Interface

When implemented, specifies a converter used to serialize/deserialize arbitrary objects in a custom manner.

Namespace : DevExpress.Utils.Serializing.Helpers

Assembly : DevExpress.Data.v25.2.dll

NuGet Package : DevExpress.Data

Declaration

csharp
public interface ICustomObjectConverter
vb
Public Interface ICustomObjectConverter

The following members return ICustomObjectConverter objects:

LibraryRelated API Members
Cross-Platform Class LibraryPivotGridOptionsData.CustomObjectConverter
ASP.NET MVC ExtensionsMVCxPivotGridWebOptionsData.CustomObjectConverter

Remarks

The ICustomObjectConverter.ToString and ICustomObjectConverter.FromString methods must be implemented to serialize and deserialize objects, respectively.

For all types the converter can process, the ICustomObjectConverter.CanConvert method must return true ; for other types, false.

The ICustomObjectConverter.GetType method must return Type objects by the types’ full names (FullName).

Example

The following example shows how to implement a custom serializer. Custom serializers are required when data source field values are custom objects (not numeric or string values). In this example, the data source contains a Sales Person field whose values are Employee objects, exposing the FirstName, LastName and Age properties. The Employee class implements the IComparable interface, and overrides the GetHashCode , Equals and ToString methods (required to display and handle custom objects). The custom serializer is represented by the CustomObjectConverter class, which implements the ICustomObjectConverter interface. The ToString and FromString methods are implemented to serialize and deserialize the Employee objects, respectively. A CustomObjectConverter class instance is assigned to the PivotGridOptionsData.CustomObjectConverter property. It is used for serializing Sales Person field values when pivot grid data is saved to a stream and restored back by an end-user via clicking the Save and Load buttons respectively.

csharp
using System;
using System.IO;
using System.Windows.Forms;
using DevExpress.Data.PivotGrid;
using DevExpress.Utils.Serializing.Helpers;

namespace XtraPivotGrid_CustomObjectConverter {
    public partial class Form1 : Form {
        MemoryStream stream;
        public Form1() {
            InitializeComponent();
            pivotGridControl1.DataSource = DataHelper.GetData();
            pivotGridControl1.OptionsData.CustomObjectConverter = new CustomObjectConverter();
        }

        // Handles the Save button's Click event to save pivot grid data to a stream
        // (requires data source serialization).
        private void simpleButton1_Click(object sender, EventArgs e) {
            if (stream != null) stream.Dispose();
            stream = new MemoryStream();
            pivotGridControl1.SavePivotGridToStream(stream);
        }

        // Handles the Load button's Click event to load pivot grid data from a stream
        // (requires stream content deserialization).
        private void simpleButton2_Click(object sender, EventArgs e) {
            if (stream == null) return;
            PivotFileDataSource ds = new PivotFileDataSource(stream, new CustomObjectConverter());
            pivotGridControl1.DataSource = ds;
        }
    }

    // Implements a custom serializer.
    public class CustomObjectConverter : ICustomObjectConverter {

        // Returns a value, indicating whether objects of the specified type
        // can be serialized/deserialized.
        public bool CanConvert(Type type) {
            return type == typeof(Employee);
        }

        // Deserializes objects of the specified type.
        public object FromString(Type type, string str) {
            if (type != typeof(Employee))
                return null;
            string[] array = str.Split('#');
            if (array.Length >= 3)
                return new Employee(array[0], array[1], int.Parse(array[2]));
            else if (array.Length == 2)
                return new Employee(array[0], array[1], 0);
            else if (array.Length == 1)
                return new Employee(array[0], string.Empty, 0);
            else
                return new Employee(string.Empty, string.Empty, 0);
        }

        // Serializes objects of the specified type.
        public string ToString(Type type, object obj) {
            if (type != typeof(Employee))
                return string.Empty;
            Employee value = obj as Employee;
            return value.FirstName + '#' + value.LastName + '#' + value.Age;
        }

        // Returns the type by its full name.
        public Type GetType(string typeName) {
            if (typeName != typeof(Employee).FullName)
                return null;
            return typeof(Employee);
        }
    }
}
vb
Imports Microsoft.VisualBasic
Imports System
Imports System.IO
Imports System.Windows.Forms
Imports DevExpress.Data.PivotGrid
Imports DevExpress.Utils.Serializing.Helpers

Namespace XtraPivotGrid_CustomObjectConverter
    Partial Public Class Form1
        Inherits Form
        Private stream As MemoryStream
        Public Sub New()
            InitializeComponent()
            pivotGridControl1.DataSource = DataHelper.GetData()
            pivotGridControl1.OptionsData.CustomObjectConverter = _
                New CustomObjectConverter()
        End Sub

        ' Handles the Save button's Click event to save pivot grid data to a stream
        ' (requires data source serialization).
        Private Sub simpleButton1_Click(ByVal sender As Object, ByVal e As EventArgs) _
                Handles simpleButton1.Click
            If stream IsNot Nothing Then
                stream.Dispose()
            End If
            stream = New MemoryStream()
            pivotGridControl1.SavePivotGridToStream(stream)
        End Sub

        ' Handles the Load button's Click event to load pivot grid data from a stream
        ' (requires stream content deserialization).
        Private Sub simpleButton2_Click(ByVal sender As Object, ByVal e As EventArgs) _
                Handles simpleButton2.Click
            If stream Is Nothing Then
                Return
            End If
            Dim ds As New PivotFileDataSource(stream, New CustomObjectConverter())
            pivotGridControl1.DataSource = ds
        End Sub
    End Class

    ' Implements a custom serializer.
    Public Class CustomObjectConverter
        Implements ICustomObjectConverter

        ' Returns a value, indicating whether objects of the specified type
        ' can be serialized/deserialized.
        Public Function CanConvert(ByVal type As Type) As Boolean _
                Implements ICustomObjectConverter.CanConvert
            Return type Is GetType(Employee)
        End Function

        ' Deserializes objects of the specified type.
        Public Function FromString(ByVal type As Type, ByVal str As String) As Object _
                Implements ICustomObjectConverter.FromString
            If type IsNot GetType(Employee) Then
                Return Nothing
            End If
            Dim array() As String = str.Split("#"c)
            If array.Length >= 3 Then
                Return New Employee(array(0), array(1), Integer.Parse(array(2)))
            ElseIf array.Length = 2 Then
                Return New Employee(array(0), array(1), 0)
            ElseIf array.Length = 1 Then
                Return New Employee(array(0), String.Empty, 0)
            Else
                Return New Employee(String.Empty, String.Empty, 0)
            End If
        End Function

        ' Serializes objects of the specified type.
        Public Overloads Function ToString(ByVal type As Type, ByVal obj As Object) As String _
                Implements ICustomObjectConverter.ToString
            If type IsNot GetType(Employee) Then
                Return String.Empty
            End If
            Dim value As Employee = TryCast(obj, Employee)
            Return value.FirstName + "#"c + value.LastName + "#"c + value.Age
        End Function

        ' Returns the type by its full name.
        Public Overloads Function [GetType](ByVal typeName As String) As Type _
                Implements ICustomObjectConverter.GetType
            If typeName IsNot GetType(Employee).FullName Then
                Return Nothing
            End If
            Return GetType(Employee)
        End Function
    End Class
End Namespace

See Also

ICustomObjectConverter Members

DevExpress.Utils.Serializing.Helpers Namespace