Skip to content

Commit

Permalink
IGNITE-5927 .NET: Fix DataTable serialization
Browse files Browse the repository at this point in the history
This closes apache#2395
  • Loading branch information
ptupitsyn committed Aug 4, 2017
1 parent 1a7354f commit ec115dc
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -146,6 +147,36 @@ private static Type GenerateDynamicType()

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]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -111,13 +110,15 @@ public EmptyObject()
/// </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);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public bool SupportsHandles
/** <inheritdoc /> */
public void WriteBinary<T>(T obj, BinaryWriter writer)
{
var ctx = GetStreamingContext(writer);
var ctx = GetStreamingContext();
_serializableTypeDesc.OnSerializing(obj, ctx);

var serializable = (ISerializable) obj;
Expand Down Expand Up @@ -90,7 +90,7 @@ public void WriteBinary<T>(T obj, BinaryWriter writer)
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;

Expand Down Expand Up @@ -583,17 +583,11 @@ private static object ReadAsCustomType(Type customType, SerializationInfo serInf
/// <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>
Expand Down

0 comments on commit ec115dc

Please sign in to comment.