-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
@@ -403,6 +403,56 @@ namespace Newtonsoft.Json.Tests.Converters
Assert.AreEqual(c.Table.Rows.Count, c2.Table.Rows.Count);
Assert.AreEqual(c.After, c2.After);
}
[Test]
public void SerializedTypedDataSet()
{
CustomerDataSet ds = new CustomerDataSet();
ds.Customers.AddCustomersRow("234");
string json = JsonConvert.SerializeObject(ds, Formatting.Indented);
Assert.AreEqual(@"{
""Customers"": [
{
""CustomerID"": ""234""
}
]
}", json);
CustomerDataSet ds1 = new CustomerDataSet();
DataTable table = ds1.Tables["Customers"];
DataRow row = ds1.Tables["Customers"].NewRow();
row["CustomerID"] = "234";
table.Rows.Add(row);
string json1 = JsonConvert.SerializeObject(ds1, Formatting.Indented);
Assert.AreEqual(@"{
""Customers"": [
{
""CustomerID"": ""234""
}
]
}", json1);
}
[Test]
public void DeserializedTypedDataSet()
{
string json = @"{
""Customers"": [
{
""CustomerID"": ""234""
}
]
}";
var ds = JsonConvert.DeserializeObject<CustomerDataSet>(json);
Assert.AreEqual("234", ds.Customers[0].CustomerID);
}
}
}
#endif
@@ -25,8 +25,8 @@
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE || PORTABLE40)
using System;
#if !NETFX_CORE
using System.Collections.Generic;
#if !NETFX_CORE
using NUnit.Framework;
#else
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
@@ -36,6 +36,7 @@ using Test = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestMethodAtt
#if !NETFX_CORE
using System.Data;
#endif
using Newtonsoft.Json.Tests.TestObjects;
namespace Newtonsoft.Json.Tests.Converters
{
@@ -270,6 +271,36 @@ namespace Newtonsoft.Json.Tests.Converters
Assert.AreEqual(1, pair2.Key.Rows.Count);
Assert.AreEqual("item!", pair2.Key.Rows[0]["item"]);
}
[Test]
public void SerializedTypedDataTable()
{
CustomerDataSet.CustomersDataTable dt = new CustomerDataSet.CustomersDataTable();
dt.AddCustomersRow("432");
string json = JsonConvert.SerializeObject(dt, Formatting.Indented);
Assert.AreEqual(@"[
{
""CustomerID"": ""432""
}
]", json);
}
[Test]
public void DeserializedTypedDataTable()
{
string json = @"[
{
""CustomerID"": ""432""
}
]";
var dt = JsonConvert.DeserializeObject<CustomerDataSet.CustomersDataTable>(json);
Assert.AreEqual("432", dt[0].CustomerID);
}
}
}
#endif
@@ -35,6 +35,7 @@ using System.IO;
using System.Text.RegularExpressions;
using Newtonsoft.Json.Bson;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
using Newtonsoft.Json.Utilities;
#if !NETFX_CORE
using NUnit.Framework;
@@ -68,6 +69,57 @@ namespace Newtonsoft.Json.Tests.Converters
}", json);
}
[Test]
public void SerializeCamelCaseAndStringEnums()
{
Regex regex = new Regex("abc", RegexOptions.IgnoreCase);
string json = JsonConvert.SerializeObject(regex, Formatting.Indented, new JsonSerializerSettings
{
Converters = {new RegexConverter(),new StringEnumConverter() { CamelCaseText = true }},
ContractResolver = new CamelCasePropertyNamesContractResolver()
});
Assert.AreEqual(@"{
""pattern"": ""abc"",
""options"": ""ignoreCase""
}", json);
}
[Test]
public void DeserializeCamelCaseAndStringEnums()
{
string json = @"{
""pattern"": ""abc"",
""options"": ""ignoreCase""
}";
Regex regex = JsonConvert.DeserializeObject<Regex>(json, new JsonSerializerSettings
{
Converters = { new RegexConverter() }
});
Assert.AreEqual("abc", regex.ToString());
Assert.AreEqual(RegexOptions.IgnoreCase, regex.Options);
}
[Test]
public void DeserializeISerializeRegexJson()
{
string json = @"{
""Regex"": {
""pattern"": ""(hi)"",
""options"": 5,
""matchTimeout"": -10000
}
}";
RegexTestClass r = JsonConvert.DeserializeObject<RegexTestClass>(json);
Assert.AreEqual("(hi)", r.Regex.ToString());
Assert.AreEqual(RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture, r.Regex.Options);
}
[Test]
public void SerializeToBson()
{
@@ -26,6 +26,7 @@
using System;
using System.IO;
using System.Runtime.Serialization;
using System.Text.RegularExpressions;
using Newtonsoft.Json.Converters;
#if !NETFX_CORE
using NUnit.Framework;
@@ -336,6 +337,7 @@ namespace Newtonsoft.Json.Tests.Converters
Assert.IsNull(c.NullableStoreColor2);
}
[Test]
public void DeserializeInvalidString()
{
string json = "{ \"Value\" : \"Three\" }";