-Added ITraceWriter

-Fixed .NET 2.0 build using DateTimeOffset
This commit is contained in:
James Newton-King
2012-11-09 17:10:32 +13:00
parent 12b60f2fca
commit 89883dc57b
36 changed files with 870 additions and 117 deletions
+1 -1
View File
@@ -20,7 +20,7 @@
@{Name = "Newtonsoft.Json.Portable"; TestsName = "Newtonsoft.Json.Tests.Portable"; Constants="PORTABLE"; FinalDir="Portable"; NuGetDir = "portable-net40+sl4+wp7+win8"; Framework="net-4.0"; Sign=$true}, @{Name = "Newtonsoft.Json.Portable"; TestsName = "Newtonsoft.Json.Tests.Portable"; Constants="PORTABLE"; FinalDir="Portable"; NuGetDir = "portable-net40+sl4+wp7+win8"; Framework="net-4.0"; Sign=$true},
@{Name = "Newtonsoft.Json.WinRT"; TestsName = $null; Constants="NETFX_CORE"; FinalDir="WinRT"; NuGetDir = "winrt45"; Framework="net-4.5"; Sign=$true}, @{Name = "Newtonsoft.Json.WinRT"; TestsName = $null; Constants="NETFX_CORE"; FinalDir="WinRT"; NuGetDir = "winrt45"; Framework="net-4.5"; Sign=$true},
@{Name = "Newtonsoft.Json.WindowsPhone"; TestsName = $null; Constants="SILVERLIGHT;WINDOWS_PHONE"; FinalDir="WindowsPhone"; NuGetDir = "sl3-wp,sl4-windowsphone71"; Framework="net-4.0"; Sign=$true}, #@{Name = "Newtonsoft.Json.WindowsPhone"; TestsName = $null; Constants="SILVERLIGHT;WINDOWS_PHONE"; FinalDir="WindowsPhone"; NuGetDir = "sl3-wp,sl4-windowsphone71"; Framework="net-4.0"; Sign=$true},
@{Name = "Newtonsoft.Json.Silverlight"; TestsName = "Newtonsoft.Json.Tests.Silverlight"; Constants="SILVERLIGHT"; FinalDir="Silverlight"; NuGetDir = "sl4"; Framework="net-4.0"; Sign=$true}, @{Name = "Newtonsoft.Json.Silverlight"; TestsName = "Newtonsoft.Json.Tests.Silverlight"; Constants="SILVERLIGHT"; FinalDir="Silverlight"; NuGetDir = "sl4"; Framework="net-4.0"; Sign=$true},
@{Name = "Newtonsoft.Json.Net35"; TestsName = "Newtonsoft.Json.Tests.Net35"; Constants="NET35"; FinalDir="Net35"; NuGetDir = "net35"; Framework="net-2.0"; Sign=$true}, @{Name = "Newtonsoft.Json.Net35"; TestsName = "Newtonsoft.Json.Tests.Net35"; Constants="NET35"; FinalDir="Net35"; NuGetDir = "net35"; Framework="net-2.0"; Sign=$true},
@@ -99,6 +99,7 @@
<Compile Include="Serialization\JsonPropertyCollectionTests.cs" /> <Compile Include="Serialization\JsonPropertyCollectionTests.cs" />
<Compile Include="Serialization\NullValueHandlingTests.cs" /> <Compile Include="Serialization\NullValueHandlingTests.cs" />
<Compile Include="Serialization\ReferenceLoopHandlingTests.cs" /> <Compile Include="Serialization\ReferenceLoopHandlingTests.cs" />
<Compile Include="Serialization\TraceWriterTests.cs" />
<Compile Include="Serialization\WebApiIntegrationTests.cs" /> <Compile Include="Serialization\WebApiIntegrationTests.cs" />
<Compile Include="TestObjects\Bar.cs" /> <Compile Include="TestObjects\Bar.cs" />
<Compile Include="TestObjects\Car.cs" /> <Compile Include="TestObjects\Car.cs" />
@@ -141,6 +141,7 @@
<Compile Include="Serialization\JsonPropertyCollectionTests.cs" /> <Compile Include="Serialization\JsonPropertyCollectionTests.cs" />
<Compile Include="Serialization\NullValueHandlingTests.cs" /> <Compile Include="Serialization\NullValueHandlingTests.cs" />
<Compile Include="Serialization\ReferenceLoopHandlingTests.cs" /> <Compile Include="Serialization\ReferenceLoopHandlingTests.cs" />
<Compile Include="Serialization\TraceWriterTests.cs" />
<Compile Include="Serialization\WebApiIntegrationTests.cs" /> <Compile Include="Serialization\WebApiIntegrationTests.cs" />
<Compile Include="TestObjects\Bar.cs" /> <Compile Include="TestObjects\Bar.cs" />
<Compile Include="TestObjects\Car.cs" /> <Compile Include="TestObjects\Car.cs" />
@@ -134,6 +134,7 @@
<Compile Include="Serialization\DefaultValueHandlingTests.cs" /> <Compile Include="Serialization\DefaultValueHandlingTests.cs" />
<Compile Include="Serialization\DynamicTests.cs" /> <Compile Include="Serialization\DynamicTests.cs" />
<Compile Include="Serialization\ReferenceLoopHandlingTests.cs" /> <Compile Include="Serialization\ReferenceLoopHandlingTests.cs" />
<Compile Include="Serialization\TraceWriterTests.cs" />
<Compile Include="Serialization\WebApiIntegrationTests.cs" /> <Compile Include="Serialization\WebApiIntegrationTests.cs" />
<Compile Include="Serialization\JsonPropertyCollectionTests.cs" /> <Compile Include="Serialization\JsonPropertyCollectionTests.cs" />
<Compile Include="Serialization\NullValueHandlingTests.cs" /> <Compile Include="Serialization\NullValueHandlingTests.cs" />
@@ -139,6 +139,7 @@
<Compile Include="Serialization\ReferenceLoopHandlingTests.cs" /> <Compile Include="Serialization\ReferenceLoopHandlingTests.cs" />
<Compile Include="Serialization\SerializationErrorHandlingTests.cs" /> <Compile Include="Serialization\SerializationErrorHandlingTests.cs" />
<Compile Include="Serialization\SerializationEventAttributeTests.cs" /> <Compile Include="Serialization\SerializationEventAttributeTests.cs" />
<Compile Include="Serialization\TraceWriterTests.cs" />
<Compile Include="Serialization\TypeNameHandlingTests.cs" /> <Compile Include="Serialization\TypeNameHandlingTests.cs" />
<Compile Include="Serialization\WebApiIntegrationTests.cs" /> <Compile Include="Serialization\WebApiIntegrationTests.cs" />
<Compile Include="TestObjects\AbstractGenericBase.cs" /> <Compile Include="TestObjects\AbstractGenericBase.cs" />
@@ -173,6 +173,7 @@
<Compile Include="Serialization\ReferenceLoopHandlingTests.cs" /> <Compile Include="Serialization\ReferenceLoopHandlingTests.cs" />
<Compile Include="Serialization\SerializationErrorHandlingTests.cs" /> <Compile Include="Serialization\SerializationErrorHandlingTests.cs" />
<Compile Include="Serialization\SerializationEventAttributeTests.cs" /> <Compile Include="Serialization\SerializationEventAttributeTests.cs" />
<Compile Include="Serialization\TraceWriterTests.cs" />
<Compile Include="Serialization\TypeNameHandlingTests.cs" /> <Compile Include="Serialization\TypeNameHandlingTests.cs" />
<Compile Include="Serialization\WebApiIntegrationTests.cs" /> <Compile Include="Serialization\WebApiIntegrationTests.cs" />
<Compile Include="SilverlightTests.cs" /> <Compile Include="SilverlightTests.cs" />
@@ -153,6 +153,7 @@
<Compile Include="Serialization\DefaultValueHandlingTests.cs" /> <Compile Include="Serialization\DefaultValueHandlingTests.cs" />
<Compile Include="Serialization\DynamicTests.cs" /> <Compile Include="Serialization\DynamicTests.cs" />
<Compile Include="Serialization\ReferenceLoopHandlingTests.cs" /> <Compile Include="Serialization\ReferenceLoopHandlingTests.cs" />
<Compile Include="Serialization\TraceWriterTests.cs" />
<Compile Include="Serialization\WebApiIntegrationTests.cs" /> <Compile Include="Serialization\WebApiIntegrationTests.cs" />
<Compile Include="Serialization\JsonPropertyCollectionTests.cs" /> <Compile Include="Serialization\JsonPropertyCollectionTests.cs" />
<Compile Include="Serialization\NullValueHandlingTests.cs" /> <Compile Include="Serialization\NullValueHandlingTests.cs" />
@@ -64,8 +64,8 @@ namespace Newtonsoft.Json.Tests
[TestFixture] [TestFixture]
public class PerformanceTests : TestFixtureBase public class PerformanceTests : TestFixtureBase
{ {
private const int Iterations = 100; //private const int Iterations = 100;
//private const int Iterations = 5000; private const int Iterations = 5000;
#region Data #region Data
@@ -76,5 +76,5 @@ using System.Security;
// by using the '*' as shown below: // by using the '*' as shown below:
[assembly: AssemblyVersion("4.5.0.0")] [assembly: AssemblyVersion("4.5.0.0")]
#if !PocketPC #if !PocketPC
[assembly: AssemblyFileVersion("4.5.10.15504")] [assembly: AssemblyFileVersion("4.5.10.15509")]
#endif #endif
@@ -0,0 +1,510 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters;
using System.Text;
#if !NETFX_CORE
using NUnit.Framework;
#else
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
using TestFixture = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestClassAttribute;
using Test = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestMethodAttribute;
#endif
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
using Newtonsoft.Json.Tests.TestObjects;
#if NET20
using Newtonsoft.Json.Utilities.LinqBridge;
#else
using System.Linq;
#endif
namespace Newtonsoft.Json.Tests.Serialization
{
[TestFixture]
public class TraceWriterTests : TestFixtureBase
{
[Test]
public void Serialize()
{
var traceWriter = new InMemoryTraceWriter
{
LevelFilter = TraceLevel.Info
};
string json =
JsonConvert.SerializeObject(
new TraceTestObject
{
StringArray = new[] {"1", "2"},
IntList = new List<int> {1, 2},
Version = new Version(1, 2, 3, 4),
StringDictionary =
new Dictionary<string, string>
{
{"1", "!"},
{"Two", "!!"},
{"III", "!!!"}
}
},
new JsonSerializerSettings
{
TraceWriter = traceWriter,
Formatting = Formatting.Indented
});
Assert.AreEqual("Started serializing Newtonsoft.Json.Tests.Serialization.TraceTestObject. Path ''.", traceWriter.TraceRecords[0].Message);
Assert.AreEqual("Started serializing System.Collections.Generic.List`1[System.Int32]. Path 'IntList'.", traceWriter.TraceRecords[1].Message);
Assert.AreEqual("Finished serializing System.Collections.Generic.List`1[System.Int32]. Path 'IntList'.", traceWriter.TraceRecords[2].Message);
Assert.AreEqual("Started serializing System.String[]. Path 'StringArray'.", traceWriter.TraceRecords[3].Message);
Assert.AreEqual("Finished serializing System.String[]. Path 'StringArray'.", traceWriter.TraceRecords[4].Message);
Assert.AreEqual("Started serializing System.Version. Path 'Version'.", traceWriter.TraceRecords[5].Message);
Assert.AreEqual("Finished serializing System.Version. Path 'Version'.", traceWriter.TraceRecords[6].Message);
Assert.AreEqual("Started serializing System.Collections.Generic.Dictionary`2[System.String,System.String]. Path 'StringDictionary'.", traceWriter.TraceRecords[7].Message);
Assert.AreEqual("Finished serializing System.Collections.Generic.Dictionary`2[System.String,System.String]. Path 'StringDictionary'.", traceWriter.TraceRecords[8].Message);
Assert.AreEqual("Finished serializing Newtonsoft.Json.Tests.Serialization.TraceTestObject. Path ''.", traceWriter.TraceRecords[9].Message);
}
[Test]
public void Deserialize()
{
InMemoryTraceWriter traceWriter = new InMemoryTraceWriter
{
LevelFilter = TraceLevel.Info
};
TraceTestObject o2 = JsonConvert.DeserializeObject<TraceTestObject>(
@"{
""IntList"": [
1,
2
],
""StringArray"": [
""1"",
""2""
],
""Version"": {
""Major"": 1,
""Minor"": 2,
""Build"": 3,
""Revision"": 4,
""MajorRevision"": 0,
""MinorRevision"": 4
},
""StringDictionary"": {
""1"": ""!"",
""Two"": ""!!"",
""III"": ""!!!""
}
}",
new JsonSerializerSettings
{
TraceWriter = traceWriter
});
Assert.AreEqual(2, o2.IntList.Count);
Assert.AreEqual(2, o2.StringArray.Length);
Assert.AreEqual(1, o2.Version.Major);
Assert.AreEqual(2, o2.Version.Minor);
Assert.AreEqual(3, o2.StringDictionary.Count);
Assert.AreEqual("Started deserializing Newtonsoft.Json.Tests.Serialization.TraceTestObject. Path 'IntList', line 2, position 13.", traceWriter.TraceRecords[0].Message);
Assert.AreEqual("Started deserializing System.Collections.Generic.IList`1[System.Int32]. Path 'IntList', line 2, position 15.", traceWriter.TraceRecords[1].Message);
Assert.AreEqual("Finished deserializing System.Collections.Generic.IList`1[System.Int32]. Path 'IntList', line 5, position 4.", traceWriter.TraceRecords[2].Message);
Assert.AreEqual("Started deserializing System.String[]. Path 'StringArray', line 6, position 19.", traceWriter.TraceRecords[3].Message);
Assert.AreEqual("Finished deserializing System.String[]. Path 'StringArray', line 9, position 4.", traceWriter.TraceRecords[4].Message);
Assert.AreEqual("Deserializing System.Version using a non-default constructor 'Void .ctor(Int32, Int32, Int32, Int32)'. Path 'Version.Major', line 11, position 13.", traceWriter.TraceRecords[5].Message);
Assert.AreEqual("Started deserializing System.Version. Path 'Version', line 17, position 4.", traceWriter.TraceRecords[6].Message);
Assert.AreEqual("Finished deserializing System.Version. Path 'Version', line 17, position 4.", traceWriter.TraceRecords[7].Message);
Assert.AreEqual("Started deserializing System.Collections.Generic.IDictionary`2[System.String,System.String]. Path 'StringDictionary.1', line 19, position 9.", traceWriter.TraceRecords[8].Message);
Assert.AreEqual("Finished deserializing System.Collections.Generic.IDictionary`2[System.String,System.String]. Path 'StringDictionary', line 22, position 4.", traceWriter.TraceRecords[9].Message);
Assert.AreEqual("Finished deserializing Newtonsoft.Json.Tests.Serialization.TraceTestObject. Path '', line 23, position 2.", traceWriter.TraceRecords[10].Message);
}
[Test]
public void ErrorDeserializing()
{
string json = @"{""Integer"":""hi""}";
var traceWriter = new InMemoryTraceWriter
{
LevelFilter = TraceLevel.Info
};
ExceptionAssert.Throws<Exception>(
"Could not convert string to integer: hi. Path 'Integer', line 1, position 15.",
() =>
{
JsonConvert.DeserializeObject<IntegerTestClass>(
json,
new JsonSerializerSettings
{
TraceWriter = traceWriter
});
});
Assert.AreEqual(2, traceWriter.TraceRecords.Count);
Assert.AreEqual(TraceLevel.Info, traceWriter.TraceRecords[0].Level);
Assert.AreEqual("Started deserializing Newtonsoft.Json.Tests.Serialization.IntegerTestClass. Path 'Integer', line 1, position 11.", traceWriter.TraceRecords[0].Message);
Assert.AreEqual(TraceLevel.Error, traceWriter.TraceRecords[1].Level);
Assert.AreEqual("Error deserializing Newtonsoft.Json.Tests.Serialization.IntegerTestClass. Could not convert string to integer: hi. Path 'Integer', line 1, position 15.", traceWriter.TraceRecords[1].Message);
}
[Test]
public void ErrorDeserializingNested()
{
string json = @"{""IntList"":[1, ""two""]}";
var traceWriter = new InMemoryTraceWriter
{
LevelFilter = TraceLevel.Info
};
ExceptionAssert.Throws<Exception>(
"Could not convert string to integer: two. Path 'IntList[1]', line 1, position 20.",
() =>
{
JsonConvert.DeserializeObject<TraceTestObject>(
json,
new JsonSerializerSettings
{
TraceWriter = traceWriter
});
});
Assert.AreEqual(3, traceWriter.TraceRecords.Count);
Assert.AreEqual(TraceLevel.Info, traceWriter.TraceRecords[0].Level);
Assert.AreEqual("Started deserializing Newtonsoft.Json.Tests.Serialization.TraceTestObject. Path 'IntList', line 1, position 11.", traceWriter.TraceRecords[0].Message);
Assert.AreEqual(TraceLevel.Info, traceWriter.TraceRecords[1].Level);
Assert.AreEqual("Started deserializing System.Collections.Generic.IList`1[System.Int32]. Path 'IntList', line 1, position 12.", traceWriter.TraceRecords[1].Message);
Assert.AreEqual(TraceLevel.Error, traceWriter.TraceRecords[2].Level);
Assert.AreEqual("Error deserializing System.Collections.Generic.IList`1[System.Int32]. Could not convert string to integer: two. Path 'IntList[1]', line 1, position 20.", traceWriter.TraceRecords[2].Message);
}
[Test]
public void SerializeDictionarysWithPreserveObjectReferences()
{
PreserveReferencesHandlingTests.CircularDictionary circularDictionary = new PreserveReferencesHandlingTests.CircularDictionary();
circularDictionary.Add("other", new PreserveReferencesHandlingTests.CircularDictionary { { "blah", null } });
circularDictionary.Add("self", circularDictionary);
InMemoryTraceWriter traceWriter = new InMemoryTraceWriter
{
LevelFilter = TraceLevel.Verbose
};
JsonConvert.SerializeObject(
circularDictionary,
Formatting.Indented,
new JsonSerializerSettings
{
PreserveReferencesHandling = PreserveReferencesHandling.All,
TraceWriter = traceWriter
});
Assert.IsTrue(traceWriter.TraceRecords.Any(r => r.Message == "Writing object reference Id '1' for Newtonsoft.Json.Tests.Serialization.PreserveReferencesHandlingTests+CircularDictionary. Path ''."));
Assert.IsTrue(traceWriter.TraceRecords.Any(r => r.Message == "Writing object reference Id '2' for Newtonsoft.Json.Tests.Serialization.PreserveReferencesHandlingTests+CircularDictionary. Path 'other'."));
Assert.IsTrue(traceWriter.TraceRecords.Any(r => r.Message == "Writing object reference to Id '1' for Newtonsoft.Json.Tests.Serialization.PreserveReferencesHandlingTests+CircularDictionary. Path 'self'."));
}
[Test]
public void DeserializeDictionarysWithPreserveObjectReferences()
{
string json = @"{
""$id"": ""1"",
""other"": {
""$id"": ""2"",
""blah"": null
},
""self"": {
""$ref"": ""1""
}
}";
InMemoryTraceWriter traceWriter = new InMemoryTraceWriter
{
LevelFilter = TraceLevel.Verbose
};
JsonConvert.DeserializeObject<PreserveReferencesHandlingTests.CircularDictionary>(json,
new JsonSerializerSettings
{
PreserveReferencesHandling = PreserveReferencesHandling.All,
TraceWriter = traceWriter
});
Assert.IsTrue(traceWriter.TraceRecords.Any(r => r.Message == "Read object reference Id '1' for Newtonsoft.Json.Tests.Serialization.PreserveReferencesHandlingTests+CircularDictionary. Path 'other', line 3, position 11."));
Assert.IsTrue(traceWriter.TraceRecords.Any(r => r.Message == "Read object reference Id '2' for Newtonsoft.Json.Tests.Serialization.PreserveReferencesHandlingTests+CircularDictionary. Path 'other.blah', line 5, position 12."));
Assert.IsTrue(traceWriter.TraceRecords.Any(r => r.Message == "Resolved object reference '1' to Newtonsoft.Json.Tests.Serialization.PreserveReferencesHandlingTests+CircularDictionary. Path 'self', line 9, position 4."));
}
[Test]
public void WriteTypeNameForObjects()
{
InMemoryTraceWriter traceWriter = new InMemoryTraceWriter
{
LevelFilter = TraceLevel.Verbose
};
IList<object> l = new List<object>
{
new Dictionary<string, string> { {"key!", "value!"}},
new Version(1, 2, 3, 4)
};
JsonConvert.SerializeObject(l, Formatting.Indented, new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.All,
TraceWriter = traceWriter
});
Assert.AreEqual("Started serializing System.Collections.Generic.List`1[System.Object]. Path ''.", traceWriter.TraceRecords[0].Message);
Assert.AreEqual("Writing type name 'System.Collections.Generic.List`1[[System.Object, mscorlib]], mscorlib' for System.Collections.Generic.List`1[System.Object]. Path ''.", traceWriter.TraceRecords[1].Message);
Assert.AreEqual("Started serializing System.Collections.Generic.Dictionary`2[System.String,System.String]. Path '$values'.", traceWriter.TraceRecords[2].Message);
Assert.AreEqual("Writing type name 'System.Collections.Generic.Dictionary`2[[System.String, mscorlib],[System.String, mscorlib]], mscorlib' for System.Collections.Generic.Dictionary`2[System.String,System.String]. Path '$values[0]'.", traceWriter.TraceRecords[3].Message);
Assert.AreEqual("Finished serializing System.Collections.Generic.Dictionary`2[System.String,System.String]. Path '$values[0]'.", traceWriter.TraceRecords[4].Message);
Assert.AreEqual("Started serializing System.Version. Path '$values[0]'.", traceWriter.TraceRecords[5].Message);
Assert.AreEqual("Writing type name 'System.Version, mscorlib' for System.Version. Path '$values[1]'.", traceWriter.TraceRecords[6].Message);
Assert.AreEqual("Finished serializing System.Version. Path '$values[1]'.", traceWriter.TraceRecords[7].Message);
Assert.AreEqual("Finished serializing System.Collections.Generic.List`1[System.Object]. Path ''.", traceWriter.TraceRecords[8].Message);
}
[Test]
public void SerializeConverter()
{
InMemoryTraceWriter traceWriter = new InMemoryTraceWriter
{
LevelFilter = TraceLevel.Verbose
};
IList<DateTime> d = new List<DateTime>
{
new DateTime(2000, 12, 12, 12, 12, 12, DateTimeKind.Utc)
};
string json = JsonConvert.SerializeObject(d, Formatting.Indented, new JsonSerializerSettings
{
Converters = { new JavaScriptDateTimeConverter() },
TraceWriter = traceWriter
});
Assert.AreEqual("Started serializing System.Collections.Generic.List`1[System.DateTime]. Path ''.", traceWriter.TraceRecords[0].Message);
Assert.AreEqual("Started serializing System.DateTime with converter Newtonsoft.Json.Converters.JavaScriptDateTimeConverter. Path ''.", traceWriter.TraceRecords[1].Message);
Assert.AreEqual("Finished serializing System.DateTime with converter Newtonsoft.Json.Converters.JavaScriptDateTimeConverter. Path '[0]'.", traceWriter.TraceRecords[2].Message);
Assert.AreEqual("Finished serializing System.Collections.Generic.List`1[System.DateTime]. Path ''.", traceWriter.TraceRecords[3].Message);
}
[Test]
public void DeserializeConverter()
{
string json = @"[new Date(976623132000)]";
InMemoryTraceWriter traceWriter =
new InMemoryTraceWriter
{
LevelFilter = TraceLevel.Verbose
};
JsonConvert.DeserializeObject<List<DateTime>>(
json,
new JsonSerializerSettings
{
Converters = {new JavaScriptDateTimeConverter()},
TraceWriter = traceWriter
});
Assert.AreEqual("Started deserializing System.Collections.Generic.List`1[System.DateTime]. Path '', line 1, position 1.", traceWriter.TraceRecords[0].Message);
Assert.AreEqual("Started deserializing System.DateTime with converter Newtonsoft.Json.Converters.JavaScriptDateTimeConverter. Path '[0]', line 1, position 10.", traceWriter.TraceRecords[1].Message);
Assert.AreEqual("Finished deserializing System.DateTime with converter Newtonsoft.Json.Converters.JavaScriptDateTimeConverter. Path '[0]', line 1, position 23.", traceWriter.TraceRecords[2].Message);
Assert.AreEqual("Finished deserializing System.Collections.Generic.List`1[System.DateTime]. Path '', line 1, position 24.", traceWriter.TraceRecords[3].Message);
}
[Test]
public void DeserializeTypeName()
{
InMemoryTraceWriter traceWriter = new InMemoryTraceWriter
{
LevelFilter = TraceLevel.Verbose
};
string json = @"{
""$type"": ""System.Collections.Generic.List`1[[System.Object, mscorlib]], mscorlib"",
""$values"": [
{
""$type"": ""System.Collections.Generic.Dictionary`2[[System.String, mscorlib],[System.String, mscorlib]], mscorlib"",
""key!"": ""value!""
},
{
""$type"": ""System.Version, mscorlib"",
""Major"": 1,
""Minor"": 2,
""Build"": 3,
""Revision"": 4,
""MajorRevision"": 0,
""MinorRevision"": 4
}
]
}";
JsonConvert.DeserializeObject(json, null, new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.All,
TraceWriter = traceWriter
});
Assert.AreEqual("Resolved type 'System.Collections.Generic.List`1[[System.Object, mscorlib]], mscorlib' to System.Collections.Generic.List`1[System.Object]. Path '$type', line 2, position 84.", traceWriter.TraceRecords[0].Message);
Assert.AreEqual("Started deserializing System.Collections.Generic.List`1[System.Object]. Path '$values', line 3, position 15.", traceWriter.TraceRecords[1].Message);
Assert.AreEqual("Resolved type 'System.Collections.Generic.Dictionary`2[[System.String, mscorlib],[System.String, mscorlib]], mscorlib' to System.Collections.Generic.Dictionary`2[System.String,System.String]. Path '$values[0].$type', line 5, position 120.", traceWriter.TraceRecords[2].Message);
Assert.AreEqual("Started deserializing System.Collections.Generic.Dictionary`2[System.String,System.String]. Path '$values[0].key!', line 6, position 14.", traceWriter.TraceRecords[3].Message);
Assert.AreEqual("Finished deserializing System.Collections.Generic.Dictionary`2[System.String,System.String]. Path '$values[0]', line 7, position 6.", traceWriter.TraceRecords[4].Message);
Assert.AreEqual("Resolved type 'System.Version, mscorlib' to System.Version. Path '$values[1].$type', line 9, position 42.", traceWriter.TraceRecords[5].Message);
Assert.AreEqual("Deserializing System.Version using a non-default constructor 'Void .ctor(Int32, Int32, Int32, Int32)'. Path '$values[1].Major', line 10, position 15.", traceWriter.TraceRecords[6].Message);
Assert.AreEqual("Started deserializing System.Version. Path '$values[1]', line 16, position 6.", traceWriter.TraceRecords[7].Message);
Assert.AreEqual("Finished deserializing System.Version. Path '$values[1]', line 16, position 6.", traceWriter.TraceRecords[8].Message);
Assert.AreEqual("Finished deserializing System.Collections.Generic.List`1[System.Object]. Path '$values', line 17, position 4.", traceWriter.TraceRecords[9].Message);
}
#if !(NETFX_CORE || PORTABLE || SILVERLIGHT)
[Test]
public void DeserializeISerializable()
{
InMemoryTraceWriter traceWriter = new InMemoryTraceWriter
{
LevelFilter = TraceLevel.Verbose
};
ExceptionAssert.Throws<SerializationException>(
"Member 'ClassName' was not found.",
() =>
{
JsonConvert.DeserializeObject<Exception>(
"{}",
new JsonSerializerSettings
{
TraceWriter = traceWriter
});
});
Assert.AreEqual("Deserializing System.Exception using ISerializable constructor. Path '', line 1, position 2.", traceWriter.TraceRecords[0].Message);
Assert.AreEqual(TraceLevel.Info, traceWriter.TraceRecords[0].Level);
Assert.AreEqual("Error deserializing System.Exception. Member 'ClassName' was not found. Path '', line 1, position 2.", traceWriter.TraceRecords[1].Message);
Assert.AreEqual(TraceLevel.Error, traceWriter.TraceRecords[1].Level);
}
#endif
[Test]
public void DeserializeMissingMember()
{
InMemoryTraceWriter traceWriter = new InMemoryTraceWriter
{
LevelFilter = TraceLevel.Verbose
};
JsonConvert.DeserializeObject<Person>(
"{'MissingMemberProperty':'!!'}",
new JsonSerializerSettings
{
TraceWriter = traceWriter
});
Assert.AreEqual("Started deserializing Newtonsoft.Json.Tests.TestObjects.Person. Path 'MissingMemberProperty', line 1, position 25.", traceWriter.TraceRecords[0].Message);
Assert.AreEqual("Could not find member 'MissingMemberProperty' on Newtonsoft.Json.Tests.TestObjects.Person. Path 'MissingMemberProperty', line 1, position 25.", traceWriter.TraceRecords[1].Message);
Assert.AreEqual("Finished deserializing Newtonsoft.Json.Tests.TestObjects.Person. Path '', line 1, position 30.", traceWriter.TraceRecords[2].Message);
}
[Test]
public void DeserializeMissingMemberConstructor()
{
InMemoryTraceWriter traceWriter = new InMemoryTraceWriter
{
LevelFilter = TraceLevel.Verbose
};
string json = @"{
""Major"": 1,
""Minor"": 2,
""Build"": 3,
""Revision"": 4,
""MajorRevision"": 0,
""MinorRevision"": 4,
""MissingMemberProperty"": null
}";
JsonConvert.DeserializeObject<Version>(json, new JsonSerializerSettings
{
TraceWriter = traceWriter
});
Assert.AreEqual("Deserializing System.Version using a non-default constructor 'Void .ctor(Int32, Int32, Int32, Int32)'. Path 'Major', line 2, position 11.", traceWriter.TraceRecords[0].Message);
Assert.AreEqual("Could not find member 'MissingMemberProperty' on System.Version. Path 'MissingMemberProperty', line 8, position 32.", traceWriter.TraceRecords[1].Message);
Assert.AreEqual("Started deserializing System.Version. Path '', line 9, position 2.", traceWriter.TraceRecords[2].Message);
Assert.AreEqual("Finished deserializing System.Version. Path '', line 9, position 2.", traceWriter.TraceRecords[3].Message);
}
}
public class TraceRecord
{
public string Message { get; set; }
public TraceLevel Level { get; set; }
public Exception Exception { get; set; }
public override string ToString()
{
return Level + " - " + Message;
}
}
public class InMemoryTraceWriter : ITraceWriter
{
public TraceLevel LevelFilter { get; set; }
public IList<TraceRecord> TraceRecords { get; set; }
public InMemoryTraceWriter()
{
LevelFilter = TraceLevel.Verbose;
TraceRecords = new List<TraceRecord>();
}
public void Trace(TraceLevel level, string message, Exception ex)
{
TraceRecords.Add(
new TraceRecord
{
Level = level,
Message = message,
Exception = ex
});
}
public override string ToString()
{
StringBuilder sb = new StringBuilder();
foreach (var traceRecord in TraceRecords)
{
sb.AppendLine(traceRecord.Message);
}
return sb.ToString();
}
}
public class TraceTestObject
{
public IList<int> IntList { get; set; }
public string[] StringArray { get; set; }
public Version Version { get; set; }
public IDictionary<string, string> StringDictionary { get; set; }
}
public class IntegerTestClass
{
public int Integer { get; set; }
}
}
-23
View File
@@ -81,28 +81,5 @@ namespace Newtonsoft.Json
{ {
} }
#endif #endif
internal static string FormatExceptionMessage(IJsonLineInfo lineInfo, string path, string message)
{
// don't add a fullstop and space when message ends with a new line
if (!message.EndsWith(Environment.NewLine))
{
message = message.Trim();
if (!message.EndsWith("."))
message += ".";
message += " ";
}
message += "Path '{0}'".FormatWith(CultureInfo.InvariantCulture, path);
if (lineInfo != null && lineInfo.HasLineInfo())
message += ", line {0}, position {1}".FormatWith(CultureInfo.InvariantCulture, lineInfo.LineNumber, lineInfo.LinePosition);
message += ".";
return message;
}
} }
} }
+26
View File
@@ -23,8 +23,11 @@
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
#endregion #endregion
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Globalization;
using System.Text; using System.Text;
using Newtonsoft.Json.Utilities;
namespace Newtonsoft.Json namespace Newtonsoft.Json
{ {
@@ -85,5 +88,28 @@ namespace Newtonsoft.Json
return sb.ToString(); return sb.ToString();
} }
internal static string FormatMessage(IJsonLineInfo lineInfo, string path, string message)
{
// don't add a fullstop and space when message ends with a new line
if (!message.EndsWith(Environment.NewLine))
{
message = message.Trim();
if (!message.EndsWith("."))
message += ".";
message += " ";
}
message += "Path '{0}'".FormatWith(CultureInfo.InvariantCulture, path);
if (lineInfo != null && lineInfo.HasLineInfo())
message += ", line {0}, position {1}".FormatWith(CultureInfo.InvariantCulture, lineInfo.LineNumber, lineInfo.LinePosition);
message += ".";
return message;
}
} }
} }
+2 -8
View File
@@ -786,14 +786,8 @@ namespace Newtonsoft.Json
private void UpdateScopeWithFinishedValue() private void UpdateScopeWithFinishedValue()
{ {
if (_currentPosition.Type == JsonContainerType.Array if (_currentPosition.HasIndex)
|| _currentPosition.Type == JsonContainerType.Constructor) _currentPosition.Position++;
{
if (_currentPosition.Position == null)
_currentPosition.Position = 0;
else
_currentPosition.Position++;
}
} }
private void ValidateEnd(JsonToken endToken) private void ValidateEnd(JsonToken endToken)
+1 -1
View File
@@ -118,7 +118,7 @@ namespace Newtonsoft.Json
internal static JsonReaderException Create(IJsonLineInfo lineInfo, string path, string message, Exception ex) internal static JsonReaderException Create(IJsonLineInfo lineInfo, string path, string message, Exception ex)
{ {
message = FormatExceptionMessage(lineInfo, path, message); message = JsonPosition.FormatMessage(lineInfo, path, message);
int lineNumber; int lineNumber;
int linePosition; int linePosition;
@@ -92,7 +92,7 @@ namespace Newtonsoft.Json
internal static JsonSerializationException Create(IJsonLineInfo lineInfo, string path, string message, Exception ex) internal static JsonSerializationException Create(IJsonLineInfo lineInfo, string path, string message, Exception ex)
{ {
message = FormatExceptionMessage(lineInfo, path, message); message = JsonPosition.FormatMessage(lineInfo, path, message);
return new JsonSerializationException(message, ex); return new JsonSerializationException(message, ex);
} }
+19
View File
@@ -55,6 +55,7 @@ namespace Newtonsoft.Json
private JsonConverterCollection _converters; private JsonConverterCollection _converters;
private IContractResolver _contractResolver; private IContractResolver _contractResolver;
private IReferenceResolver _referenceResolver; private IReferenceResolver _referenceResolver;
private ITraceWriter _traceWriter;
private SerializationBinder _binder; private SerializationBinder _binder;
private StreamingContext _context; private StreamingContext _context;
private Formatting? _formatting; private Formatting? _formatting;
@@ -110,6 +111,22 @@ namespace Newtonsoft.Json
} }
} }
/// <summary>
/// Gets or sets the <see cref="ITraceWriter"/> used by the serializer when writing trace messages.
/// </summary>
/// <value>The trace writer.</value>
public virtual ITraceWriter TraceWriter
{
get
{
return _traceWriter;
}
set
{
_traceWriter = value;
}
}
/// <summary> /// <summary>
/// Gets or sets how type name writing and reading is handled by the serializer. /// Gets or sets how type name writing and reading is handled by the serializer.
/// </summary> /// </summary>
@@ -430,6 +447,8 @@ namespace Newtonsoft.Json
jsonSerializer.ContractResolver = settings.ContractResolver; jsonSerializer.ContractResolver = settings.ContractResolver;
if (settings.ReferenceResolver != null) if (settings.ReferenceResolver != null)
jsonSerializer.ReferenceResolver = settings.ReferenceResolver; jsonSerializer.ReferenceResolver = settings.ReferenceResolver;
if (settings.TraceWriter != null)
jsonSerializer.TraceWriter = settings.TraceWriter;
if (settings.Binder != null) if (settings.Binder != null)
jsonSerializer.Binder = settings.Binder; jsonSerializer.Binder = settings.Binder;
} }
@@ -137,6 +137,12 @@ namespace Newtonsoft.Json
/// <value>The reference resolver.</value> /// <value>The reference resolver.</value>
public IReferenceResolver ReferenceResolver { get; set; } public IReferenceResolver ReferenceResolver { get; set; }
/// <summary>
/// Gets or sets the <see cref="ITraceWriter"/> used by the serializer when writing trace messages.
/// </summary>
/// <value>The trace writer.</value>
public ITraceWriter TraceWriter { get; set; }
/// <summary> /// <summary>
/// Gets or sets the <see cref="SerializationBinder"/> used by the serializer when resolving type names. /// Gets or sets the <see cref="SerializationBinder"/> used by the serializer when resolving type names.
/// </summary> /// </summary>
+1 -1
View File
@@ -99,7 +99,7 @@ namespace Newtonsoft.Json
internal static JsonWriterException Create(string path, string message, Exception ex) internal static JsonWriterException Create(string path, string message, Exception ex)
{ {
message = FormatExceptionMessage(null, path, message); message = JsonPosition.FormatMessage(null, path, message);
return new JsonWriterException(message, ex, path); return new JsonWriterException(message, ex, path);
} }
+2
View File
@@ -219,9 +219,11 @@ namespace Newtonsoft.Json.Linq
DateTime date1 = (DateTime)objA; DateTime date1 = (DateTime)objA;
DateTime date2; DateTime date2;
#if !NET20
if (objB is DateTimeOffset) if (objB is DateTimeOffset)
date2 = ((DateTimeOffset)objB).DateTime; date2 = ((DateTimeOffset)objB).DateTime;
else else
#endif
date2 = Convert.ToDateTime(objB, CultureInfo.InvariantCulture); date2 = Convert.ToDateTime(objB, CultureInfo.InvariantCulture);
return date1.CompareTo(date2); return date1.CompareTo(date2);
@@ -103,6 +103,7 @@
<Compile Include="JsonPosition.cs" /> <Compile Include="JsonPosition.cs" />
<Compile Include="Linq\JPropertyKeyedCollection.cs" /> <Compile Include="Linq\JPropertyKeyedCollection.cs" />
<Compile Include="SerializationBinder.cs" /> <Compile Include="SerializationBinder.cs" />
<Compile Include="Serialization\ITraceWriter.cs" />
<Compile Include="Serialization\JsonContainerContract.cs" /> <Compile Include="Serialization\JsonContainerContract.cs" />
<Compile Include="StreamingContext.cs" /> <Compile Include="StreamingContext.cs" />
<Compile Include="Utilities\LinqBridge.cs" /> <Compile Include="Utilities\LinqBridge.cs" />
@@ -116,8 +116,10 @@
<Compile Include="Linq\JPropertyDescriptor.cs" /> <Compile Include="Linq\JPropertyDescriptor.cs" />
<Compile Include="Linq\JPropertyKeyedCollection.cs" /> <Compile Include="Linq\JPropertyKeyedCollection.cs" />
<Compile Include="SerializationBinder.cs" /> <Compile Include="SerializationBinder.cs" />
<Compile Include="Serialization\ITraceWriter.cs" />
<Compile Include="Serialization\JsonContainerContract.cs" /> <Compile Include="Serialization\JsonContainerContract.cs" />
<Compile Include="StreamingContext.cs" /> <Compile Include="StreamingContext.cs" />
<Compile Include="TraceLevel.cs" />
<Compile Include="Utilities\DynamicProxy.cs" /> <Compile Include="Utilities\DynamicProxy.cs" />
<Compile Include="Linq\JPath.cs" /> <Compile Include="Linq\JPath.cs" />
<Compile Include="Linq\JRaw.cs" /> <Compile Include="Linq\JRaw.cs" />
@@ -148,6 +148,7 @@
<Compile Include="Serialization\ErrorEventArgs.cs" /> <Compile Include="Serialization\ErrorEventArgs.cs" />
<Compile Include="Serialization\IContractResolver.cs" /> <Compile Include="Serialization\IContractResolver.cs" />
<Compile Include="Serialization\IReferenceResolver.cs" /> <Compile Include="Serialization\IReferenceResolver.cs" />
<Compile Include="Serialization\ITraceWriter.cs" />
<Compile Include="Serialization\IValueProvider.cs" /> <Compile Include="Serialization\IValueProvider.cs" />
<Compile Include="Serialization\JsonArrayContract.cs" /> <Compile Include="Serialization\JsonArrayContract.cs" />
<Compile Include="Serialization\JsonContainerContract.cs" /> <Compile Include="Serialization\JsonContainerContract.cs" />
@@ -172,6 +173,7 @@
<Compile Include="Serialization\OnErrorAttribute.cs" /> <Compile Include="Serialization\OnErrorAttribute.cs" />
<Compile Include="Serialization\ReflectionValueProvider.cs" /> <Compile Include="Serialization\ReflectionValueProvider.cs" />
<Compile Include="StreamingContext.cs" /> <Compile Include="StreamingContext.cs" />
<Compile Include="TraceLevel.cs" />
<Compile Include="TypeNameHandling.cs" /> <Compile Include="TypeNameHandling.cs" />
<Compile Include="Utilities\Base64Encoder.cs" /> <Compile Include="Utilities\Base64Encoder.cs" />
<Compile Include="Utilities\BidirectionalDictionary.cs" /> <Compile Include="Utilities\BidirectionalDictionary.cs" />
@@ -171,6 +171,7 @@
<Compile Include="Serialization\ErrorEventArgs.cs" /> <Compile Include="Serialization\ErrorEventArgs.cs" />
<Compile Include="Serialization\IContractResolver.cs" /> <Compile Include="Serialization\IContractResolver.cs" />
<Compile Include="Serialization\IReferenceResolver.cs" /> <Compile Include="Serialization\IReferenceResolver.cs" />
<Compile Include="Serialization\ITraceWriter.cs" />
<Compile Include="Serialization\IValueProvider.cs" /> <Compile Include="Serialization\IValueProvider.cs" />
<Compile Include="Serialization\JsonArrayContract.cs" /> <Compile Include="Serialization\JsonArrayContract.cs" />
<Compile Include="Serialization\JsonContainerContract.cs" /> <Compile Include="Serialization\JsonContainerContract.cs" />
@@ -194,6 +195,7 @@
<Compile Include="Serialization\OnErrorAttribute.cs" /> <Compile Include="Serialization\OnErrorAttribute.cs" />
<Compile Include="Serialization\ReflectionValueProvider.cs" /> <Compile Include="Serialization\ReflectionValueProvider.cs" />
<Compile Include="StreamingContext.cs" /> <Compile Include="StreamingContext.cs" />
<Compile Include="TraceLevel.cs" />
<Compile Include="TypeNameHandling.cs" /> <Compile Include="TypeNameHandling.cs" />
<Compile Include="Utilities\Base64Encoder.cs" /> <Compile Include="Utilities\Base64Encoder.cs" />
<Compile Include="Utilities\BidirectionalDictionary.cs" /> <Compile Include="Utilities\BidirectionalDictionary.cs" />
@@ -217,6 +217,7 @@
<Compile Include="Serialization\ErrorEventArgs.cs" /> <Compile Include="Serialization\ErrorEventArgs.cs" />
<Compile Include="Serialization\IContractResolver.cs" /> <Compile Include="Serialization\IContractResolver.cs" />
<Compile Include="Serialization\IReferenceResolver.cs" /> <Compile Include="Serialization\IReferenceResolver.cs" />
<Compile Include="Serialization\ITraceWriter.cs" />
<Compile Include="Serialization\IValueProvider.cs" /> <Compile Include="Serialization\IValueProvider.cs" />
<Compile Include="Serialization\JsonArrayContract.cs" /> <Compile Include="Serialization\JsonArrayContract.cs" />
<Compile Include="Serialization\JsonContainerContract.cs" /> <Compile Include="Serialization\JsonContainerContract.cs" />
@@ -241,6 +242,7 @@
<Compile Include="Serialization\OnErrorAttribute.cs" /> <Compile Include="Serialization\OnErrorAttribute.cs" />
<Compile Include="Serialization\ReflectionValueProvider.cs" /> <Compile Include="Serialization\ReflectionValueProvider.cs" />
<Compile Include="StreamingContext.cs" /> <Compile Include="StreamingContext.cs" />
<Compile Include="TraceLevel.cs" />
<Compile Include="TypeNameHandling.cs" /> <Compile Include="TypeNameHandling.cs" />
<Compile Include="Utilities\Base64Encoder.cs" /> <Compile Include="Utilities\Base64Encoder.cs" />
<Compile Include="Utilities\BidirectionalDictionary.cs" /> <Compile Include="Utilities\BidirectionalDictionary.cs" />
@@ -67,6 +67,7 @@
<WarningLevel>4</WarningLevel> <WarningLevel>4</WarningLevel>
<DocumentationFile>bin\Release\Net40\Newtonsoft.Json.xml</DocumentationFile> <DocumentationFile>bin\Release\Net40\Newtonsoft.Json.xml</DocumentationFile>
<CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet> <CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet>
<UseVSHostingProcess>true</UseVSHostingProcess>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Reference Include="System" /> <Reference Include="System" />
@@ -112,6 +113,7 @@
<Compile Include="JsonConstructorAttribute.cs" /> <Compile Include="JsonConstructorAttribute.cs" />
<Compile Include="JsonPosition.cs" /> <Compile Include="JsonPosition.cs" />
<Compile Include="Linq\JPropertyKeyedCollection.cs" /> <Compile Include="Linq\JPropertyKeyedCollection.cs" />
<Compile Include="Serialization\ITraceWriter.cs" />
<Compile Include="Serialization\JsonContainerContract.cs" /> <Compile Include="Serialization\JsonContainerContract.cs" />
<Compile Include="Utilities\DynamicProxy.cs" /> <Compile Include="Utilities\DynamicProxy.cs" />
<Compile Include="Linq\JPath.cs" /> <Compile Include="Linq\JPath.cs" />
@@ -90,7 +90,7 @@ using System.Security;
// by using the '*' as shown below: // by using the '*' as shown below:
[assembly: AssemblyVersion("4.5.0.0")] [assembly: AssemblyVersion("4.5.0.0")]
#if !PocketPC #if !PocketPC
[assembly: AssemblyFileVersion("4.5.10.15504")] [assembly: AssemblyFileVersion("4.5.10.15509")]
#endif #endif
[assembly: CLSCompliant(true)] [assembly: CLSCompliant(true)]
@@ -40,6 +40,8 @@ namespace Newtonsoft.Json.Serialization
Path = path; Path = path;
} }
internal bool Traced { get; set; }
/// <summary> /// <summary>
/// Gets or sets the error. /// Gets or sets the error.
/// </summary> /// </summary>
@@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
namespace Newtonsoft.Json.Serialization
{
/// <summary>
/// Represents a trace writer.
/// </summary>
public interface ITraceWriter
{
/// <summary>
/// Gets the <see cref="TraceLevel"/> that will be used to filter the trace messages passed to the writer.
/// For example a filter level of <code>Info</code> will exclude <code>Verbose</code> messages and include <code>Info</code>,
/// <code>Warning</code> and <code>Error</code> messages.
/// </summary>
/// <value>The <see cref="TraceLevel"/> that will be used to filter the trace messages passed to the writer.</value>
TraceLevel LevelFilter { get; }
/// <summary>
/// Writes the specified trace level, message and optional exception.
/// </summary>
/// <param name="level">The <see cref="TraceLevel"/> at which to write this trace.</param>
/// <param name="message">The trace message.</param>
/// <param name="ex">The trace exception. This parameter is optional.</param>
void Trace(TraceLevel level, string message, Exception ex);
}
}
@@ -25,6 +25,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics;
using System.Net; using System.Net;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using Newtonsoft.Json.Utilities; using Newtonsoft.Json.Utilities;
@@ -54,14 +55,20 @@ namespace Newtonsoft.Json.Serialization
private ErrorContext _currentErrorContext; private ErrorContext _currentErrorContext;
private BidirectionalDictionary<string, object> _mappings; private BidirectionalDictionary<string, object> _mappings;
private bool _serializing;
internal readonly JsonSerializer Serializer; internal readonly JsonSerializer Serializer;
internal readonly ITraceWriter TraceWriter;
protected JsonSerializerInternalBase(JsonSerializer serializer) protected JsonSerializerInternalBase(JsonSerializer serializer)
{ {
ValidationUtils.ArgumentNotNull(serializer, "serializer"); ValidationUtils.ArgumentNotNull(serializer, "serializer");
Serializer = serializer; Serializer = serializer;
TraceWriter = serializer.TraceWriter;
// kind of a hack but meh. might clean this up later
_serializing = (GetType() == typeof (JsonSerializerInternalWriter));
} }
internal BidirectionalDictionary<string, object> DefaultReferenceMappings internal BidirectionalDictionary<string, object> DefaultReferenceMappings
@@ -81,7 +88,7 @@ namespace Newtonsoft.Json.Serialization
} }
} }
protected ErrorContext GetErrorContext(object currentObject, object member, string path, Exception error) private ErrorContext GetErrorContext(object currentObject, object member, string path, Exception error)
{ {
if (_currentErrorContext == null) if (_currentErrorContext == null)
_currentErrorContext = new ErrorContext(currentObject, member, path, error); _currentErrorContext = new ErrorContext(currentObject, member, path, error);
@@ -100,9 +107,27 @@ namespace Newtonsoft.Json.Serialization
_currentErrorContext = null; _currentErrorContext = null;
} }
protected bool IsErrorHandled(object currentObject, JsonContract contract, object keyValue, string path, Exception ex) protected bool IsErrorHandled(object currentObject, JsonContract contract, object keyValue, IJsonLineInfo lineInfo, string path, Exception ex)
{ {
ErrorContext errorContext = GetErrorContext(currentObject, keyValue, path, ex); ErrorContext errorContext = GetErrorContext(currentObject, keyValue, path, ex);
if (TraceWriter != null && TraceWriter.LevelFilter >= TraceLevel.Error && !errorContext.Traced)
{
// only write error once
errorContext.Traced = true;
string message = (_serializing) ? "Error serializing" : "Error deserializing";
if (contract != null)
message += " " + contract.UnderlyingType;
message += ". " + ex.Message;
// add line information to non-json.net exception message
if (!(ex is JsonException))
message = JsonPosition.FormatMessage(lineInfo, path, message);
TraceWriter.Trace(TraceLevel.Error, message, ex);
}
if (contract != null) if (contract != null)
contract.InvokeOnError(currentObject, Serializer.Context, errorContext); contract.InvokeOnError(currentObject, Serializer.Context, errorContext);
@@ -31,6 +31,7 @@ using System.Collections.ObjectModel;
using System.ComponentModel; using System.ComponentModel;
using System.Dynamic; using System.Dynamic;
#endif #endif
using System.Diagnostics;
using System.Globalization; using System.Globalization;
using System.Reflection; using System.Reflection;
using System.Runtime.Serialization; using System.Runtime.Serialization;
@@ -46,6 +47,13 @@ namespace Newtonsoft.Json.Serialization
{ {
internal class JsonSerializerInternalReader : JsonSerializerInternalBase internal class JsonSerializerInternalReader : JsonSerializerInternalBase
{ {
internal enum PropertyPresence
{
None,
Null,
Value
}
private JsonSerializerProxy _internalSerializer; private JsonSerializerProxy _internalSerializer;
#if !(SILVERLIGHT || NETFX_CORE || PORTABLE) #if !(SILVERLIGHT || NETFX_CORE || PORTABLE)
private JsonFormatterConverter _formatterConverter; private JsonFormatterConverter _formatterConverter;
@@ -129,7 +137,7 @@ namespace Newtonsoft.Json.Serialization
object deserializedValue; object deserializedValue;
if (converter != null && converter.CanRead) if (converter != null && converter.CanRead)
deserializedValue = converter.ReadJson(reader, objectType, null, GetInternalSerializer()); deserializedValue = DeserializeConvertable(converter, reader, objectType, null);
else else
deserializedValue = CreateValueInternal(reader, objectType, contract, null, null, null, null); deserializedValue = CreateValueInternal(reader, objectType, contract, null, null, null, null);
@@ -143,7 +151,7 @@ namespace Newtonsoft.Json.Serialization
} }
catch (Exception ex) catch (Exception ex)
{ {
if (IsErrorHandled(null, contract, null, reader.Path, ex)) if (IsErrorHandled(null, contract, null, reader as IJsonLineInfo, reader.Path, ex))
{ {
HandleError(reader, false, 0); HandleError(reader, false, 0);
return null; return null;
@@ -430,10 +438,12 @@ To fix this error either change the JSON to a {1} or change the deserialized typ
if (reader.TokenType == JsonToken.PropertyName) if (reader.TokenType == JsonToken.PropertyName)
throw JsonSerializationException.Create(reader, "Additional content found in JSON reference object. A JSON reference object should only have a {0} property.".FormatWith(CultureInfo.InvariantCulture, JsonTypeReflector.RefPropertyName)); throw JsonSerializationException.Create(reader, "Additional content found in JSON reference object. A JSON reference object should only have a {0} property.".FormatWith(CultureInfo.InvariantCulture, JsonTypeReflector.RefPropertyName));
{ newValue = Serializer.ReferenceResolver.ResolveReference(this, reference);
newValue = Serializer.ReferenceResolver.ResolveReference(this, reference);
return true; if (TraceWriter != null && TraceWriter.LevelFilter >= TraceLevel.Info)
} TraceWriter.Trace(TraceLevel.Info, JsonPosition.FormatMessage(reader as IJsonLineInfo, reader.Path, "Resolved object reference '{0}' to {1}.".FormatWith(CultureInfo.InvariantCulture, reference, newValue.GetType())), null);
return true;
} }
else else
{ {
@@ -470,6 +480,9 @@ To fix this error either change the JSON to a {1} or change the deserialized typ
if (specifiedType == null) if (specifiedType == null)
throw JsonSerializationException.Create(reader, "Type specified in JSON '{0}' was not resolved.".FormatWith(CultureInfo.InvariantCulture, qualifiedTypeName)); throw JsonSerializationException.Create(reader, "Type specified in JSON '{0}' was not resolved.".FormatWith(CultureInfo.InvariantCulture, qualifiedTypeName));
if (TraceWriter != null && TraceWriter.LevelFilter >= TraceLevel.Verbose)
TraceWriter.Trace(TraceLevel.Verbose, JsonPosition.FormatMessage(reader as IJsonLineInfo, reader.Path, "Resolved type '{0}' to {1}.".FormatWith(CultureInfo.InvariantCulture, qualifiedTypeName, specifiedType)), null);
if (objectType != null if (objectType != null
#if !(NET35 || NET20 || WINDOWS_PHONE || PORTABLE) #if !(NET35 || NET20 || WINDOWS_PHONE || PORTABLE)
&& objectType != typeof (IDynamicMetaObjectProvider) && objectType != typeof (IDynamicMetaObjectProvider)
@@ -672,7 +685,7 @@ To fix this error either change the JSON to a {1} or change the deserialized typ
if (!gottenCurrentValue && target != null && property.Readable) if (!gottenCurrentValue && target != null && property.Readable)
currentValue = property.ValueProvider.GetValue(target); currentValue = property.ValueProvider.GetValue(target);
value = propertyConverter.ReadJson(reader, property.PropertyType, currentValue, GetInternalSerializer()); value = DeserializeConvertable(propertyConverter, reader, property.PropertyType, currentValue);
} }
else else
{ {
@@ -765,6 +778,9 @@ To fix this error either change the JSON to a {1} or change the deserialized typ
{ {
try try
{ {
if (TraceWriter != null && TraceWriter.LevelFilter >= TraceLevel.Verbose)
TraceWriter.Trace(TraceLevel.Verbose, JsonPosition.FormatMessage(reader as IJsonLineInfo, reader.Path, "Read object reference Id '{0}' for {1}.".FormatWith(CultureInfo.InvariantCulture, id, value.GetType())), null);
Serializer.ReferenceResolver.AddReference(this, id, value); Serializer.ReferenceResolver.AddReference(this, id, value);
} }
catch (Exception ex) catch (Exception ex)
@@ -806,6 +822,22 @@ To fix this error either change the JSON to a {1} or change the deserialized typ
return dictionary; return dictionary;
} }
private void OnDeserializing(JsonReader reader, JsonContract contract, object value)
{
if (TraceWriter != null && TraceWriter.LevelFilter >= TraceLevel.Info)
TraceWriter.Trace(TraceLevel.Info, JsonPosition.FormatMessage(reader as IJsonLineInfo, reader.Path, "Started deserializing {0}".FormatWith(CultureInfo.InvariantCulture, contract.UnderlyingType)), null);
contract.InvokeOnDeserializing(value, Serializer.Context);
}
private void OnDeserialized(JsonReader reader, JsonContract contract, object value)
{
if (TraceWriter != null && TraceWriter.LevelFilter >= TraceLevel.Info)
TraceWriter.Trace(TraceLevel.Info, JsonPosition.FormatMessage(reader as IJsonLineInfo, reader.Path, "Finished deserializing {0}".FormatWith(CultureInfo.InvariantCulture, contract.UnderlyingType)), null);
contract.InvokeOnDeserialized(value, Serializer.Context);
}
private object PopulateDictionary(IWrappedDictionary wrappedDictionary, JsonReader reader, JsonDictionaryContract contract, JsonProperty containerProperty, string id) private object PopulateDictionary(IWrappedDictionary wrappedDictionary, JsonReader reader, JsonDictionaryContract contract, JsonProperty containerProperty, string id)
{ {
object dictionary = wrappedDictionary.UnderlyingDictionary; object dictionary = wrappedDictionary.UnderlyingDictionary;
@@ -813,7 +845,7 @@ To fix this error either change the JSON to a {1} or change the deserialized typ
if (id != null) if (id != null)
AddReference(reader, id, dictionary); AddReference(reader, id, dictionary);
contract.InvokeOnDeserializing(dictionary, Serializer.Context); OnDeserializing(reader, contract, dictionary);
int initialDepth = reader.Depth; int initialDepth = reader.Depth;
@@ -848,7 +880,7 @@ To fix this error either change the JSON to a {1} or change the deserialized typ
object itemValue; object itemValue;
if (dictionaryValueConverter != null && dictionaryValueConverter.CanRead) if (dictionaryValueConverter != null && dictionaryValueConverter.CanRead)
itemValue = dictionaryValueConverter.ReadJson(reader, contract.DictionaryValueType, null, GetInternalSerializer()); itemValue = DeserializeConvertable(dictionaryValueConverter, reader, contract.DictionaryValueType, null);
else else
itemValue = CreateValueInternal(reader, contract.DictionaryValueType, contract.ItemContract, null, contract, containerProperty, null); itemValue = CreateValueInternal(reader, contract.DictionaryValueType, contract.ItemContract, null, contract, containerProperty, null);
@@ -856,7 +888,7 @@ To fix this error either change the JSON to a {1} or change the deserialized typ
} }
catch (Exception ex) catch (Exception ex)
{ {
if (IsErrorHandled(dictionary, contract, keyValue, reader.Path, ex)) if (IsErrorHandled(dictionary, contract, keyValue, reader as IJsonLineInfo, reader.Path, ex))
HandleError(reader, true, initialDepth); HandleError(reader, true, initialDepth);
else else
throw; throw;
@@ -875,7 +907,7 @@ To fix this error either change the JSON to a {1} or change the deserialized typ
if (!finished) if (!finished)
ThrowUnexpectedEndException(reader, contract, dictionary, "Unexpected end when deserializing object."); ThrowUnexpectedEndException(reader, contract, dictionary, "Unexpected end when deserializing object.");
contract.InvokeOnDeserialized(dictionary, Serializer.Context); OnDeserialized(reader, contract, dictionary);
return dictionary; return dictionary;
} }
@@ -886,7 +918,7 @@ To fix this error either change the JSON to a {1} or change the deserialized typ
if (id != null) if (id != null)
AddReference(reader, id, list); AddReference(reader, id, list);
contract.InvokeOnDeserializing(list, Serializer.Context); OnDeserializing(reader, contract, list);
JsonContract collectionItemContract = GetContractSafe(contract.CollectionItemType); JsonContract collectionItemContract = GetContractSafe(contract.CollectionItemType);
JsonConverter collectionItemConverter = GetConverter(collectionItemContract, null, contract, containerProperty); JsonConverter collectionItemConverter = GetConverter(collectionItemContract, null, contract, containerProperty);
@@ -920,7 +952,7 @@ To fix this error either change the JSON to a {1} or change the deserialized typ
object value; object value;
if (collectionItemConverter != null && collectionItemConverter.CanRead) if (collectionItemConverter != null && collectionItemConverter.CanRead)
value = collectionItemConverter.ReadJson(reader, contract.CollectionItemType, null, GetInternalSerializer()); value = DeserializeConvertable(collectionItemConverter, reader, contract.CollectionItemType, null);
else else
value = CreateValueInternal(reader, contract.CollectionItemType, collectionItemContract, null, contract, containerProperty, null); value = CreateValueInternal(reader, contract.CollectionItemType, collectionItemContract, null, contract, containerProperty, null);
@@ -937,7 +969,7 @@ To fix this error either change the JSON to a {1} or change the deserialized typ
{ {
JsonPosition errorPosition = reader.GetPosition(initialDepth); JsonPosition errorPosition = reader.GetPosition(initialDepth);
if (IsErrorHandled(list, contract, errorPosition.Position, reader.Path, ex)) if (IsErrorHandled(list, contract, errorPosition.Position, reader as IJsonLineInfo, reader.Path, ex))
{ {
HandleError(reader, true, initialDepth); HandleError(reader, true, initialDepth);
@@ -998,7 +1030,7 @@ To fix this error either change the JSON to a {1} or change the deserialized typ
if (!finished) if (!finished)
ThrowUnexpectedEndException(reader, contract, list, "Unexpected end when deserializing array."); ThrowUnexpectedEndException(reader, contract, list, "Unexpected end when deserializing array.");
contract.InvokeOnDeserialized(list, Serializer.Context); OnDeserialized(reader, contract, list);
return list; return list;
} }
@@ -1010,7 +1042,7 @@ To fix this error either change the JSON to a {1} or change the deserialized typ
} }
catch (Exception ex) catch (Exception ex)
{ {
if (IsErrorHandled(currentObject, contract, null, reader.Path, ex)) if (IsErrorHandled(currentObject, contract, null, reader as IJsonLineInfo, reader.Path, ex))
HandleError(reader, false, 0); HandleError(reader, false, 0);
else else
throw; throw;
@@ -1031,7 +1063,7 @@ To fix this error either change the JSON to a {1} or change the deserialized typ
return list; return list;
} }
contract.InvokeOnDeserializing(list, Serializer.Context); OnDeserializing(reader, contract, list);
int initialDepth = reader.Depth; int initialDepth = reader.Depth;
@@ -1058,7 +1090,7 @@ To fix this error either change the JSON to a {1} or change the deserialized typ
object value; object value;
if (collectionItemConverter != null && collectionItemConverter.CanRead) if (collectionItemConverter != null && collectionItemConverter.CanRead)
value = collectionItemConverter.ReadJson(reader, contract.CollectionItemType, null, GetInternalSerializer()); value = DeserializeConvertable(collectionItemConverter, reader, contract.CollectionItemType, null);
else else
value = CreateValueInternal(reader, contract.CollectionItemType, collectionItemContract, null, contract, containerProperty, null); value = CreateValueInternal(reader, contract.CollectionItemType, collectionItemContract, null, contract, containerProperty, null);
@@ -1075,7 +1107,7 @@ To fix this error either change the JSON to a {1} or change the deserialized typ
{ {
JsonPosition errorPosition = reader.GetPosition(initialDepth); JsonPosition errorPosition = reader.GetPosition(initialDepth);
if (IsErrorHandled(list, contract, errorPosition.Position, reader.Path, ex)) if (IsErrorHandled(list, contract, errorPosition.Position, reader as IJsonLineInfo, reader.Path, ex))
{ {
HandleError(reader, true, initialDepth); HandleError(reader, true, initialDepth);
@@ -1100,7 +1132,7 @@ To fix this error either change the JSON to a {1} or change the deserialized typ
if (!finished) if (!finished)
ThrowUnexpectedEndException(reader, contract, list, "Unexpected end when deserializing array."); ThrowUnexpectedEndException(reader, contract, list, "Unexpected end when deserializing array.");
contract.InvokeOnDeserialized(list, Serializer.Context); OnDeserialized(reader, contract, list);
return list; return list;
} }
@@ -1116,6 +1148,9 @@ To fix this error either change the environment to be fully trusted, change the
".FormatWith(CultureInfo.InvariantCulture, objectType)); ".FormatWith(CultureInfo.InvariantCulture, objectType));
} }
if (TraceWriter != null && TraceWriter.LevelFilter >= TraceLevel.Info)
TraceWriter.Trace(TraceLevel.Info, JsonPosition.FormatMessage(reader as IJsonLineInfo, reader.Path, "Deserializing {0} using ISerializable constructor.".FormatWith(CultureInfo.InvariantCulture, contract.UnderlyingType)), null);
SerializationInfo serializationInfo = new SerializationInfo(contract.UnderlyingType, GetFormatterConverter()); SerializationInfo serializationInfo = new SerializationInfo(contract.UnderlyingType, GetFormatterConverter());
bool finished = false; bool finished = false;
@@ -1152,8 +1187,8 @@ To fix this error either change the environment to be fully trusted, change the
AddReference(reader, id, createdObject); AddReference(reader, id, createdObject);
// these are together because OnDeserializing takes an object but for an ISerializable the object is fully created in the constructor // these are together because OnDeserializing takes an object but for an ISerializable the object is fully created in the constructor
contract.InvokeOnDeserializing(createdObject, Serializer.Context); OnDeserializing(reader, contract, createdObject);
contract.InvokeOnDeserialized(createdObject, Serializer.Context); OnDeserialized(reader, contract, createdObject);
return createdObject; return createdObject;
} }
@@ -1176,7 +1211,7 @@ To fix this error either change the environment to be fully trusted, change the
if (id != null) if (id != null)
AddReference(reader, id, newObject); AddReference(reader, id, newObject);
contract.InvokeOnDeserializing(newObject, Serializer.Context); OnDeserializing(reader, contract, newObject);
int initialDepth = reader.Depth; int initialDepth = reader.Depth;
@@ -1214,7 +1249,7 @@ To fix this error either change the environment to be fully trusted, change the
object value; object value;
if (dynamicMemberConverter != null && dynamicMemberConverter.CanRead) if (dynamicMemberConverter != null && dynamicMemberConverter.CanRead)
value = dynamicMemberConverter.ReadJson(reader, t, null, GetInternalSerializer()); value = DeserializeConvertable(dynamicMemberConverter, reader, t, null);
else else
value = CreateValueInternal(reader, t, dynamicMemberContract, null, null, member, null); value = CreateValueInternal(reader, t, dynamicMemberContract, null, null, member, null);
@@ -1223,7 +1258,7 @@ To fix this error either change the environment to be fully trusted, change the
} }
catch (Exception ex) catch (Exception ex)
{ {
if (IsErrorHandled(newObject, contract, memberName, reader.Path, ex)) if (IsErrorHandled(newObject, contract, memberName, reader as IJsonLineInfo, reader.Path, ex))
HandleError(reader, true, initialDepth); HandleError(reader, true, initialDepth);
else else
throw; throw;
@@ -1240,7 +1275,7 @@ To fix this error either change the environment to be fully trusted, change the
if (!finished) if (!finished)
ThrowUnexpectedEndException(reader, contract, newObject, "Unexpected end when deserializing object."); ThrowUnexpectedEndException(reader, contract, newObject, "Unexpected end when deserializing object.");
contract.InvokeOnDeserialized(newObject, Serializer.Context); OnDeserialized(reader, contract, newObject);
return newObject; return newObject;
} }
@@ -1252,6 +1287,9 @@ To fix this error either change the environment to be fully trusted, change the
Type objectType = contract.UnderlyingType; Type objectType = contract.UnderlyingType;
if (TraceWriter != null && TraceWriter.LevelFilter >= TraceLevel.Info)
TraceWriter.Trace(TraceLevel.Info, JsonPosition.FormatMessage(reader as IJsonLineInfo, reader.Path, "Deserializing {0} using a non-default constructor '{1}'.".FormatWith(CultureInfo.InvariantCulture, contract.UnderlyingType, constructorInfo)), null);
IDictionary<JsonProperty, object> propertyValues = ResolvePropertyAndConstructorValues(contract, containerProperty, reader, objectType); IDictionary<JsonProperty, object> propertyValues = ResolvePropertyAndConstructorValues(contract, containerProperty, reader, objectType);
IDictionary<ParameterInfo, object> constructorParameters = constructorInfo.GetParameters().ToDictionary(p => p, p => (object) null); IDictionary<ParameterInfo, object> constructorParameters = constructorInfo.GetParameters().ToDictionary(p => p, p => (object) null);
@@ -1271,7 +1309,7 @@ To fix this error either change the environment to be fully trusted, change the
if (id != null) if (id != null)
AddReference(reader, id, createdObject); AddReference(reader, id, createdObject);
contract.InvokeOnDeserializing(createdObject, Serializer.Context); OnDeserializing(reader, contract, createdObject);
// go through unused values and set the newly created object's properties // go through unused values and set the newly created object's properties
foreach (KeyValuePair<JsonProperty, object> remainingPropertyValue in remainingPropertyValues) foreach (KeyValuePair<JsonProperty, object> remainingPropertyValue in remainingPropertyValues)
@@ -1323,10 +1361,23 @@ To fix this error either change the environment to be fully trusted, change the
} }
} }
contract.InvokeOnDeserialized(createdObject, Serializer.Context); OnDeserialized(reader, contract, createdObject);
return createdObject; return createdObject;
} }
private object DeserializeConvertable(JsonConverter converter, JsonReader reader, Type objectType, object existingValue)
{
if (TraceWriter != null && TraceWriter.LevelFilter >= TraceLevel.Info)
TraceWriter.Trace(TraceLevel.Info, JsonPosition.FormatMessage(reader as IJsonLineInfo, reader.Path, "Started deserializing {0} with converter {1}.".FormatWith(CultureInfo.InvariantCulture, objectType, converter.GetType())), null);
object value = converter.ReadJson(reader, objectType, existingValue, GetInternalSerializer());
if (TraceWriter != null && TraceWriter.LevelFilter >= TraceLevel.Info)
TraceWriter.Trace(TraceLevel.Info, JsonPosition.FormatMessage(reader as IJsonLineInfo, reader.Path, "Finished deserializing {0} with converter {1}.".FormatWith(CultureInfo.InvariantCulture, objectType, converter.GetType())), null);
return value;
}
private IDictionary<JsonProperty, object> ResolvePropertyAndConstructorValues(JsonObjectContract contract, JsonProperty containerProperty, JsonReader reader, Type objectType) private IDictionary<JsonProperty, object> ResolvePropertyAndConstructorValues(JsonObjectContract contract, JsonProperty containerProperty, JsonReader reader, Type objectType)
{ {
IDictionary<JsonProperty, object> propertyValues = new Dictionary<JsonProperty, object>(); IDictionary<JsonProperty, object> propertyValues = new Dictionary<JsonProperty, object>();
@@ -1360,7 +1411,7 @@ To fix this error either change the environment to be fully trusted, change the
object propertyValue; object propertyValue;
if (propertyConverter != null && propertyConverter.CanRead) if (propertyConverter != null && propertyConverter.CanRead)
propertyValue = propertyConverter.ReadJson(reader, property.PropertyType, null, GetInternalSerializer()); propertyValue = DeserializeConvertable(propertyConverter, reader, property.PropertyType, null);
else else
propertyValue = CreateValueInternal(reader, property.PropertyType, property.PropertyContract, property, contract, containerProperty, null); propertyValue = CreateValueInternal(reader, property.PropertyType, property.PropertyContract, property, contract, containerProperty, null);
@@ -1376,6 +1427,9 @@ To fix this error either change the environment to be fully trusted, change the
if (!reader.Read()) if (!reader.Read())
throw JsonSerializationException.Create(reader, "Unexpected end when setting {0}'s value.".FormatWith(CultureInfo.InvariantCulture, memberName)); throw JsonSerializationException.Create(reader, "Unexpected end when setting {0}'s value.".FormatWith(CultureInfo.InvariantCulture, memberName));
if (TraceWriter != null && TraceWriter.LevelFilter >= TraceLevel.Verbose)
TraceWriter.Trace(TraceLevel.Verbose, JsonPosition.FormatMessage(reader as IJsonLineInfo, reader.Path, "Could not find member '{0}' on {1}.".FormatWith(CultureInfo.InvariantCulture, memberName, contract.UnderlyingType)), null);
if (Serializer.MissingMemberHandling == MissingMemberHandling.Error) if (Serializer.MissingMemberHandling == MissingMemberHandling.Error)
throw JsonSerializationException.Create(reader, "Could not find member '{0}' on object of type '{1}'".FormatWith(CultureInfo.InvariantCulture, memberName, objectType.Name)); throw JsonSerializationException.Create(reader, "Could not find member '{0}' on object of type '{1}'".FormatWith(CultureInfo.InvariantCulture, memberName, objectType.Name));
@@ -1482,7 +1536,7 @@ To fix this error either change the environment to be fully trusted, change the
private object PopulateObject(object newObject, JsonReader reader, JsonObjectContract contract, JsonProperty member, string id) private object PopulateObject(object newObject, JsonReader reader, JsonObjectContract contract, JsonProperty member, string id)
{ {
contract.InvokeOnDeserializing(newObject, Serializer.Context); OnDeserializing(reader, contract, newObject);
// only need to keep a track of properies presence if they are required or a value should be defaulted if missing // only need to keep a track of properies presence if they are required or a value should be defaulted if missing
Dictionary<JsonProperty, PropertyPresence> propertiesPresence = (contract.HasRequiredOrDefaultValueProperties || HasFlag(Serializer.DefaultValueHandling, DefaultValueHandling.Populate)) Dictionary<JsonProperty, PropertyPresence> propertiesPresence = (contract.HasRequiredOrDefaultValueProperties || HasFlag(Serializer.DefaultValueHandling, DefaultValueHandling.Populate))
@@ -1511,6 +1565,9 @@ To fix this error either change the environment to be fully trusted, change the
if (property == null) if (property == null)
{ {
if (TraceWriter != null && TraceWriter.LevelFilter >= TraceLevel.Verbose)
TraceWriter.Trace(TraceLevel.Verbose, JsonPosition.FormatMessage(reader as IJsonLineInfo, reader.Path, "Could not find member '{0}' on {1}".FormatWith(CultureInfo.InvariantCulture, memberName, contract.UnderlyingType)), null);
if (Serializer.MissingMemberHandling == MissingMemberHandling.Error) if (Serializer.MissingMemberHandling == MissingMemberHandling.Error)
throw JsonSerializationException.Create(reader, "Could not find member '{0}' on object of type '{1}'".FormatWith(CultureInfo.InvariantCulture, memberName, contract.UnderlyingType.Name)); throw JsonSerializationException.Create(reader, "Could not find member '{0}' on object of type '{1}'".FormatWith(CultureInfo.InvariantCulture, memberName, contract.UnderlyingType.Name));
@@ -1532,7 +1589,7 @@ To fix this error either change the environment to be fully trusted, change the
} }
catch (Exception ex) catch (Exception ex)
{ {
if (IsErrorHandled(newObject, contract, memberName, reader.Path, ex)) if (IsErrorHandled(newObject, contract, memberName, reader as IJsonLineInfo, reader.Path, ex))
HandleError(reader, true, initialDepth); HandleError(reader, true, initialDepth);
else else
throw; throw;
@@ -1555,7 +1612,7 @@ To fix this error either change the environment to be fully trusted, change the
EndObject(newObject, reader, contract, initialDepth, propertiesPresence); EndObject(newObject, reader, contract, initialDepth, propertiesPresence);
contract.InvokeOnDeserialized(newObject, Serializer.Context); OnDeserialized(reader, contract, newObject);
return newObject; return newObject;
} }
@@ -1594,7 +1651,7 @@ To fix this error either change the environment to be fully trusted, change the
} }
catch (Exception ex) catch (Exception ex)
{ {
if (IsErrorHandled(newObject, contract, property.PropertyName, reader.Path, ex)) if (IsErrorHandled(newObject, contract, property.PropertyName, reader as IJsonLineInfo, reader.Path, ex))
HandleError(reader, true, initialDepth); HandleError(reader, true, initialDepth);
else else
throw; throw;
@@ -1629,12 +1686,5 @@ To fix this error either change the environment to be fully trusted, change the
} }
} }
} }
internal enum PropertyPresence
{
None,
Null,
Value
}
} }
} }
@@ -30,6 +30,7 @@ using System.ComponentModel;
#if !(NET35 || NET20 || WINDOWS_PHONE || PORTABLE) #if !(NET35 || NET20 || WINDOWS_PHONE || PORTABLE)
using System.Dynamic; using System.Dynamic;
#endif #endif
using System.Diagnostics;
using System.Globalization; using System.Globalization;
using System.Security; using System.Security;
using Newtonsoft.Json.Linq; using Newtonsoft.Json.Linq;
@@ -229,21 +230,25 @@ namespace Newtonsoft.Json.Serialization
if (_serializeStack.IndexOf(value) != -1) if (_serializeStack.IndexOf(value) != -1)
{ {
string message = "Self referencing loop detected";
if (property != null)
message += " for property '{0}'".FormatWith(CultureInfo.InvariantCulture, property.PropertyName);
message += " with type '{0}'.".FormatWith(CultureInfo.InvariantCulture, value.GetType());
switch (referenceLoopHandling.GetValueOrDefault(Serializer.ReferenceLoopHandling)) switch (referenceLoopHandling.GetValueOrDefault(Serializer.ReferenceLoopHandling))
{ {
case ReferenceLoopHandling.Error: case ReferenceLoopHandling.Error:
string message = "Self referencing loop detected";
if (property != null)
message += " for property '{0}'".FormatWith(CultureInfo.InvariantCulture, property.PropertyName);
message += " with type '{0}'.".FormatWith(CultureInfo.InvariantCulture, value.GetType());
throw JsonSerializationException.Create(null, writer.ContainerPath, message, null); throw JsonSerializationException.Create(null, writer.ContainerPath, message, null);
case ReferenceLoopHandling.Ignore: case ReferenceLoopHandling.Ignore:
if (TraceWriter != null && TraceWriter.LevelFilter >= TraceLevel.Verbose)
TraceWriter.Trace(TraceLevel.Verbose, JsonPosition.FormatMessage(null, writer.Path, message + ". Skipping serializing self referenced value."), null);
return false; return false;
case ReferenceLoopHandling.Serialize: case ReferenceLoopHandling.Serialize:
if (TraceWriter != null && TraceWriter.LevelFilter >= TraceLevel.Verbose)
TraceWriter.Trace(TraceLevel.Verbose, JsonPosition.FormatMessage(null, writer.Path, message + ". Serializing self referenced value."), null);
return true; return true;
default:
throw new InvalidOperationException("Unexpected ReferenceLoopHandling value: '{0}'".FormatWith(CultureInfo.InvariantCulture, Serializer.ReferenceLoopHandling));
} }
} }
@@ -252,9 +257,14 @@ namespace Newtonsoft.Json.Serialization
private void WriteReference(JsonWriter writer, object value) private void WriteReference(JsonWriter writer, object value)
{ {
string reference = GetReference(writer, value);
if (TraceWriter != null && TraceWriter.LevelFilter >= TraceLevel.Info)
TraceWriter.Trace(TraceLevel.Info, JsonPosition.FormatMessage(null, writer.Path, "Writing object reference to Id '{0}' for {1}.".FormatWith(CultureInfo.InvariantCulture, reference, value.GetType())), null);
writer.WriteStartObject(); writer.WriteStartObject();
writer.WritePropertyName(JsonTypeReflector.RefPropertyName); writer.WritePropertyName(JsonTypeReflector.RefPropertyName);
writer.WriteValue(GetReference(writer, value)); writer.WriteValue(reference);
writer.WriteEndObject(); writer.WriteEndObject();
} }
@@ -263,6 +273,7 @@ namespace Newtonsoft.Json.Serialization
try try
{ {
string reference = Serializer.ReferenceResolver.GetReference(this, value); string reference = Serializer.ReferenceResolver.GetReference(this, value);
return reference; return reference;
} }
catch (Exception ex) catch (Exception ex)
@@ -290,6 +301,7 @@ namespace Newtonsoft.Json.Serialization
#else #else
s = converter.ConvertToString(value); s = converter.ConvertToString(value);
#endif #endif
return true; return true;
} }
} }
@@ -315,18 +327,34 @@ namespace Newtonsoft.Json.Serialization
private void SerializeString(JsonWriter writer, object value, JsonStringContract contract) private void SerializeString(JsonWriter writer, object value, JsonStringContract contract)
{ {
contract.InvokeOnSerializing(value, Serializer.Context); OnSerializing(writer, contract, value);
string s; string s;
TryConvertToString(value, contract.UnderlyingType, out s); TryConvertToString(value, contract.UnderlyingType, out s);
writer.WriteValue(s); writer.WriteValue(s);
OnSerialized(writer, contract, value);
}
private void OnSerializing(JsonWriter writer, JsonContract contract, object value)
{
if (TraceWriter != null && TraceWriter.LevelFilter >= TraceLevel.Info)
TraceWriter.Trace(TraceLevel.Info, JsonPosition.FormatMessage(null, writer.Path, "Started serializing {0}".FormatWith(CultureInfo.InvariantCulture, contract.UnderlyingType)), null);
contract.InvokeOnSerializing(value, Serializer.Context);
}
private void OnSerialized(JsonWriter writer, JsonContract contract, object value)
{
if (TraceWriter != null && TraceWriter.LevelFilter >= TraceLevel.Info)
TraceWriter.Trace(TraceLevel.Info, JsonPosition.FormatMessage(null, writer.Path, "Finished serializing {0}".FormatWith(CultureInfo.InvariantCulture, contract.UnderlyingType)), null);
contract.InvokeOnSerialized(value, Serializer.Context); contract.InvokeOnSerialized(value, Serializer.Context);
} }
private void SerializeObject(JsonWriter writer, object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty) private void SerializeObject(JsonWriter writer, object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
{ {
contract.InvokeOnSerializing(value, Serializer.Context); OnSerializing(writer, contract, value);
_serializeStack.Add(value); _serializeStack.Add(value);
@@ -349,7 +377,7 @@ namespace Newtonsoft.Json.Serialization
} }
catch (Exception ex) catch (Exception ex)
{ {
if (IsErrorHandled(value, contract, property.PropertyName, writer.ContainerPath, ex)) if (IsErrorHandled(value, contract, property.PropertyName, null, writer.ContainerPath, ex))
HandleError(writer, initialDepth); HandleError(writer, initialDepth);
else else
throw; throw;
@@ -360,7 +388,7 @@ namespace Newtonsoft.Json.Serialization
_serializeStack.RemoveAt(_serializeStack.Count - 1); _serializeStack.RemoveAt(_serializeStack.Count - 1);
contract.InvokeOnSerialized(value, Serializer.Context); OnSerialized(writer, contract, value);
} }
private bool CalculatePropertyValues(JsonWriter writer, object value, JsonContainerContract contract, JsonProperty member, JsonProperty property, out JsonContract memberContract, out object memberValue) private bool CalculatePropertyValues(JsonWriter writer, object value, JsonContainerContract contract, JsonProperty member, JsonProperty property, out JsonContract memberContract, out object memberValue)
@@ -409,8 +437,7 @@ namespace Newtonsoft.Json.Serialization
bool isReference = ResolveIsReference(contract, member, collectionContract, containerProperty) ?? HasFlag(Serializer.PreserveReferencesHandling, PreserveReferencesHandling.Objects); bool isReference = ResolveIsReference(contract, member, collectionContract, containerProperty) ?? HasFlag(Serializer.PreserveReferencesHandling, PreserveReferencesHandling.Objects);
if (isReference) if (isReference)
{ {
writer.WritePropertyName(JsonTypeReflector.IdPropertyName); WriteReferenceIdProperty(writer, contract.UnderlyingType, value);
writer.WriteValue(GetReference(writer, value));
} }
if (ShouldWriteType(TypeNameHandling.Objects, contract, member, collectionContract, containerProperty)) if (ShouldWriteType(TypeNameHandling.Objects, contract, member, collectionContract, containerProperty))
{ {
@@ -418,10 +445,26 @@ namespace Newtonsoft.Json.Serialization
} }
} }
private void WriteReferenceIdProperty(JsonWriter writer, Type type, object value)
{
string reference = GetReference(writer, value);
if (TraceWriter != null && TraceWriter.LevelFilter >= TraceLevel.Verbose)
TraceWriter.Trace(TraceLevel.Verbose, JsonPosition.FormatMessage(null, writer.Path, "Writing object reference Id '{0}' for {1}.".FormatWith(CultureInfo.InvariantCulture, reference, type)), null);
writer.WritePropertyName(JsonTypeReflector.IdPropertyName);
writer.WriteValue(reference);
}
private void WriteTypeProperty(JsonWriter writer, Type type) private void WriteTypeProperty(JsonWriter writer, Type type)
{ {
string typeName = ReflectionUtils.GetTypeName(type, Serializer.TypeNameAssemblyFormat, Serializer.Binder);
if (TraceWriter != null && TraceWriter.LevelFilter >= TraceLevel.Verbose)
TraceWriter.Trace(TraceLevel.Verbose, JsonPosition.FormatMessage(null, writer.Path, "Writing type name '{0}' for {1}.".FormatWith(CultureInfo.InvariantCulture, typeName, type)), null);
writer.WritePropertyName(JsonTypeReflector.TypePropertyName); writer.WritePropertyName(JsonTypeReflector.TypePropertyName);
writer.WriteValue(ReflectionUtils.GetTypeName(type, Serializer.TypeNameAssemblyFormat, Serializer.Binder)); writer.WriteValue(typeName);
} }
private bool HasFlag(DefaultValueHandling value, DefaultValueHandling flag) private bool HasFlag(DefaultValueHandling value, DefaultValueHandling flag)
@@ -452,15 +495,21 @@ namespace Newtonsoft.Json.Serialization
_serializeStack.Add(value); _serializeStack.Add(value);
if (TraceWriter != null && TraceWriter.LevelFilter >= TraceLevel.Info)
TraceWriter.Trace(TraceLevel.Info, JsonPosition.FormatMessage(null, writer.Path, "Started serializing {0} with converter {1}.".FormatWith(CultureInfo.InvariantCulture, value.GetType(), converter.GetType())), null);
converter.WriteJson(writer, value, GetInternalSerializer()); converter.WriteJson(writer, value, GetInternalSerializer());
if (TraceWriter != null && TraceWriter.LevelFilter >= TraceLevel.Info)
TraceWriter.Trace(TraceLevel.Info, JsonPosition.FormatMessage(null, writer.Path, "Finished serializing {0} with converter {1}.".FormatWith(CultureInfo.InvariantCulture, value.GetType(), converter.GetType())), null);
_serializeStack.RemoveAt(_serializeStack.Count - 1); _serializeStack.RemoveAt(_serializeStack.Count - 1);
} }
} }
private void SerializeList(JsonWriter writer, IWrappedCollection values, JsonArrayContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty) private void SerializeList(JsonWriter writer, IWrappedCollection values, JsonArrayContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
{ {
contract.InvokeOnSerializing(values.UnderlyingCollection, Serializer.Context); OnSerializing(writer, contract, values.UnderlyingCollection);
_serializeStack.Add(values.UnderlyingCollection); _serializeStack.Add(values.UnderlyingCollection);
@@ -492,7 +541,7 @@ namespace Newtonsoft.Json.Serialization
} }
catch (Exception ex) catch (Exception ex)
{ {
if (IsErrorHandled(values.UnderlyingCollection, contract, index, writer.ContainerPath, ex)) if (IsErrorHandled(values.UnderlyingCollection, contract, index, null, writer.ContainerPath, ex))
HandleError(writer, initialDepth); HandleError(writer, initialDepth);
else else
throw; throw;
@@ -510,12 +559,12 @@ namespace Newtonsoft.Json.Serialization
_serializeStack.RemoveAt(_serializeStack.Count - 1); _serializeStack.RemoveAt(_serializeStack.Count - 1);
contract.InvokeOnSerialized(values.UnderlyingCollection, Serializer.Context); OnSerialized(writer, contract, values.UnderlyingCollection);
} }
private void SerializeMultidimensionalArray(JsonWriter writer, Array values, JsonArrayContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty) private void SerializeMultidimensionalArray(JsonWriter writer, Array values, JsonArrayContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
{ {
contract.InvokeOnSerializing(values, Serializer.Context); OnSerializing(writer, contract, values);
_serializeStack.Add(values); _serializeStack.Add(values);
@@ -528,7 +577,7 @@ namespace Newtonsoft.Json.Serialization
_serializeStack.RemoveAt(_serializeStack.Count - 1); _serializeStack.RemoveAt(_serializeStack.Count - 1);
contract.InvokeOnSerialized(values, Serializer.Context); OnSerialized(writer, contract, values);
} }
private void SerializeMultidimensionalArray(JsonWriter writer, Array values, JsonArrayContract contract, JsonProperty member, int initialDepth, int[] indices) private void SerializeMultidimensionalArray(JsonWriter writer, Array values, JsonArrayContract contract, JsonProperty member, int initialDepth, int[] indices)
@@ -569,7 +618,7 @@ namespace Newtonsoft.Json.Serialization
} }
catch (Exception ex) catch (Exception ex)
{ {
if (IsErrorHandled(values, contract, i, writer.ContainerPath, ex)) if (IsErrorHandled(values, contract, i, null, writer.ContainerPath, ex))
HandleError(writer, initialDepth + 1); HandleError(writer, initialDepth + 1);
else else
throw; throw;
@@ -596,8 +645,7 @@ namespace Newtonsoft.Json.Serialization
if (isReference) if (isReference)
{ {
writer.WritePropertyName(JsonTypeReflector.IdPropertyName); WriteReferenceIdProperty(writer, contract.UnderlyingType, values);
writer.WriteValue(GetReference(writer, values));
} }
if (includeTypeDetails) if (includeTypeDetails)
{ {
@@ -624,7 +672,7 @@ namespace Newtonsoft.Json.Serialization
To fix this error either change the environment to be fully trusted, change the application to not deserialize the type, add JsonObjectAttribute to the type or change the JsonSerializer setting ContractResolver to use a new DefaultContractResolver with IgnoreSerializableInterface set to true.".FormatWith(CultureInfo.InvariantCulture, value.GetType()), null); To fix this error either change the environment to be fully trusted, change the application to not deserialize the type, add JsonObjectAttribute to the type or change the JsonSerializer setting ContractResolver to use a new DefaultContractResolver with IgnoreSerializableInterface set to true.".FormatWith(CultureInfo.InvariantCulture, value.GetType()), null);
} }
contract.InvokeOnSerializing(value, Serializer.Context); OnSerializing(writer, contract, value);
_serializeStack.Add(value); _serializeStack.Add(value);
WriteObjectStart(writer, value, contract, member, collectionContract, containerProperty); WriteObjectStart(writer, value, contract, member, collectionContract, containerProperty);
@@ -641,14 +689,14 @@ To fix this error either change the environment to be fully trusted, change the
writer.WriteEndObject(); writer.WriteEndObject();
_serializeStack.RemoveAt(_serializeStack.Count - 1); _serializeStack.RemoveAt(_serializeStack.Count - 1);
contract.InvokeOnSerialized(value, Serializer.Context); OnSerialized(writer, contract, value);
} }
#endif #endif
#if !(NET35 || NET20 || WINDOWS_PHONE || PORTABLE) #if !(NET35 || NET20 || WINDOWS_PHONE || PORTABLE)
private void SerializeDynamic(JsonWriter writer, IDynamicMetaObjectProvider value, JsonDynamicContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty) private void SerializeDynamic(JsonWriter writer, IDynamicMetaObjectProvider value, JsonDynamicContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
{ {
contract.InvokeOnSerializing(value, Serializer.Context); OnSerializing(writer, contract, value);
_serializeStack.Add(value); _serializeStack.Add(value);
WriteObjectStart(writer, value, contract, member, collectionContract, containerProperty); WriteObjectStart(writer, value, contract, member, collectionContract, containerProperty);
@@ -673,7 +721,7 @@ To fix this error either change the environment to be fully trusted, change the
} }
catch (Exception ex) catch (Exception ex)
{ {
if (IsErrorHandled(value, contract, property.PropertyName, writer.ContainerPath, ex)) if (IsErrorHandled(value, contract, property.PropertyName, null, writer.ContainerPath, ex))
HandleError(writer, initialDepth); HandleError(writer, initialDepth);
else else
throw; throw;
@@ -697,7 +745,7 @@ To fix this error either change the environment to be fully trusted, change the
} }
catch (Exception ex) catch (Exception ex)
{ {
if (IsErrorHandled(value, contract, memberName, writer.ContainerPath, ex)) if (IsErrorHandled(value, contract, memberName, null, writer.ContainerPath, ex))
HandleError(writer, initialDepth); HandleError(writer, initialDepth);
else else
throw; throw;
@@ -708,7 +756,7 @@ To fix this error either change the environment to be fully trusted, change the
writer.WriteEndObject(); writer.WriteEndObject();
_serializeStack.RemoveAt(_serializeStack.Count - 1); _serializeStack.RemoveAt(_serializeStack.Count - 1);
contract.InvokeOnSerialized(value, Serializer.Context); OnSerialized(writer, contract, value);
} }
#endif #endif
@@ -743,8 +791,7 @@ To fix this error either change the environment to be fully trusted, change the
private void SerializeDictionary(JsonWriter writer, IWrappedDictionary values, JsonDictionaryContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty) private void SerializeDictionary(JsonWriter writer, IWrappedDictionary values, JsonDictionaryContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
{ {
contract.InvokeOnSerializing(values.UnderlyingDictionary, Serializer.Context); OnSerializing(writer, contract, values.UnderlyingDictionary);
_serializeStack.Add(values.UnderlyingDictionary); _serializeStack.Add(values.UnderlyingDictionary);
WriteObjectStart(writer, values.UnderlyingDictionary, contract, member, collectionContract, containerProperty); WriteObjectStart(writer, values.UnderlyingDictionary, contract, member, collectionContract, containerProperty);
@@ -787,7 +834,7 @@ To fix this error either change the environment to be fully trusted, change the
} }
catch (Exception ex) catch (Exception ex)
{ {
if (IsErrorHandled(values.UnderlyingDictionary, contract, propertyName, writer.ContainerPath, ex)) if (IsErrorHandled(values.UnderlyingDictionary, contract, propertyName, null, writer.ContainerPath, ex))
HandleError(writer, initialDepth); HandleError(writer, initialDepth);
else else
throw; throw;
@@ -798,7 +845,7 @@ To fix this error either change the environment to be fully trusted, change the
_serializeStack.RemoveAt(_serializeStack.Count - 1); _serializeStack.RemoveAt(_serializeStack.Count - 1);
contract.InvokeOnSerialized(values.UnderlyingDictionary, Serializer.Context); OnSerialized(writer, contract, values.UnderlyingDictionary);
} }
private string GetPropertyName(DictionaryEntry entry) private string GetPropertyName(DictionaryEntry entry)
@@ -49,6 +49,12 @@ namespace Newtonsoft.Json.Serialization
set { _serializer.ReferenceResolver = value; } set { _serializer.ReferenceResolver = value; }
} }
public override ITraceWriter TraceWriter
{
get { return _serializer.TraceWriter; }
set { _serializer.TraceWriter = value; }
}
public override JsonConverterCollection Converters public override JsonConverterCollection Converters
{ {
get { return _serializer.Converters; } get { return _serializer.Converters; }
+33
View File
@@ -0,0 +1,33 @@
#if (NETFX_CORE || SILVERLIGHT || PORTABLE)
using Newtonsoft.Json.Serialization;
namespace Newtonsoft.Json
{
/// <summary>
/// Specifies what messages to output for the <see cref="ITraceWriter"/> class.
/// </summary>
public enum TraceLevel
{
/// <summary>
/// Output no tracing and debugging messages.
/// </summary>
Off,
/// <summary>
/// Output error-handling messages.
/// </summary>
Error,
/// <summary>
/// Output warnings and error-handling messages.
/// </summary>
Warning,
/// <summary>
/// Output informational messages, warnings, and error-handling messages.
/// </summary>
Info,
/// <summary>
/// Output all debugging and tracing messages.
/// </summary>
Verbose
}
}
#endif
+11 -2
View File
@@ -332,7 +332,7 @@ namespace Newtonsoft.Json.Utilities
if (targetType == typeof (Uri)) if (targetType == typeof (Uri))
return new Uri((string) initialValue, UriKind.RelativeOrAbsolute); return new Uri((string) initialValue, UriKind.RelativeOrAbsolute);
if (targetType == typeof (TimeSpan)) if (targetType == typeof (TimeSpan))
#if !(NET35 || NET20 || SILVERLIGHT || PORTABLE) #if !(NET35 || NET20 || PORTABLE)
return TimeSpan.Parse((string) initialValue, CultureInfo.InvariantCulture); return TimeSpan.Parse((string) initialValue, CultureInfo.InvariantCulture);
#else #else
return TimeSpan.Parse((string)initialValue); return TimeSpan.Parse((string)initialValue);
@@ -395,7 +395,16 @@ namespace Newtonsoft.Json.Utilities
/// </returns> /// </returns>
public static bool TryConvert(object initialValue, CultureInfo culture, Type targetType, out object convertedValue) public static bool TryConvert(object initialValue, CultureInfo culture, Type targetType, out object convertedValue)
{ {
return MiscellaneousUtils.TryAction<object>(delegate { return Convert(initialValue, culture, targetType); }, out convertedValue); try
{
convertedValue = Convert(initialValue, culture, targetType);
return true;
}
catch
{
convertedValue = null;
return false;
}
} }
#endregion #endregion
+2 -1
View File
@@ -31,6 +31,7 @@ using System;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.Globalization;
using Newtonsoft.Json.Serialization; using Newtonsoft.Json.Serialization;
namespace Newtonsoft.Json.Utilities.LinqBridge namespace Newtonsoft.Json.Utilities.LinqBridge
@@ -2966,7 +2967,7 @@ namespace Newtonsoft.Json.Utilities.LinqBridge
public override string ToString() public override string ToString()
{ {
return string.Format(@"{{ First = {0}, Second = {1} }}", First, Second); return string.Format(CultureInfo.InvariantCulture, @"{{ First = {0}, Second = {1} }}", First, Second);
} }
} }
} }
@@ -1049,8 +1049,10 @@ namespace Newtonsoft.Json.Utilities
if (type == typeof(Guid)) if (type == typeof(Guid))
return new Guid(); return new Guid();
#if !NET20
if (type == typeof(DateTimeOffset)) if (type == typeof(DateTimeOffset))
return new DateTimeOffset(); return new DateTimeOffset();
#endif
if (IsNullable(type)) if (IsNullable(type))
return null; return null;