-Fixed DataTable and DataColumn names not being modified by CamelCasePropertyNamesContractResolver

This commit is contained in:
JamesNK
2011-08-03 00:50:44 +00:00
parent 18c9400054
commit 44ab7f71df
10 changed files with 106 additions and 10 deletions
@@ -801,7 +801,54 @@ namespace Newtonsoft.Json.Tests.Bson
reader = new BsonReader(new MemoryStream(bson), false, DateTimeKind.Unspecified);
o = (JObject)JToken.ReadFrom(reader);
Assert.AreEqual(DateTime.SpecifyKind(value.ToLocalTime(), DateTimeKind.Unspecified), (DateTime)o["DateTime"]);
Assert.AreEqual(DateTime.SpecifyKind(value, DateTimeKind.Unspecified), (DateTime)o["DateTime"]);
}
[Test]
public void UnspecifiedDateTimeKindHandling()
{
DateTime value = new DateTime(2000, 1, 1, 0, 0, 0, DateTimeKind.Unspecified);
MemoryStream ms = new MemoryStream();
BsonWriter writer = new BsonWriter(ms);
writer.DateTimeKindHandling = DateTimeKind.Unspecified;
writer.WriteStartObject();
writer.WritePropertyName("DateTime");
writer.WriteValue(value);
writer.WriteEndObject();
byte[] bson = ms.ToArray();
JObject o;
BsonReader reader;
reader = new BsonReader(new MemoryStream(bson), false, DateTimeKind.Unspecified);
o = (JObject)JToken.ReadFrom(reader);
Assert.AreEqual(value, (DateTime)o["DateTime"]);
}
[Test]
public void LocalDateTimeKindHandling()
{
DateTime value = new DateTime(2000, 1, 1, 0, 0, 0, DateTimeKind.Local);
MemoryStream ms = new MemoryStream();
BsonWriter writer = new BsonWriter(ms);
writer.WriteStartObject();
writer.WritePropertyName("DateTime");
writer.WriteValue(value);
writer.WriteEndObject();
byte[] bson = ms.ToArray();
JObject o;
BsonReader reader;
reader = new BsonReader(new MemoryStream(bson), false, DateTimeKind.Local);
o = (JObject)JToken.ReadFrom(reader);
Assert.AreEqual(value, (DateTime)o["DateTime"]);
}
private string WriteAndReadStringValue(string val)
@@ -5,6 +5,7 @@ using System.Linq;
using System.Text;
using Newtonsoft.Json.Converters;
using NUnit.Framework;
using Newtonsoft.Json.Serialization;
using Newtonsoft.Json.Tests.TestObjects;
using System.Data;
@@ -251,7 +252,51 @@ namespace Newtonsoft.Json.Tests.Converters
}
[Test]
public void Blah()
public void SerializeWithCamelCaseResolver()
{
DataSet ds = new DataSet();
ds.Tables.Add(CreateDataTable("FirstTable", 2));
ds.Tables.Add(CreateDataTable("SecondTable", 1));
string json = JsonConvert.SerializeObject(ds, Formatting.Indented, new JsonSerializerSettings
{
ContractResolver = new CamelCasePropertyNamesContractResolver()
});
Assert.AreEqual(@"{
""firstTable"": [
{
""stringCol"": ""Item Name"",
""int32Col"": 1,
""booleanCol"": true,
""timeSpanCol"": ""10.22:10:15.1000000"",
""dateTimeCol"": ""\/Date(978048000000)\/"",
""decimalCol"": 64.0021
},
{
""stringCol"": ""Item Name"",
""int32Col"": 2,
""booleanCol"": true,
""timeSpanCol"": ""10.22:10:15.1000000"",
""dateTimeCol"": ""\/Date(978048000000)\/"",
""decimalCol"": 64.0021
}
],
""secondTable"": [
{
""stringCol"": ""Item Name"",
""int32Col"": 1,
""booleanCol"": true,
""timeSpanCol"": ""10.22:10:15.1000000"",
""dateTimeCol"": ""\/Date(978048000000)\/"",
""decimalCol"": 64.0021
}
]
}", json);
}
[Test]
public void SerializeDataSetProperty()
{
DataSet ds = new DataSet();
ds.Tables.Add(CreateDataTable("FirstTable", 2));
@@ -41,5 +41,5 @@ using System.Runtime.InteropServices;
// by using the '*' as shown below:
[assembly: AssemblyVersion("4.0.2.0")]
#if !PocketPC
[assembly: AssemblyFileVersion("4.0.2.14002")]
[assembly: AssemblyFileVersion("4.0.2.14003")]
#endif
+1 -1
View File
@@ -501,7 +501,7 @@ namespace Newtonsoft.Json.Bson
switch (DateTimeKindHandling)
{
case DateTimeKind.Unspecified:
dateTime = DateTime.SpecifyKind(utcDateTime.ToLocalTime(), DateTimeKind.Unspecified);
dateTime = DateTime.SpecifyKind(utcDateTime, DateTimeKind.Unspecified);
break;
case DateTimeKind.Local:
dateTime = utcDateTime.ToLocalTime();
@@ -26,6 +26,7 @@
#if !SILVERLIGHT
using System;
using System.Data;
using Newtonsoft.Json.Serialization;
namespace Newtonsoft.Json.Converters
{
@@ -43,6 +44,7 @@ namespace Newtonsoft.Json.Converters
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
DataSet dataSet = (DataSet)value;
DefaultContractResolver resolver = serializer.ContractResolver as DefaultContractResolver;
DataTableConverter converter = new DataTableConverter();
@@ -50,7 +52,7 @@ namespace Newtonsoft.Json.Converters
foreach (DataTable table in dataSet.Tables)
{
writer.WritePropertyName(table.TableName);
writer.WritePropertyName((resolver != null) ? resolver.ResolvePropertyName(table.TableName) : table.TableName);
converter.WriteJson(writer, table, serializer);
}
@@ -26,6 +26,7 @@
#if !SILVERLIGHT
using System;
using System.Data;
using Newtonsoft.Json.Serialization;
namespace Newtonsoft.Json.Converters
{
@@ -43,6 +44,7 @@ namespace Newtonsoft.Json.Converters
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
DataTable table = (DataTable)value;
DefaultContractResolver resolver = serializer.ContractResolver as DefaultContractResolver;
writer.WriteStartArray();
@@ -51,7 +53,7 @@ namespace Newtonsoft.Json.Converters
writer.WriteStartObject();
foreach (DataColumn column in row.Table.Columns)
{
writer.WritePropertyName(column.ColumnName);
writer.WritePropertyName((resolver != null) ? resolver.ResolvePropertyName(column.ColumnName) : column.ColumnName);
serializer.Serialize(writer, row[column]);
}
writer.WriteEndObject();
@@ -110,7 +110,7 @@ using System.Security;
// by using the '*' as shown below:
[assembly: AssemblyVersion("4.0.2.0")]
#if !PocketPC
[assembly: AssemblyFileVersion("4.0.2.14002")]
[assembly: AssemblyFileVersion("4.0.2.14003")]
#endif
[assembly: CLSCompliant(true)]
@@ -46,7 +46,7 @@ namespace Newtonsoft.Json.Serialization
/// </summary>
/// <param name="propertyName">Name of the property.</param>
/// <returns>The property name camel cased.</returns>
protected override string ResolvePropertyName(string propertyName)
protected internal override string ResolvePropertyName(string propertyName)
{
// lower case the first letter of the passed in name
return StringUtils.ToCamelCase(propertyName);
@@ -920,7 +920,7 @@ namespace Newtonsoft.Json.Serialization
/// </summary>
/// <param name="propertyName">Name of the property.</param>
/// <returns>Name of the property.</returns>
protected virtual string ResolvePropertyName(string propertyName)
protected internal virtual string ResolvePropertyName(string propertyName)
{
return propertyName;
}
@@ -792,7 +792,7 @@ namespace Newtonsoft.Json.Serialization
} while (!exit && reader.Read());
if (contract.ISerializableCreator == null)
throw new JsonSerializationException("ISerializable type '{0}' does not have a valid constructor.".FormatWith(CultureInfo.InvariantCulture, objectType));
throw new JsonSerializationException("ISerializable type '{0}' does not have a valid constructor. To correctly implement ISerializable a constructor that takes SerializationInfo and StreamingContext parameters should be present.".FormatWith(CultureInfo.InvariantCulture, objectType));
object createdObject = contract.ISerializableCreator(serializationInfo, Serializer.Context);