IGNITE-5927 .NET: Fix DataTable serialization This closes #2395
Project: http://git-wip-us.apache.org/repos/asf/ignite/repo Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/ec115dcf Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/ec115dcf Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/ec115dcf Branch: refs/heads/ignite-5872 Commit: ec115dcfa702aa7cc7db5b87c330755b7f37ea0b Parents: 1a7354f Author: Pavel Tupitsyn <ptupit...@apache.org> Authored: Fri Aug 4 12:34:05 2017 +0300 Committer: Pavel Tupitsyn <ptupit...@apache.org> Committed: Fri Aug 4 12:34:05 2017 +0300 ---------------------------------------------------------------------- .../Apache.Ignite.Core.Tests.csproj | 1 + .../Serializable/AdvancedSerializationTest.cs | 31 ++++++++++++++++++++ .../BasicSerializableObjectsTest.cs | 7 +++-- .../Impl/Binary/SerializableSerializer.cs | 18 ++++-------- 4 files changed, 42 insertions(+), 15 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ignite/blob/ec115dcf/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Apache.Ignite.Core.Tests.csproj ---------------------------------------------------------------------- diff --git a/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Apache.Ignite.Core.Tests.csproj b/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Apache.Ignite.Core.Tests.csproj index e4f65bc..3f5f9b3 100644 --- a/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Apache.Ignite.Core.Tests.csproj +++ b/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Apache.Ignite.Core.Tests.csproj @@ -61,6 +61,7 @@ <Reference Include="System" /> <Reference Include="System.configuration" /> <Reference Include="System.Core" /> + <Reference Include="System.Data" /> <Reference Include="System.Runtime.Serialization" /> <Reference Include="System.ServiceProcess" /> <Reference Include="System.Transactions" /> http://git-wip-us.apache.org/repos/asf/ignite/blob/ec115dcf/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Binary/Serializable/AdvancedSerializationTest.cs ---------------------------------------------------------------------- diff --git a/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Binary/Serializable/AdvancedSerializationTest.cs b/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Binary/Serializable/AdvancedSerializationTest.cs index c96d111..dc208d0 100644 --- a/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Binary/Serializable/AdvancedSerializationTest.cs +++ b/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Binary/Serializable/AdvancedSerializationTest.cs @@ -19,6 +19,7 @@ namespace Apache.Ignite.Core.Tests.Binary.Serializable { using System; using System.Collections.Generic; + using System.Data; using System.Linq; using System.Reflection; using System.Reflection.Emit; @@ -146,6 +147,36 @@ namespace Apache.Ignite.Core.Tests.Binary.Serializable return typeBuilder.CreateType(); } + + /// <summary> + /// Tests the DataTable serialization. + /// </summary> + [Test] + public void TestDataTable() + { + var dt = new DataTable("foo"); + + dt.Columns.Add("intCol", typeof(int)); + dt.Columns.Add("stringCol", typeof(string)); + + dt.Rows.Add(1, "1"); + dt.Rows.Add(2, "2"); + + var cache = Ignition.GetIgnite().GetOrCreateCache<int, DataTable>("dataTables"); + cache.Put(1, dt); + + var res = cache.Get(1); + + Assert.AreEqual("foo", res.TableName); + + Assert.AreEqual(2, res.Columns.Count); + Assert.AreEqual("intCol", res.Columns[0].ColumnName); + Assert.AreEqual("stringCol", res.Columns[1].ColumnName); + + Assert.AreEqual(2, res.Rows.Count); + Assert.AreEqual(new object[] {1, "1"}, res.Rows[0].ItemArray); + Assert.AreEqual(new object[] {2, "2"}, res.Rows[1].ItemArray); + } } [Serializable] http://git-wip-us.apache.org/repos/asf/ignite/blob/ec115dcf/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Binary/Serializable/BasicSerializableObjectsTest.cs ---------------------------------------------------------------------- diff --git a/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Binary/Serializable/BasicSerializableObjectsTest.cs b/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Binary/Serializable/BasicSerializableObjectsTest.cs index 82deb3c..e9b5576 100644 --- a/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Binary/Serializable/BasicSerializableObjectsTest.cs +++ b/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Binary/Serializable/BasicSerializableObjectsTest.cs @@ -19,7 +19,6 @@ namespace Apache.Ignite.Core.Tests.Binary.Serializable { using System; using System.Runtime.Serialization; - using Apache.Ignite.Core.Binary; using NUnit.Framework; /// <summary> @@ -111,13 +110,15 @@ namespace Apache.Ignite.Core.Tests.Binary.Serializable /// </summary> private EmptyObject(SerializationInfo info, StreamingContext context) { - Assert.IsInstanceOf<IBinaryReader>(context.Context); + Assert.AreEqual(StreamingContextStates.All, context.State); + Assert.IsNull(context.Context); } /** <inheritdoc /> */ public void GetObjectData(SerializationInfo info, StreamingContext context) { - Assert.IsInstanceOf<IBinaryWriter>(context.Context); + Assert.AreEqual(StreamingContextStates.All, context.State); + Assert.IsNull(context.Context); } } } http://git-wip-us.apache.org/repos/asf/ignite/blob/ec115dcf/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Binary/SerializableSerializer.cs ---------------------------------------------------------------------- diff --git a/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Binary/SerializableSerializer.cs b/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Binary/SerializableSerializer.cs index 13310e4..e660cff 100644 --- a/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Binary/SerializableSerializer.cs +++ b/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Binary/SerializableSerializer.cs @@ -53,7 +53,7 @@ namespace Apache.Ignite.Core.Impl.Binary /** <inheritdoc /> */ public void WriteBinary<T>(T obj, BinaryWriter writer) { - var ctx = GetStreamingContext(writer); + var ctx = GetStreamingContext(); _serializableTypeDesc.OnSerializing(obj, ctx); var serializable = (ISerializable) obj; @@ -90,7 +90,7 @@ namespace Apache.Ignite.Core.Impl.Binary public T ReadBinary<T>(BinaryReader reader, IBinaryTypeDescriptor desc, int pos, Type typeOverride) { object res; - var ctx = GetStreamingContext(reader); + var ctx = GetStreamingContext(); var type = typeOverride ?? desc.Type; @@ -583,17 +583,11 @@ namespace Apache.Ignite.Core.Impl.Binary /// <summary> /// Gets the streaming context. /// </summary> - private static StreamingContext GetStreamingContext(IBinaryReader reader) + private static StreamingContext GetStreamingContext() { - return new StreamingContext(StreamingContextStates.All, reader); - } - - /// <summary> - /// Gets the streaming context. - /// </summary> - private static StreamingContext GetStreamingContext(IBinaryWriter writer) - { - return new StreamingContext(StreamingContextStates.All, writer); + // Additional parameter must be null, because some ISerializable implementations expect weird things there. + // For example, System.Data.DataTable calls Convert.ToBoolean on that value. + return new StreamingContext(StreamingContextStates.All, null); } /// <summary>