-Fixed deserializing Regex when using StringEnumConverter

-Fixed serializing and deserializing typed DataSets
This commit is contained in:
James Newton-King
2013-05-04 15:28:01 +12:00
parent b6816866ba
commit 6f2b931a90
22 changed files with 393 additions and 39 deletions
@@ -30,6 +30,7 @@ using System.Globalization;
using System.Numerics;
#endif
using System.Text;
using System.Text.RegularExpressions;
#if !NETFX_CORE
using NUnit.Framework;
#else
@@ -1469,5 +1470,44 @@ namespace Newtonsoft.Json.Tests.Bson
Assert.AreEqual(i, c.Blah);
}
#endif
public class RegexTestClass
{
public Regex Regex { get; set; }
}
[Test]
public void DeserializeRegexNonConverterBson()
{
string hex = "46-00-00-00-03-52-65-67-65-78-00-3A-00-00-00-02-70-61-74-74-65-72-6E-00-05-00-00-00-28-68-69-29-00-10-6F-70-74-69-6F-6E-73-00-05-00-00-00-12-6D-61-74-63-68-54-69-6D-65-6F-75-74-00-F0-D8-FF-FF-FF-FF-FF-FF-00-00";
byte[] data = HexToBytes(hex);
MemoryStream ms = new MemoryStream(data);
BsonReader reader = new BsonReader(ms);
JsonSerializer serializer = new JsonSerializer();
RegexTestClass c = serializer.Deserialize<RegexTestClass>(reader);
Assert.AreEqual("(hi)", c.Regex.ToString());
Assert.AreEqual(RegexOptions.ExplicitCapture | RegexOptions.IgnoreCase, c.Regex.Options);
}
[Test]
public void DeserializeRegexBson()
{
string hex = "15-00-00-00-0B-52-65-67-65-78-00-28-68-69-29-00-69-75-78-00-00";
byte[] data = HexToBytes(hex);
MemoryStream ms = new MemoryStream(data);
BsonReader reader = new BsonReader(ms);
JsonSerializer serializer = new JsonSerializer();
RegexTestClass c = serializer.Deserialize<RegexTestClass>(reader);
Assert.AreEqual("(hi)", c.Regex.ToString());
Assert.AreEqual(RegexOptions.ExplicitCapture | RegexOptions.IgnoreCase, c.Regex.Options);
}
}
}
@@ -29,6 +29,7 @@ using System.Collections.Generic;
using System.Numerics;
#endif
using System.Text;
using System.Text.RegularExpressions;
#if !NETFX_CORE
using NUnit.Framework;
#else
@@ -38,6 +39,7 @@ using Test = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestMethodAtt
#endif
using Newtonsoft.Json.Bson;
using System.IO;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Utilities;
using Newtonsoft.Json.Tests.TestObjects;
using System.Globalization;
@@ -715,6 +717,35 @@ namespace Newtonsoft.Json.Tests.Bson
Assert.AreEqual("10-00-00-00-09-30-00-C8-88-07-6B-DC-00-00-00-00", (BitConverter.ToString(ms.ToArray())));
}
public class RegexTestClass
{
public Regex Regex { get; set; }
}
[Test]
public void SerializeDeserializeRegex()
{
Regex r1 = new Regex("(hi)", RegexOptions.ExplicitCapture | RegexOptions.IgnoreCase);
RegexTestClass c = new RegexTestClass { Regex = r1 };
MemoryStream ms = new MemoryStream();
JsonSerializer serializer = new JsonSerializer();
BsonWriter writer = new BsonWriter(ms);
serializer.Serialize(writer, c);
string hex = BitConverter.ToString(ms.ToArray());
Assert.AreEqual("15-00-00-00-0B-52-65-67-65-78-00-28-68-69-29-00-69-75-78-00-00", hex);
JObject o = (JObject)JObject.ReadFrom(new BsonReader(new MemoryStream(ms.ToArray())));
Console.WriteLine(o.ToString());
//{
// "Regex": "/(hi)/iux"
//}
}
#if !(NET20 || NET35 || SILVERLIGHT || PORTABLE || PORTABLE40)
[Test]
public void WriteBigInteger()