-Fixed DateTimeZoneHandling not being used when writing DateTime dictionary keys
-Buffer pooling fixes -Improved date parsing performance
This commit is contained in:
@@ -24,6 +24,7 @@
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using Newtonsoft.Json.Linq;
|
||||
@@ -1175,8 +1176,8 @@ third line", jsonTextReader.Value);
|
||||
|
||||
public class FakeBufferPool : IJsonBufferPool<char>
|
||||
{
|
||||
public readonly IList<char[]> FreeBuffers = new List<char[]>();
|
||||
public readonly IList<char[]> UsedBuffers = new List<char[]>();
|
||||
public readonly List<char[]> FreeBuffers = new List<char[]>();
|
||||
public readonly List<char[]> UsedBuffers = new List<char[]>();
|
||||
|
||||
public char[] RentBuffer(int minSize)
|
||||
{
|
||||
@@ -1197,10 +1198,12 @@ third line", jsonTextReader.Value);
|
||||
|
||||
public void ReturnBuffer(ref char[] buffer)
|
||||
{
|
||||
UsedBuffers.Remove(buffer);
|
||||
if (!FreeBuffers.Contains(buffer))
|
||||
if (UsedBuffers.Remove(buffer))
|
||||
{
|
||||
FreeBuffers.Add(buffer);
|
||||
|
||||
// smallest first so the first buffer large enough is rented
|
||||
FreeBuffers.Sort((b1, b2) => Comparer<int>.Default.Compare(b1.Length, b2.Length));
|
||||
}
|
||||
|
||||
buffer = null;
|
||||
@@ -1215,7 +1218,8 @@ third line", jsonTextReader.Value);
|
||||
""Description"": ""Amazing!\nBuy now!"",
|
||||
""Drives"": [
|
||||
""DVD read/writer"",
|
||||
""500 gigabyte hard drive""
|
||||
""500 gigabyte hard drive"",
|
||||
""Amazing Drive" + new string('!', 9000) + @"""
|
||||
]
|
||||
}";
|
||||
|
||||
@@ -1238,7 +1242,8 @@ third line", jsonTextReader.Value);
|
||||
}
|
||||
}
|
||||
|
||||
Assert.AreEqual(2, bufferPool.FreeBuffers.Count);
|
||||
Assert.AreEqual(0, bufferPool.UsedBuffers.Count);
|
||||
Assert.AreEqual(6, bufferPool.FreeBuffers.Count);
|
||||
}
|
||||
|
||||
[Test]
|
||||
|
||||
@@ -59,6 +59,50 @@ namespace Newtonsoft.Json.Tests
|
||||
[TestFixture]
|
||||
public class JsonTextWriterTest : TestFixtureBase
|
||||
{
|
||||
[Test]
|
||||
public void BufferTest()
|
||||
{
|
||||
JsonTextReaderTest.FakeBufferPool bufferPool = new JsonTextReaderTest.FakeBufferPool();
|
||||
|
||||
string longString = new string('A', 2000);
|
||||
string longEscapedString = "Hello!" + new string('!', 50) + new string('\n', 1000) + "Good bye!";
|
||||
string longerEscapedString = "Hello!" + new string('!', 2000) + new string('\n', 1000) + "Good bye!";
|
||||
|
||||
for (int i = 0; i < 1000; i++)
|
||||
{
|
||||
StringWriter sw = new StringWriter(CultureInfo.InvariantCulture);
|
||||
|
||||
using (JsonTextWriter writer = new JsonTextWriter(sw))
|
||||
{
|
||||
writer.BufferPool = bufferPool;
|
||||
|
||||
writer.WriteStartObject();
|
||||
|
||||
writer.WritePropertyName("Prop1");
|
||||
writer.WriteValue(new DateTime(2000, 12, 12, 12, 12, 12, DateTimeKind.Utc));
|
||||
|
||||
writer.WritePropertyName("Prop2");
|
||||
writer.WriteValue(longString);
|
||||
|
||||
writer.WritePropertyName("Prop3");
|
||||
writer.WriteValue(longEscapedString);
|
||||
|
||||
writer.WritePropertyName("Prop4");
|
||||
writer.WriteValue(longerEscapedString);
|
||||
|
||||
writer.WriteEndObject();
|
||||
}
|
||||
|
||||
if ((i + 1) % 100 == 0)
|
||||
{
|
||||
Console.WriteLine("Allocated buffers: " + bufferPool.FreeBuffers.Count);
|
||||
}
|
||||
}
|
||||
|
||||
Assert.AreEqual(0, bufferPool.UsedBuffers.Count);
|
||||
Assert.AreEqual(3, bufferPool.FreeBuffers.Count);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void NewLine()
|
||||
{
|
||||
|
||||
@@ -7487,6 +7487,34 @@ Path '', line 1, position 1.");
|
||||
Assert.AreEqual(2, dic2[new DateTime(2013, 12, 12, 12, 12, 12, DateTimeKind.Utc)]);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DateTimeDictionaryKey_DateTime_Iso_Local()
|
||||
{
|
||||
IDictionary<DateTime, int> dic1 = new Dictionary<DateTime, int>
|
||||
{
|
||||
{ new DateTime(2000, 12, 12, 12, 12, 12, DateTimeKind.Utc), 1 },
|
||||
{ new DateTime(2013, 12, 12, 12, 12, 12, DateTimeKind.Utc), 2 }
|
||||
};
|
||||
|
||||
string json = JsonConvert.SerializeObject(dic1, Formatting.Indented, new JsonSerializerSettings
|
||||
{
|
||||
DateTimeZoneHandling = DateTimeZoneHandling.Local
|
||||
});
|
||||
|
||||
JObject o = JObject.Parse(json);
|
||||
Assert.IsFalse(o.Properties().ElementAt(0).Name.Contains("Z"));
|
||||
Assert.IsFalse(o.Properties().ElementAt(1).Name.Contains("Z"));
|
||||
|
||||
IDictionary<DateTime, int> dic2 = JsonConvert.DeserializeObject<IDictionary<DateTime, int>>(json, new JsonSerializerSettings
|
||||
{
|
||||
DateTimeZoneHandling = DateTimeZoneHandling.Utc
|
||||
});
|
||||
|
||||
Assert.AreEqual(2, dic2.Count);
|
||||
Assert.AreEqual(1, dic2[new DateTime(2000, 12, 12, 12, 12, 12, DateTimeKind.Utc)]);
|
||||
Assert.AreEqual(2, dic2[new DateTime(2013, 12, 12, 12, 12, 12, DateTimeKind.Utc)]);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DateTimeDictionaryKey_DateTime_MS()
|
||||
{
|
||||
|
||||
@@ -53,16 +53,20 @@ namespace Newtonsoft.Json.Tests.Utilities
|
||||
RoundtripDateIso(DateTime.MaxValue);
|
||||
}
|
||||
|
||||
private static StringReference CreateStringReference(string s)
|
||||
{
|
||||
return new StringReference(s.ToCharArray(), 0, s.Length);
|
||||
}
|
||||
|
||||
private static void RoundtripDateIso(DateTime value)
|
||||
{
|
||||
StringWriter sw = new StringWriter();
|
||||
DateTimeUtils.WriteDateTimeString(sw, value, DateFormatHandling.IsoDateFormat, null, CultureInfo.InvariantCulture);
|
||||
string minDateText = sw.ToString();
|
||||
|
||||
object dt;
|
||||
DateTimeUtils.TryParseDateIso(minDateText, DateParseHandling.DateTime, DateTimeZoneHandling.RoundtripKind, out dt);
|
||||
DateTime parsedDt;
|
||||
DateTimeUtils.TryParseDateTimeIso(CreateStringReference(minDateText), DateTimeZoneHandling.RoundtripKind, out parsedDt);
|
||||
|
||||
DateTime parsedDt = (DateTime)dt;
|
||||
Assert.AreEqual(value, parsedDt);
|
||||
}
|
||||
|
||||
@@ -118,8 +122,12 @@ namespace Newtonsoft.Json.Tests.Utilities
|
||||
oldDate = oldDt;
|
||||
}
|
||||
|
||||
object newDt;
|
||||
DateTimeUtils.TryParseDateIso(text, DateParseHandling.DateTime, DateTimeZoneHandling.RoundtripKind, out newDt);
|
||||
object newDt = null;
|
||||
DateTime temp;
|
||||
if (DateTimeUtils.TryParseDateTimeIso(CreateStringReference(text), DateTimeZoneHandling.RoundtripKind, out temp))
|
||||
{
|
||||
newDt = temp;
|
||||
}
|
||||
|
||||
if (!Equals(oldDate, newDt))
|
||||
{
|
||||
@@ -175,17 +183,26 @@ namespace Newtonsoft.Json.Tests.Utilities
|
||||
private void AssertNewDateTimeOffsetParseEqual(string text)
|
||||
{
|
||||
object oldDt;
|
||||
object newDt;
|
||||
object newDt = null;
|
||||
|
||||
TryParseDateIso(text, DateParseHandling.DateTimeOffset, DateTimeZoneHandling.Unspecified, out oldDt);
|
||||
DateTimeUtils.TryParseDateIso(text, DateParseHandling.DateTimeOffset, DateTimeZoneHandling.Unspecified, out newDt);
|
||||
|
||||
DateTimeOffset temp;
|
||||
if (DateTimeUtils.TryParseDateTimeOffsetIso(CreateStringReference(text), out temp))
|
||||
{
|
||||
newDt = temp;
|
||||
}
|
||||
|
||||
if (!Equals(oldDt, newDt))
|
||||
{
|
||||
long? oldTicks = oldDt != null ? (long?)((DateTime)oldDt).Ticks : null;
|
||||
long? newTicks = newDt != null ? (long?)((DateTime)newDt).Ticks : null;
|
||||
|
||||
Assert.AreEqual(oldDt, newDt, "DateTimeOffset parse not equal. Text: '{0}' Old ticks: {1} New ticks: {2}".FormatWith(
|
||||
CultureInfo.InvariantCulture,
|
||||
text,
|
||||
((DateTime)oldDt).Ticks,
|
||||
((DateTime)newDt).Ticks));
|
||||
oldTicks,
|
||||
newTicks));
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user