-Added ReflectionDelegateFactory, DynamicReflectionDelegateFactory and LateBoundDelegateFactory to support switching between dynamic and late bound reflection for medium trust environments
-Added WriteObjectId to BsonWriter -Added BsonObjectId and BsonObjectIdConverter classes -Added KeyValuePairConverter -Added DynamicCodeGeneration flag to DefaultContractResolver -Changed JsonConverter.ReadJson to add an extra parameter: currentValue
This commit is contained in:
@@ -392,5 +392,56 @@ namespace Newtonsoft.Json.Tests.Bson
|
||||
Assert.AreEqual(9.95m, deserializedProduct.Price);
|
||||
Assert.AreEqual(3, deserializedProduct.Sizes.Length);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WriteOid()
|
||||
{
|
||||
MemoryStream ms = new MemoryStream();
|
||||
BsonWriter writer = new BsonWriter(ms);
|
||||
|
||||
byte[] oid = new byte[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
|
||||
|
||||
writer.WriteStartObject();
|
||||
writer.WritePropertyName("_oid");
|
||||
writer.WriteObjectId(oid);
|
||||
writer.WriteEndObject();
|
||||
|
||||
string bson = MiscellaneousUtils.BytesToHex(ms.ToArray());
|
||||
Assert.AreEqual("17-00-00-00-07-5F-6F-69-64-00-01-02-03-04-05-06-07-08-09-0A-0B-0C-00", bson);
|
||||
|
||||
ms.Seek(0, SeekOrigin.Begin);
|
||||
BsonReader reader = new BsonReader(ms);
|
||||
|
||||
Assert.IsTrue(reader.Read());
|
||||
Assert.AreEqual(JsonToken.StartObject, reader.TokenType);
|
||||
|
||||
Assert.IsTrue(reader.Read());
|
||||
Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
|
||||
|
||||
Assert.IsTrue(reader.Read());
|
||||
Assert.AreEqual(JsonToken.Bytes, reader.TokenType);
|
||||
Assert.AreEqual(oid, reader.Value);
|
||||
|
||||
Assert.IsTrue(reader.Read());
|
||||
Assert.AreEqual(JsonToken.EndObject, reader.TokenType);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WriteOidPlusContent()
|
||||
{
|
||||
MemoryStream ms = new MemoryStream();
|
||||
BsonWriter writer = new BsonWriter(ms);
|
||||
|
||||
writer.WriteStartObject();
|
||||
writer.WritePropertyName("_id");
|
||||
writer.WriteObjectId(MiscellaneousUtils.HexToBytes("4ABBED9D1D8B0F0218000001"));
|
||||
writer.WritePropertyName("test");
|
||||
writer.WriteValue("1234£56");
|
||||
writer.WriteEndObject();
|
||||
|
||||
byte[] expected = MiscellaneousUtils.HexToBytes("29000000075F6964004ABBED9D1D8B0F02180000010274657374000900000031323334C2A335360000");
|
||||
|
||||
Assert.AreEqual(expected, ms.ToArray());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -111,7 +111,7 @@ namespace Newtonsoft.Json.Tests.Converters
|
||||
writer.WriteValue(d.TableName);
|
||||
}
|
||||
|
||||
public override object ReadJson(JsonReader reader, Type objectType, JsonSerializer serializer)
|
||||
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
|
||||
{
|
||||
//reader.Read();
|
||||
DataTable d = new DataTable((string)reader.Value);
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Newtonsoft.Json.Bson;
|
||||
using Newtonsoft.Json.Tests.TestObjects;
|
||||
using Newtonsoft.Json.Utilities;
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace Newtonsoft.Json.Tests.Converters
|
||||
{
|
||||
public class ObjectIdConverterTests : TestFixtureBase
|
||||
{
|
||||
public class ObjectIdTestClass
|
||||
{
|
||||
[JsonProperty("_id")]
|
||||
public BsonObjectId Id { get; set; }
|
||||
[JsonProperty("test")]
|
||||
public string Test { get; set; }
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Serialize()
|
||||
{
|
||||
ObjectIdTestClass c = new ObjectIdTestClass
|
||||
{
|
||||
Id = new BsonObjectId(MiscellaneousUtils.HexToBytes("4ABBED9D1D8B0F0218000001")),
|
||||
Test = "1234£56"
|
||||
};
|
||||
|
||||
MemoryStream ms = new MemoryStream();
|
||||
JsonSerializer serializer = new JsonSerializer();
|
||||
|
||||
// serialize product to BSON
|
||||
BsonWriter writer = new BsonWriter(ms);
|
||||
serializer.Serialize(writer, c);
|
||||
|
||||
byte[] expected = MiscellaneousUtils.HexToBytes("29000000075F6964004ABBED9D1D8B0F02180000010274657374000900000031323334C2A335360000");
|
||||
|
||||
Assert.AreEqual(expected, ms.ToArray());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Deserialize()
|
||||
{
|
||||
byte[] bson = MiscellaneousUtils.HexToBytes("29000000075F6964004ABBED9D1D8B0F02180000010274657374000900000031323334C2A335360000");
|
||||
|
||||
JsonSerializer serializer = new JsonSerializer();
|
||||
|
||||
BsonReader reader = new BsonReader(new MemoryStream(bson));
|
||||
ObjectIdTestClass c = serializer.Deserialize<ObjectIdTestClass>(reader);
|
||||
|
||||
Assert.AreEqual(c.Id.Value, MiscellaneousUtils.HexToBytes("4ABBED9D1D8B0F0218000001"));
|
||||
Assert.AreEqual(c.Test, "1234£56");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -86,14 +86,14 @@ namespace Newtonsoft.Json.Tests.Converters
|
||||
if (deserializeRootElementName != null)
|
||||
converter.DeserializeRootElementName = deserializeRootElementName;
|
||||
|
||||
XmlNode node = (XmlNode)converter.ReadJson(reader, typeof (XmlDocument), new JsonSerializer());
|
||||
XmlNode node = (XmlNode)converter.ReadJson(reader, typeof (XmlDocument), null, new JsonSerializer());
|
||||
|
||||
#if !NET20
|
||||
string xmlText = node.OuterXml;
|
||||
|
||||
reader = new JsonTextReader(new StringReader(json));
|
||||
reader.Read();
|
||||
XDocument d = (XDocument) converter.ReadJson(reader, typeof (XDocument), new JsonSerializer());
|
||||
XDocument d = (XDocument) converter.ReadJson(reader, typeof (XDocument), null, new JsonSerializer());
|
||||
|
||||
string linqXmlText = d.ToString(SaveOptions.DisableFormatting);
|
||||
if (d.Declaration != null)
|
||||
|
||||
@@ -19,7 +19,7 @@ namespace Newtonsoft.Json.Tests.LinqToSql
|
||||
o.WriteTo(writer);
|
||||
}
|
||||
|
||||
public override object ReadJson(JsonReader reader, Type objectType, JsonSerializer serializer)
|
||||
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
|
||||
{
|
||||
JObject o = JObject.Load(reader);
|
||||
|
||||
|
||||
@@ -13,7 +13,7 @@ namespace Newtonsoft.Json.Tests.LinqToSql
|
||||
writer.WriteValue(Convert.ToBase64String(guid.ToByteArray()));
|
||||
}
|
||||
|
||||
public override object ReadJson(JsonReader reader, Type objectType, JsonSerializer serializer)
|
||||
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
|
||||
{
|
||||
string encodedData = (string) reader.Value;
|
||||
byte[] data = Convert.FromBase64String(encodedData);
|
||||
|
||||
@@ -198,7 +198,7 @@
|
||||
<Compile Include="TestObjects\UserNullable.cs" />
|
||||
<Compile Include="TestObjects\VersionKeyedCollection.cs" />
|
||||
<Compile Include="TestObjects\WagePerson.cs" />
|
||||
<Compile Include="Utilities\LateBoundDelegateFactoryTests.cs" />
|
||||
<Compile Include="Utilities\DynamicReflectionDelegateFactoryTests.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Newtonsoft.Json\Newtonsoft.Json.Compact.csproj">
|
||||
|
||||
@@ -204,7 +204,7 @@
|
||||
<Compile Include="TestObjects\UserNullable.cs" />
|
||||
<Compile Include="TestObjects\VersionKeyedCollection.cs" />
|
||||
<Compile Include="TestObjects\WagePerson.cs" />
|
||||
<Compile Include="Utilities\LateBoundDelegateFactoryTests.cs" />
|
||||
<Compile Include="Utilities\DynamicReflectionDelegateFactoryTests.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Service Include="{3259AA49-8AA1-44D3-9025-A0B520596A8C}" />
|
||||
|
||||
@@ -209,7 +209,7 @@
|
||||
<Compile Include="TestObjects\UserNullable.cs" />
|
||||
<Compile Include="TestObjects\VersionKeyedCollection.cs" />
|
||||
<Compile Include="TestObjects\WagePerson.cs" />
|
||||
<Compile Include="Utilities\LateBoundDelegateFactoryTests.cs" />
|
||||
<Compile Include="Utilities\DynamicReflectionDelegateFactoryTests.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Newtonsoft.Json\Newtonsoft.Json.Silverlight.csproj">
|
||||
|
||||
@@ -91,6 +91,7 @@
|
||||
<Compile Include="Converters\DataSetConverterTests.cs" />
|
||||
<Compile Include="Converters\CustomCreationConverterTests.cs" />
|
||||
<Compile Include="Converters\HtmlColorConverterTests.cs" />
|
||||
<Compile Include="Converters\ObjectIdConverterTests.cs" />
|
||||
<Compile Include="Converters\StringEnumConverterTests.cs" />
|
||||
<Compile Include="FileSystemEntityModel.Designer.cs">
|
||||
<AutoGen>True</AutoGen>
|
||||
@@ -244,7 +245,7 @@
|
||||
<Compile Include="TestObjects\TypeClass.cs" />
|
||||
<Compile Include="TestObjects\TypedSubHashtable.cs" />
|
||||
<Compile Include="TestObjects\UserNullable.cs" />
|
||||
<Compile Include="Utilities\LateBoundDelegateFactoryTests.cs" />
|
||||
<Compile Include="Utilities\DynamicReflectionDelegateFactoryTests.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Newtonsoft.Json\Newtonsoft.Json.csproj">
|
||||
|
||||
@@ -2983,7 +2983,7 @@ keyword such as type of business.""
|
||||
writer.WriteNull();
|
||||
}
|
||||
|
||||
public override object ReadJson(JsonReader reader, Type objectType, JsonSerializer serializer)
|
||||
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
@@ -3006,7 +3006,7 @@ keyword such as type of business.""
|
||||
writer.WriteNull();
|
||||
}
|
||||
|
||||
public override object ReadJson(JsonReader reader, Type objectType, JsonSerializer serializer)
|
||||
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
@@ -3056,5 +3056,158 @@ keyword such as type of business.""
|
||||
Assert.AreEqual(2, newValue.GenericList.Count);
|
||||
Assert.AreEqual(typeof(List<string>), newValue.GenericList.GetType());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeserializeSimpleKeyValuePair()
|
||||
{
|
||||
List<KeyValuePair<string, string>> list = new List<KeyValuePair<string, string>>();
|
||||
list.Add(new KeyValuePair<string, string>("key1", "value1"));
|
||||
list.Add(new KeyValuePair<string, string>("key2", "value2"));
|
||||
|
||||
string json = JsonConvert.SerializeObject(list);
|
||||
|
||||
Assert.AreEqual(@"[{""Key"":""key1"",""Value"":""value1""},{""Key"":""key2"",""Value"":""value2""}]", json);
|
||||
|
||||
List<KeyValuePair<string, string>> result = JsonConvert.DeserializeObject<List<KeyValuePair<string, string>>>(json);
|
||||
Assert.AreEqual(2, result.Count);
|
||||
Assert.AreEqual("key1", result[0].Key);
|
||||
Assert.AreEqual("value1", result[0].Value);
|
||||
Assert.AreEqual("key2", result[1].Key);
|
||||
Assert.AreEqual("value2", result[1].Value);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeserializeComplexKeyValuePair()
|
||||
{
|
||||
DateTime dateTime = new DateTime(2000, 12, 1, 23, 1, 1, DateTimeKind.Utc);
|
||||
|
||||
List<KeyValuePair<string, WagePerson>> list = new List<KeyValuePair<string, WagePerson>>();
|
||||
list.Add(new KeyValuePair<string, WagePerson>("key1", new WagePerson
|
||||
{
|
||||
BirthDate = dateTime,
|
||||
Department = "Department1",
|
||||
LastModified = dateTime,
|
||||
HourlyWage = 1
|
||||
}));
|
||||
list.Add(new KeyValuePair<string, WagePerson>("key2", new WagePerson
|
||||
{
|
||||
BirthDate = dateTime,
|
||||
Department = "Department2",
|
||||
LastModified = dateTime,
|
||||
HourlyWage = 2
|
||||
}));
|
||||
|
||||
string json = JsonConvert.SerializeObject(list, Formatting.Indented);
|
||||
|
||||
Assert.AreEqual(@"[
|
||||
{
|
||||
""Key"": ""key1"",
|
||||
""Value"": {
|
||||
""HourlyWage"": 1.0,
|
||||
""Name"": null,
|
||||
""BirthDate"": ""\/Date(975711661000)\/"",
|
||||
""LastModified"": ""\/Date(975711661000)\/""
|
||||
}
|
||||
},
|
||||
{
|
||||
""Key"": ""key2"",
|
||||
""Value"": {
|
||||
""HourlyWage"": 2.0,
|
||||
""Name"": null,
|
||||
""BirthDate"": ""\/Date(975711661000)\/"",
|
||||
""LastModified"": ""\/Date(975711661000)\/""
|
||||
}
|
||||
}
|
||||
]", json);
|
||||
|
||||
List<KeyValuePair<string, WagePerson>> result = JsonConvert.DeserializeObject<List<KeyValuePair<string, WagePerson>>>(json);
|
||||
Assert.AreEqual(2, result.Count);
|
||||
Assert.AreEqual("key1", result[0].Key);
|
||||
Assert.AreEqual(1, result[0].Value.HourlyWage);
|
||||
Assert.AreEqual("key2", result[1].Key);
|
||||
Assert.AreEqual(2, result[1].Value.HourlyWage);
|
||||
}
|
||||
|
||||
public class StringListAppenderConverter : JsonConverter
|
||||
{
|
||||
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
|
||||
{
|
||||
writer.WriteValue(value);
|
||||
}
|
||||
|
||||
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
|
||||
{
|
||||
List<string> existingStrings = (List<string>)existingValue;
|
||||
List<string> newStrings = new List<string>(existingStrings);
|
||||
|
||||
reader.Read();
|
||||
|
||||
while (reader.TokenType != JsonToken.EndArray)
|
||||
{
|
||||
string s = (string)reader.Value;
|
||||
newStrings.Add(s);
|
||||
|
||||
reader.Read();
|
||||
}
|
||||
|
||||
return newStrings;
|
||||
}
|
||||
|
||||
public override bool CanConvert(Type objectType)
|
||||
{
|
||||
return (objectType == typeof (List<string>));
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void StringListAppenderConverterTest()
|
||||
{
|
||||
Movie p = new Movie();
|
||||
p.ReleaseCountries = new List<string> { "Existing" };
|
||||
|
||||
JsonConvert.PopulateObject("{'ReleaseCountries':['Appended']}", p, new JsonSerializerSettings
|
||||
{
|
||||
Converters = new List<JsonConverter> { new StringListAppenderConverter() }
|
||||
});
|
||||
|
||||
Assert.AreEqual(2, p.ReleaseCountries.Count);
|
||||
Assert.AreEqual("Existing", p.ReleaseCountries[0]);
|
||||
Assert.AreEqual("Appended", p.ReleaseCountries[1]);
|
||||
}
|
||||
|
||||
public class StringAppenderConverter : JsonConverter
|
||||
{
|
||||
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
|
||||
{
|
||||
writer.WriteValue(value);
|
||||
}
|
||||
|
||||
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
|
||||
{
|
||||
string existingString = (string)existingValue;
|
||||
string newString = existingString + (string)reader.Value;
|
||||
|
||||
return newString;
|
||||
}
|
||||
|
||||
public override bool CanConvert(Type objectType)
|
||||
{
|
||||
return (objectType == typeof(string));
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void StringAppenderConverterTest()
|
||||
{
|
||||
Movie p = new Movie();
|
||||
p.Name = "Existing,";
|
||||
|
||||
JsonConvert.PopulateObject("{'Name':'Appended'}", p, new JsonSerializerSettings
|
||||
{
|
||||
Converters = new List<JsonConverter> { new StringAppenderConverter() }
|
||||
});
|
||||
|
||||
Assert.AreEqual(p.Name, "Existing,Appended");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -296,7 +296,7 @@ namespace Newtonsoft.Json.Tests.Serialization
|
||||
me.WriteTo(writer);
|
||||
}
|
||||
|
||||
public override object ReadJson(JsonReader reader, Type objectType, JsonSerializer serializer)
|
||||
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
|
||||
{
|
||||
JObject o = JObject.Load(reader);
|
||||
string id = (string)o["$id"];
|
||||
|
||||
@@ -43,7 +43,7 @@ namespace Newtonsoft.Json.Tests.TestObjects
|
||||
j.WriteTo(writer);
|
||||
}
|
||||
|
||||
public override object ReadJson(JsonReader reader, Type objectType, JsonSerializer serializer)
|
||||
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
|
||||
{
|
||||
JToken j = JArray.Load(reader);
|
||||
|
||||
|
||||
@@ -42,7 +42,7 @@ namespace Newtonsoft.Json.Tests.TestObjects
|
||||
writer.WriteEndArray();
|
||||
}
|
||||
|
||||
public override object ReadJson(JsonReader reader, Type objectType, JsonSerializer serializer)
|
||||
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
|
||||
{
|
||||
IList<T> list = new List<T>();
|
||||
|
||||
|
||||
+5
-5
@@ -34,7 +34,7 @@ using Newtonsoft.Json.Tests.TestObjects;
|
||||
|
||||
namespace Newtonsoft.Json.Tests.Utilities
|
||||
{
|
||||
public class LateBoundDelegateFactoryTests : TestFixtureBase
|
||||
public class DynamicReflectionDelegateFactoryTests : TestFixtureBase
|
||||
{
|
||||
[Test]
|
||||
[ExpectedException(typeof(InvalidCastException), ExpectedMessage = "Unable to cast object of type 'Newtonsoft.Json.Tests.TestObjects.Person' to type 'Newtonsoft.Json.Tests.TestObjects.Movie'.")]
|
||||
@@ -43,7 +43,7 @@ namespace Newtonsoft.Json.Tests.Utilities
|
||||
Person p = new Person();
|
||||
p.Name = "Hi";
|
||||
|
||||
Func<object, object> setter = LateBoundDelegateFactory.CreateGet<object>(typeof(Movie).GetProperty("Name"));
|
||||
Func<object, object> setter = DynamicReflectionDelegateFactory.Instance.CreateGet<object>(typeof(Movie).GetProperty("Name"));
|
||||
|
||||
setter(p);
|
||||
}
|
||||
@@ -55,7 +55,7 @@ namespace Newtonsoft.Json.Tests.Utilities
|
||||
Person p = new Person();
|
||||
Movie m = new Movie();
|
||||
|
||||
Action<object, object> setter = LateBoundDelegateFactory.CreateSet<object>(typeof(Movie).GetProperty("Name"));
|
||||
Action<object, object> setter = DynamicReflectionDelegateFactory.Instance.CreateSet<object>(typeof(Movie).GetProperty("Name"));
|
||||
|
||||
setter(m, "Hi");
|
||||
|
||||
@@ -70,7 +70,7 @@ namespace Newtonsoft.Json.Tests.Utilities
|
||||
{
|
||||
object structTest = new StructTest();
|
||||
|
||||
Action<object, object> setter = LateBoundDelegateFactory.CreateSet<object>(typeof(StructTest).GetProperty("StringProperty"));
|
||||
Action<object, object> setter = DynamicReflectionDelegateFactory.Instance.CreateSet<object>(typeof(StructTest).GetProperty("StringProperty"));
|
||||
|
||||
setter(structTest, "Hi");
|
||||
|
||||
@@ -85,7 +85,7 @@ namespace Newtonsoft.Json.Tests.Utilities
|
||||
{
|
||||
Movie m = new Movie();
|
||||
|
||||
Action<object, object> setter = LateBoundDelegateFactory.CreateSet<object>(typeof(Movie).GetProperty("Name"));
|
||||
Action<object, object> setter = DynamicReflectionDelegateFactory.Instance.CreateSet<object>(typeof(Movie).GetProperty("Name"));
|
||||
|
||||
setter(m, new Version());
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
#region License
|
||||
// Copyright (c) 2007 James Newton-King
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person
|
||||
// obtaining a copy of this software and associated documentation
|
||||
// files (the "Software"), to deal in the Software without
|
||||
// restriction, including without limitation the rights to use,
|
||||
// copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the
|
||||
// Software is furnished to do so, subject to the following
|
||||
// conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be
|
||||
// included in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
||||
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
||||
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||||
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||
// OTHER DEALINGS IN THE SOFTWARE.
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Newtonsoft.Json.Utilities;
|
||||
|
||||
namespace Newtonsoft.Json.Bson
|
||||
{
|
||||
public class BsonObjectId
|
||||
{
|
||||
public byte[] Value { get; private set; }
|
||||
|
||||
public BsonObjectId(byte[] value)
|
||||
{
|
||||
ValidationUtils.ArgumentNotNull(value, "value");
|
||||
if (value.Length != 12)
|
||||
throw new Exception("An ObjectId must be 12 bytes");
|
||||
|
||||
Value = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -110,7 +110,7 @@ namespace Newtonsoft.Json.Bson
|
||||
case JTokenType.Undefined:
|
||||
break;
|
||||
case JTokenType.Date:
|
||||
DateTime dateTime = (DateTime) t;
|
||||
DateTime dateTime = (DateTime)t;
|
||||
dateTime = dateTime.ToUniversalTime();
|
||||
long ticks = JsonConvert.ConvertDateTimeToJavaScriptTicks(dateTime);
|
||||
_writer.Write(ticks);
|
||||
@@ -121,6 +121,11 @@ namespace Newtonsoft.Json.Bson
|
||||
_writer.Write((byte)BsonBinaryType.Data);
|
||||
_writer.Write(data);
|
||||
break;
|
||||
case JTokenType.Raw:
|
||||
// hacky
|
||||
JRaw r = (JRaw)t;
|
||||
_writer.Write((byte[])r.Value);
|
||||
break;
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException("t", "Unexpected token when writing BSON: {0}".FormatWith(CultureInfo.InvariantCulture, t.Type));
|
||||
}
|
||||
@@ -182,6 +187,9 @@ namespace Newtonsoft.Json.Bson
|
||||
return BsonType.Date;
|
||||
case JTokenType.Bytes:
|
||||
return BsonType.Binary;
|
||||
case JTokenType.Raw:
|
||||
// hacky
|
||||
return BsonType.Oid;
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException("t", "Unexpected token when resolving JSON type to BSON type: {0}".FormatWith(CultureInfo.InvariantCulture, t.Type));
|
||||
}
|
||||
@@ -235,7 +243,7 @@ namespace Newtonsoft.Json.Bson
|
||||
return bases;
|
||||
}
|
||||
case JTokenType.Property:
|
||||
JProperty property = (JProperty) t;
|
||||
JProperty property = (JProperty)t;
|
||||
int ss = 1;
|
||||
ss += CalculateSize(property.Name);
|
||||
ss += CalculateSize(property.Value);
|
||||
@@ -256,8 +264,11 @@ namespace Newtonsoft.Json.Bson
|
||||
case JTokenType.Date:
|
||||
return 8;
|
||||
case JTokenType.Bytes:
|
||||
byte[] data = (byte[]) t;
|
||||
byte[] data = (byte[])t;
|
||||
return 4 + 1 + data.Length;
|
||||
case JTokenType.Raw:
|
||||
// hacky
|
||||
return 12;
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException("t", "Unexpected token when writing BSON: {0}".FormatWith(CultureInfo.InvariantCulture, t.Type));
|
||||
}
|
||||
@@ -312,5 +323,21 @@ namespace Newtonsoft.Json.Bson
|
||||
{
|
||||
throw new JsonWriterException("Cannot write raw JSON as BSON.");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Writes a <see cref="T:Byte[]"/> value that represents a BSON object id.
|
||||
/// </summary>
|
||||
/// <param name="value"></param>
|
||||
public void WriteObjectId(byte[] value)
|
||||
{
|
||||
ValidationUtils.ArgumentNotNull(value, "value");
|
||||
|
||||
if (value.Length != 12)
|
||||
throw new Exception("An object id must be 12 bytes");
|
||||
|
||||
// hack to update the writer state
|
||||
AutoComplete(JsonToken.Undefined);
|
||||
AddValue(new JRaw(value), JsonToken.Raw);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -88,9 +88,10 @@ namespace Newtonsoft.Json.Converters
|
||||
/// </summary>
|
||||
/// <param name="reader">The <see cref="JsonReader"/> to read from.</param>
|
||||
/// <param name="objectType">Type of the object.</param>
|
||||
/// <param name="existingValue">The existing value of object being read.</param>
|
||||
/// <param name="serializer">The calling serializer.</param>
|
||||
/// <returns>The object value.</returns>
|
||||
public override object ReadJson(JsonReader reader, Type objectType, JsonSerializer serializer)
|
||||
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
|
||||
{
|
||||
Type t = (ReflectionUtils.IsNullableType(objectType))
|
||||
? Nullable.GetUnderlyingType(objectType)
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Newtonsoft.Json.Bson;
|
||||
using System.Globalization;
|
||||
using Newtonsoft.Json.Utilities;
|
||||
|
||||
namespace Newtonsoft.Json.Converters
|
||||
{
|
||||
public class BsonObjectIdConverter : JsonConverter
|
||||
{
|
||||
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
|
||||
{
|
||||
BsonObjectId objectId = (BsonObjectId) value;
|
||||
|
||||
BsonWriter bsonWriter = writer as BsonWriter;
|
||||
if (bsonWriter != null)
|
||||
{
|
||||
bsonWriter.WriteObjectId(objectId.Value);
|
||||
}
|
||||
else
|
||||
{
|
||||
writer.WriteValue(objectId.Value);
|
||||
}
|
||||
}
|
||||
|
||||
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
|
||||
{
|
||||
if (reader.TokenType != JsonToken.Bytes)
|
||||
throw new JsonSerializationException("Expected Bytes but got {0}.".FormatWith(CultureInfo.InvariantCulture, reader.TokenType));
|
||||
|
||||
byte[] value = (byte[])reader.Value;
|
||||
|
||||
return new BsonObjectId(value);
|
||||
}
|
||||
|
||||
public override bool CanConvert(Type objectType)
|
||||
{
|
||||
return (objectType == typeof (BsonObjectId));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -49,9 +49,10 @@ namespace Newtonsoft.Json.Converters
|
||||
/// </summary>
|
||||
/// <param name="reader">The <see cref="JsonReader"/> to read from.</param>
|
||||
/// <param name="objectType">Type of the object.</param>
|
||||
/// <param name="existingValue">The existing value of object being read.</param>
|
||||
/// <param name="serializer">The calling serializer.</param>
|
||||
/// <returns>The object value.</returns>
|
||||
public override object ReadJson(JsonReader reader, Type objectType, JsonSerializer serializer)
|
||||
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
|
||||
{
|
||||
T value = Create(objectType);
|
||||
if (value == null)
|
||||
|
||||
@@ -63,9 +63,10 @@ namespace Newtonsoft.Json.Converters
|
||||
/// </summary>
|
||||
/// <param name="reader">The <see cref="JsonReader"/> to read from.</param>
|
||||
/// <param name="objectType">Type of the object.</param>
|
||||
/// <param name="existingValue">The existing value of object being read.</param>
|
||||
/// <param name="serializer">The calling serializer.</param>
|
||||
/// <returns>The object value.</returns>
|
||||
public override object ReadJson(JsonReader reader, Type objectType, JsonSerializer serializer)
|
||||
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
|
||||
{
|
||||
DataSet ds = new DataSet();
|
||||
|
||||
@@ -75,7 +76,7 @@ namespace Newtonsoft.Json.Converters
|
||||
|
||||
while (reader.TokenType == JsonToken.PropertyName)
|
||||
{
|
||||
DataTable dt = (DataTable)converter.ReadJson(reader, typeof (DataTable), serializer);
|
||||
DataTable dt = (DataTable)converter.ReadJson(reader, typeof (DataTable), null, serializer);
|
||||
ds.Tables.Add(dt);
|
||||
}
|
||||
|
||||
|
||||
@@ -67,7 +67,7 @@ namespace Newtonsoft.Json.Converters
|
||||
/// <param name="objectType">Type of the object.</param>
|
||||
/// <param name="serializer">The calling serializer.</param>
|
||||
/// <returns>The object value.</returns>
|
||||
public override object ReadJson(JsonReader reader, Type objectType, JsonSerializer serializer)
|
||||
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
|
||||
{
|
||||
DataTable dt;
|
||||
|
||||
|
||||
@@ -98,9 +98,10 @@ namespace Newtonsoft.Json.Converters
|
||||
/// </summary>
|
||||
/// <param name="reader">The <see cref="JsonReader"/> to read from.</param>
|
||||
/// <param name="objectType">Type of the object.</param>
|
||||
/// <param name="existingValue">The existing value of object being read.</param>
|
||||
/// <param name="serializer">The calling serializer.</param>
|
||||
/// <returns>The object value.</returns>
|
||||
public override object ReadJson(JsonReader reader, Type objectType, JsonSerializer serializer)
|
||||
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
|
||||
{
|
||||
IEntityKeyMember entityKeyMember = DynamicWrapper.CreateWrapper<IEntityKeyMember>(Activator.CreateInstance(objectType));
|
||||
|
||||
|
||||
@@ -62,9 +62,10 @@ namespace Newtonsoft.Json.Converters
|
||||
/// </summary>
|
||||
/// <param name="reader">The <see cref="JsonReader"/> to read from.</param>
|
||||
/// <param name="objectType">Type of the object.</param>
|
||||
/// <param name="existingValue">The existing value of object being read.</param>
|
||||
/// <param name="serializer">The calling serializer.</param>
|
||||
/// <returns>The object value.</returns>
|
||||
public override object ReadJson(JsonReader reader, Type objectType, JsonSerializer serializer)
|
||||
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
@@ -89,9 +89,10 @@ namespace Newtonsoft.Json.Converters
|
||||
/// </summary>
|
||||
/// <param name="reader">The <see cref="JsonReader"/> to read from.</param>
|
||||
/// <param name="objectType">Type of the object.</param>
|
||||
/// <param name="existingValue">The existing value of object being read.</param>
|
||||
/// <param name="serializer">The calling serializer.</param>
|
||||
/// <returns>The object value.</returns>
|
||||
public override object ReadJson(JsonReader reader, Type objectType, JsonSerializer serializer)
|
||||
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
|
||||
{
|
||||
bool nullable = ReflectionUtils.IsNullableType(objectType);
|
||||
Type t = (nullable)
|
||||
|
||||
@@ -50,7 +50,7 @@ namespace Newtonsoft.Json.Converters
|
||||
/// <param name="objectType">Type of the object.</param>
|
||||
/// <param name="serializer">The calling serializer.</param>
|
||||
/// <returns>The object value.</returns>
|
||||
public override object ReadJson(JsonReader reader, Type objectType, JsonSerializer serializer)
|
||||
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
|
||||
{
|
||||
Type t = (ReflectionUtils.IsNullableType(objectType))
|
||||
? Nullable.GetUnderlyingType(objectType)
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Newtonsoft.Json.Utilities;
|
||||
using System.Reflection;
|
||||
|
||||
namespace Newtonsoft.Json.Converters
|
||||
{
|
||||
public class KeyValuePairConverter : JsonConverter
|
||||
{
|
||||
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
|
||||
{
|
||||
Type t = value.GetType();
|
||||
PropertyInfo keyProperty = t.GetProperty("Key");
|
||||
PropertyInfo valueProperty = t.GetProperty("Value");
|
||||
|
||||
writer.WriteStartObject();
|
||||
writer.WritePropertyName("Key");
|
||||
serializer.Serialize(writer, ReflectionUtils.GetMemberValue(keyProperty, value));
|
||||
writer.WritePropertyName("Value");
|
||||
serializer.Serialize(writer, ReflectionUtils.GetMemberValue(valueProperty, value));
|
||||
writer.WriteEndObject();
|
||||
}
|
||||
|
||||
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
|
||||
{
|
||||
IList<Type> genericArguments = objectType.GetGenericArguments();
|
||||
Type keyType = genericArguments[0];
|
||||
Type valueType = genericArguments[1];
|
||||
|
||||
reader.Read();
|
||||
reader.Read();
|
||||
object key = serializer.Deserialize(reader, keyType);
|
||||
reader.Read();
|
||||
reader.Read();
|
||||
object value = serializer.Deserialize(reader, valueType);
|
||||
reader.Read();
|
||||
|
||||
return ReflectionUtils.CreateInstance(objectType, key, value);
|
||||
}
|
||||
|
||||
public override bool CanConvert(Type objectType)
|
||||
{
|
||||
if (objectType.IsValueType && objectType.IsGenericType)
|
||||
return (objectType.GetGenericTypeDefinition() == typeof (KeyValuePair<,>));
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -63,9 +63,10 @@ namespace Newtonsoft.Json.Converters
|
||||
/// </summary>
|
||||
/// <param name="reader">The <see cref="JsonReader"/> to read from.</param>
|
||||
/// <param name="objectType">Type of the object.</param>
|
||||
/// <param name="existingValue">The existing value of object being read.</param>
|
||||
/// <param name="serializer">The calling serializer.</param>
|
||||
/// <returns>The object value.</returns>
|
||||
public override object ReadJson(JsonReader reader, Type objectType, JsonSerializer serializer)
|
||||
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
|
||||
{
|
||||
Type t = (ReflectionUtils.IsNullableType(objectType))
|
||||
? Nullable.GetUnderlyingType(objectType)
|
||||
|
||||
@@ -1016,9 +1016,10 @@ namespace Newtonsoft.Json.Converters
|
||||
/// </summary>
|
||||
/// <param name="reader">The <see cref="JsonReader"/> to read from.</param>
|
||||
/// <param name="objectType">Type of the object.</param>
|
||||
/// <param name="existingValue">The existing value of object being read.</param>
|
||||
/// <param name="serializer">The calling serializer.</param>
|
||||
/// <returns>The object value.</returns>
|
||||
public override object ReadJson(JsonReader reader, Type objectType, JsonSerializer serializer)
|
||||
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
|
||||
{
|
||||
XmlNamespaceManager manager = new XmlNamespaceManager(new NameTable());
|
||||
IXmlDocument document;
|
||||
|
||||
@@ -49,9 +49,10 @@ namespace Newtonsoft.Json
|
||||
/// </summary>
|
||||
/// <param name="reader">The <see cref="JsonReader"/> to read from.</param>
|
||||
/// <param name="objectType">Type of the object.</param>
|
||||
/// <param name="existingValue">The existing value of object being read.</param>
|
||||
/// <param name="serializer">The calling serializer.</param>
|
||||
/// <returns>The object value.</returns>
|
||||
public abstract object ReadJson(JsonReader reader, Type objectType, JsonSerializer serializer);
|
||||
public abstract object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer);
|
||||
|
||||
/// <summary>
|
||||
/// Determines whether this instance can convert the specified object type.
|
||||
|
||||
@@ -373,6 +373,9 @@ namespace Newtonsoft.Json
|
||||
case JsonToken.Raw:
|
||||
WriteRawValue((string)reader.Value);
|
||||
break;
|
||||
case JsonToken.Bytes:
|
||||
WriteValue((byte[])reader.Value);
|
||||
break;
|
||||
default:
|
||||
throw MiscellaneousUtils.CreateArgumentOutOfRangeException("TokenType", reader.TokenType, "Unexpected token type.");
|
||||
}
|
||||
@@ -563,7 +566,7 @@ namespace Newtonsoft.Json
|
||||
{
|
||||
}
|
||||
|
||||
private void AutoComplete(JsonToken tokenBeingWritten)
|
||||
internal void AutoComplete(JsonToken tokenBeingWritten)
|
||||
{
|
||||
int token;
|
||||
|
||||
|
||||
@@ -25,7 +25,7 @@ namespace Newtonsoft.Json.Linq
|
||||
/// Initializes a new instance of the <see cref="JRaw"/> class.
|
||||
/// </summary>
|
||||
/// <param name="rawJson">The raw json.</param>
|
||||
public JRaw(string rawJson)
|
||||
public JRaw(object rawJson)
|
||||
: base(rawJson, JTokenType.Raw)
|
||||
{
|
||||
}
|
||||
@@ -50,39 +50,5 @@ namespace Newtonsoft.Json.Linq
|
||||
{
|
||||
return new JRaw(this);
|
||||
}
|
||||
|
||||
//public override void WriteTo(JsonWriter writer, params JsonConverter[] converters)
|
||||
//{
|
||||
// writer.WriteRawValue(Value);
|
||||
//}
|
||||
|
||||
//internal override JToken CloneToken()
|
||||
//{
|
||||
// throw new NotImplementedException();
|
||||
//}
|
||||
|
||||
//internal override bool DeepEquals(JToken node)
|
||||
//{
|
||||
// JRaw other = node as JRaw;
|
||||
// if (other == null)
|
||||
// return false;
|
||||
|
||||
// return (this == other || string.Equals(Value, other.Value, StringComparison.Ordinal));
|
||||
//}
|
||||
|
||||
//public override JTokenType Type
|
||||
//{
|
||||
// get { return JTokenType.Raw; }
|
||||
//}
|
||||
|
||||
//public override bool HasValues
|
||||
//{
|
||||
// get { return false; }
|
||||
//}
|
||||
|
||||
//internal override int GetDeepHashCode()
|
||||
//{
|
||||
// return Value.GetHashCode();
|
||||
//}
|
||||
}
|
||||
}
|
||||
@@ -139,7 +139,7 @@ namespace Newtonsoft.Json.Linq
|
||||
AddValue(new JValue(value), token);
|
||||
}
|
||||
|
||||
private void AddValue(JValue value, JsonToken token)
|
||||
internal void AddValue(JValue value, JsonToken token)
|
||||
{
|
||||
if (_parent != null)
|
||||
{
|
||||
|
||||
@@ -315,7 +315,7 @@ namespace Newtonsoft.Json.Linq
|
||||
}, _value);
|
||||
break;
|
||||
case JTokenType.Raw:
|
||||
writer.WriteRawValue((string)_value);
|
||||
writer.WriteRawValue((_value != null) ? _value.ToString() : null);
|
||||
break;
|
||||
case JTokenType.Bytes:
|
||||
WriteConvertableValue(writer, converters, v => writer.WriteValue((byte[])v), _value);
|
||||
|
||||
@@ -58,16 +58,19 @@
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Bson\BsonBinaryType.cs" />
|
||||
<Compile Include="Bson\BsonObjectId.cs" />
|
||||
<Compile Include="Bson\BsonReader.cs" />
|
||||
<Compile Include="Bson\BsonType.cs" />
|
||||
<Compile Include="Bson\BsonWriter.cs" />
|
||||
<Compile Include="ConstructorHandling.cs" />
|
||||
<Compile Include="Converters\BinaryConverter.cs" />
|
||||
<Compile Include="Converters\BsonObjectIdConverter.cs" />
|
||||
<Compile Include="Converters\CustomCreationConverter.cs" />
|
||||
<Compile Include="Converters\DataSetConverter.cs" />
|
||||
<Compile Include="Converters\DataTableConverter.cs" />
|
||||
<Compile Include="Converters\DateTimeConverterBase.cs" />
|
||||
<Compile Include="Converters\EntityKeyMemberConverter.cs" />
|
||||
<Compile Include="Converters\KeyValuePairConverter.cs" />
|
||||
<Compile Include="Converters\StringEnumConverter.cs" />
|
||||
<Compile Include="IJsonLineInfo.cs" />
|
||||
<Compile Include="JsonArrayAttribute.cs" />
|
||||
@@ -164,6 +167,7 @@
|
||||
<Compile Include="Serialization\JsonSerializerProxy.cs" />
|
||||
<Compile Include="Serialization\JsonStringContract.cs" />
|
||||
<Compile Include="Serialization\JsonTypeReflector.cs" />
|
||||
<Compile Include="Serialization\LateBoundMetadataTypeAttribute.cs" />
|
||||
<Compile Include="Serialization\ObjectConstructor.cs" />
|
||||
<Compile Include="Serialization\OnErrorAttribute.cs" />
|
||||
<Compile Include="Serialization\ReflectionValueProvider.cs" />
|
||||
@@ -175,20 +179,23 @@
|
||||
<Compile Include="Utilities\CollectionWrapper.cs" />
|
||||
<Compile Include="Utilities\DateTimeUtils.cs" />
|
||||
<Compile Include="Utilities\DictionaryWrapper.cs" />
|
||||
<Compile Include="Utilities\DynamicReflectionDelegateFactory.cs" />
|
||||
<Compile Include="Utilities\DynamicWrapper.cs" />
|
||||
<Compile Include="Utilities\EnumUtils.cs" />
|
||||
<Compile Include="Utilities\EnumValue.cs" />
|
||||
<Compile Include="Utilities\EnumValues.cs" />
|
||||
<Compile Include="Utilities\ILGeneratorExtensions.cs" />
|
||||
<Compile Include="Utilities\JavaScriptUtils.cs" />
|
||||
<Compile Include="JsonToken.cs" />
|
||||
<Compile Include="JsonWriter.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="Utilities\CollectionUtils.cs" />
|
||||
<Compile Include="Utilities\LateBoundDelegateFactory.cs" />
|
||||
<Compile Include="Utilities\LateBoundReflectionDelegateFactory.cs" />
|
||||
<Compile Include="Utilities\ListWrapper.cs" />
|
||||
<Compile Include="Utilities\MathUtils.cs" />
|
||||
<Compile Include="Utilities\MethodCaller.cs" />
|
||||
<Compile Include="Utilities\MethodCall.cs" />
|
||||
<Compile Include="Utilities\MiscellaneousUtils.cs" />
|
||||
<Compile Include="Utilities\ReflectionDelegateFactory.cs" />
|
||||
<Compile Include="Utilities\ReflectionUtils.cs" />
|
||||
<Compile Include="Utilities\StringBuffer.cs" />
|
||||
<Compile Include="Utilities\StringUtils.cs" />
|
||||
|
||||
@@ -78,16 +78,19 @@
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Bson\BsonBinaryType.cs" />
|
||||
<Compile Include="Bson\BsonObjectId.cs" />
|
||||
<Compile Include="Bson\BsonReader.cs" />
|
||||
<Compile Include="Bson\BsonType.cs" />
|
||||
<Compile Include="Bson\BsonWriter.cs" />
|
||||
<Compile Include="ConstructorHandling.cs" />
|
||||
<Compile Include="Converters\BinaryConverter.cs" />
|
||||
<Compile Include="Converters\BsonObjectIdConverter.cs" />
|
||||
<Compile Include="Converters\CustomCreationConverter.cs" />
|
||||
<Compile Include="Converters\DataSetConverter.cs" />
|
||||
<Compile Include="Converters\DataTableConverter.cs" />
|
||||
<Compile Include="Converters\DateTimeConverterBase.cs" />
|
||||
<Compile Include="Converters\EntityKeyMemberConverter.cs" />
|
||||
<Compile Include="Converters\KeyValuePairConverter.cs" />
|
||||
<Compile Include="Converters\StringEnumConverter.cs" />
|
||||
<Compile Include="Linq\ComponentModel\JPropertyDescriptor.cs" />
|
||||
<Compile Include="Linq\ComponentModel\JTypeDescriptionProvider.cs" />
|
||||
@@ -183,13 +186,17 @@
|
||||
<Compile Include="Serialization\JsonStringContract.cs" />
|
||||
<Compile Include="Serialization\JsonTypeReflector.cs" />
|
||||
<Compile Include="Serialization\CachedAttributeGetter.cs" />
|
||||
<Compile Include="Serialization\LateBoundMetadataTypeAttribute.cs" />
|
||||
<Compile Include="Serialization\ObjectConstructor.cs" />
|
||||
<Compile Include="Serialization\OnErrorAttribute.cs" />
|
||||
<Compile Include="Serialization\ReflectionValueProvider.cs" />
|
||||
<Compile Include="Utilities\Base64Encoder.cs" />
|
||||
<Compile Include="Utilities\DynamicReflectionDelegateFactory.cs" />
|
||||
<Compile Include="Utilities\DynamicWrapper.cs" />
|
||||
<Compile Include="Utilities\LateBoundDelegateFactory.cs" />
|
||||
<Compile Include="Utilities\MethodCaller.cs" />
|
||||
<Compile Include="Utilities\ILGeneratorExtensions.cs" />
|
||||
<Compile Include="Utilities\LateBoundReflectionDelegateFactory.cs" />
|
||||
<Compile Include="Utilities\MethodCall.cs" />
|
||||
<Compile Include="Utilities\ReflectionDelegateFactory.cs" />
|
||||
<Compile Include="Utilities\ThreadSafeStore.cs" />
|
||||
<Compile Include="TypeNameHandling.cs" />
|
||||
<Compile Include="Utilities\BidirectionalDictionary.cs" />
|
||||
|
||||
@@ -55,11 +55,13 @@
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Bson\BsonBinaryType.cs" />
|
||||
<Compile Include="Bson\BsonObjectId.cs" />
|
||||
<Compile Include="Bson\BsonReader.cs" />
|
||||
<Compile Include="Bson\BsonType.cs" />
|
||||
<Compile Include="Bson\BsonWriter.cs" />
|
||||
<Compile Include="ConstructorHandling.cs" />
|
||||
<Compile Include="Converters\BinaryConverter.cs" />
|
||||
<Compile Include="Converters\BsonObjectIdConverter.cs" />
|
||||
<Compile Include="Converters\CustomCreationConverter.cs" />
|
||||
<Compile Include="Converters\DataSetConverter.cs" />
|
||||
<Compile Include="Converters\DataTableConverter.cs" />
|
||||
@@ -68,6 +70,7 @@
|
||||
<Compile Include="Converters\IsoDateTimeConverter.cs" />
|
||||
<Compile Include="Converters\JavaScriptDateTimeConverter.cs" />
|
||||
<Compile Include="Converters\JsonDateTimeSerializationMode.cs" />
|
||||
<Compile Include="Converters\KeyValuePairConverter.cs" />
|
||||
<Compile Include="Converters\StringEnumConverter.cs" />
|
||||
<Compile Include="Converters\XmlNodeConverter.cs" />
|
||||
<Compile Include="Converters\HtmlColorConverter.cs" />
|
||||
@@ -147,6 +150,7 @@
|
||||
<Compile Include="Serialization\JsonSerializerInternalReader.cs" />
|
||||
<Compile Include="Serialization\JsonSerializerInternalWriter.cs" />
|
||||
<Compile Include="Serialization\JsonStringContract.cs" />
|
||||
<Compile Include="Serialization\LateBoundMetadataTypeAttribute.cs" />
|
||||
<Compile Include="Serialization\ObjectConstructor.cs" />
|
||||
<Compile Include="Serialization\OnErrorAttribute.cs" />
|
||||
<Compile Include="Serialization\ReflectionValueProvider.cs" />
|
||||
@@ -159,16 +163,19 @@
|
||||
<Compile Include="Utilities\ConvertUtils.cs" />
|
||||
<Compile Include="Utilities\DateTimeUtils.cs" />
|
||||
<Compile Include="Utilities\DictionaryWrapper.cs" />
|
||||
<Compile Include="Utilities\DynamicReflectionDelegateFactory.cs" />
|
||||
<Compile Include="Utilities\DynamicWrapper.cs" />
|
||||
<Compile Include="Utilities\EnumUtils.cs" />
|
||||
<Compile Include="Utilities\EnumValue.cs" />
|
||||
<Compile Include="Utilities\EnumValues.cs" />
|
||||
<Compile Include="Utilities\ILGeneratorExtensions.cs" />
|
||||
<Compile Include="Utilities\JavaScriptUtils.cs" />
|
||||
<Compile Include="Utilities\LateBoundDelegateFactory.cs" />
|
||||
<Compile Include="Utilities\LateBoundReflectionDelegateFactory.cs" />
|
||||
<Compile Include="Utilities\ListWrapper.cs" />
|
||||
<Compile Include="Utilities\MathUtils.cs" />
|
||||
<Compile Include="Utilities\MethodCaller.cs" />
|
||||
<Compile Include="Utilities\MethodCall.cs" />
|
||||
<Compile Include="Utilities\MiscellaneousUtils.cs" />
|
||||
<Compile Include="Utilities\ReflectionDelegateFactory.cs" />
|
||||
<Compile Include="Utilities\ReflectionUtils.cs" />
|
||||
<Compile Include="Utilities\StringBuffer.cs" />
|
||||
<Compile Include="Utilities\StringUtils.cs" />
|
||||
|
||||
@@ -84,12 +84,15 @@
|
||||
<Compile Include="Bson\BsonReader.cs" />
|
||||
<Compile Include="Bson\BsonType.cs" />
|
||||
<Compile Include="Bson\BsonWriter.cs" />
|
||||
<Compile Include="Bson\BsonObjectId.cs" />
|
||||
<Compile Include="Converters\BinaryConverter.cs" />
|
||||
<Compile Include="Converters\DataSetConverter.cs" />
|
||||
<Compile Include="Converters\DataTableConverter.cs" />
|
||||
<Compile Include="Converters\CustomCreationConverter.cs" />
|
||||
<Compile Include="Converters\DateTimeConverterBase.cs" />
|
||||
<Compile Include="Converters\EntityKeyMemberConverter.cs" />
|
||||
<Compile Include="Converters\KeyValuePairConverter.cs" />
|
||||
<Compile Include="Converters\BsonObjectIdConverter.cs" />
|
||||
<Compile Include="Converters\StringEnumConverter.cs" />
|
||||
<Compile Include="ConstructorHandling.cs" />
|
||||
<Compile Include="Linq\JPath.cs" />
|
||||
@@ -186,13 +189,17 @@
|
||||
<Compile Include="Serialization\JsonStringContract.cs" />
|
||||
<Compile Include="Serialization\JsonTypeReflector.cs" />
|
||||
<Compile Include="Serialization\CachedAttributeGetter.cs" />
|
||||
<Compile Include="Serialization\LateBoundMetadataTypeAttribute.cs" />
|
||||
<Compile Include="Serialization\ReflectionValueProvider.cs" />
|
||||
<Compile Include="Serialization\OnErrorAttribute.cs" />
|
||||
<Compile Include="Utilities\Base64Encoder.cs" />
|
||||
<Compile Include="Utilities\DynamicWrapper.cs" />
|
||||
<Compile Include="Utilities\LateBoundDelegateFactory.cs" />
|
||||
<Compile Include="Utilities\DynamicReflectionDelegateFactory.cs" />
|
||||
<Compile Include="Serialization\ObjectConstructor.cs" />
|
||||
<Compile Include="Utilities\MethodCaller.cs" />
|
||||
<Compile Include="Utilities\ILGeneratorExtensions.cs" />
|
||||
<Compile Include="Utilities\ReflectionDelegateFactory.cs" />
|
||||
<Compile Include="Utilities\LateBoundReflectionDelegateFactory.cs" />
|
||||
<Compile Include="Utilities\MethodCall.cs" />
|
||||
<Compile Include="Utilities\ThreadSafeStore.cs" />
|
||||
<Compile Include="TypeNameHandling.cs" />
|
||||
<Compile Include="Utilities\BidirectionalDictionary.cs" />
|
||||
|
||||
@@ -31,6 +31,7 @@ using System.Globalization;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Runtime.Serialization;
|
||||
using System.Security.Permissions;
|
||||
using Newtonsoft.Json.Converters;
|
||||
using Newtonsoft.Json.Utilities;
|
||||
using Newtonsoft.Json.Linq;
|
||||
@@ -79,11 +80,13 @@ namespace Newtonsoft.Json.Serialization
|
||||
new EntityKeyMemberConverter(),
|
||||
#endif
|
||||
new BinaryConverter(),
|
||||
new KeyValuePairConverter(),
|
||||
#if !SILVERLIGHT
|
||||
new XmlNodeConverter(),
|
||||
new DataSetConverter(),
|
||||
new DataTableConverter()
|
||||
new DataTableConverter(),
|
||||
#endif
|
||||
new BsonObjectIdConverter()
|
||||
};
|
||||
|
||||
private static Dictionary<ResolverContractKey, JsonContract> _sharedContractCache;
|
||||
@@ -92,6 +95,18 @@ namespace Newtonsoft.Json.Serialization
|
||||
private Dictionary<ResolverContractKey, JsonContract> _instanceContractCache;
|
||||
private readonly bool _sharedCache;
|
||||
|
||||
/// <summary>
|
||||
/// Gets a value indicating whether members are being get and set using dynamic code generation.
|
||||
/// This value is determined by the runtime permissions available.
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// <c>true</c> if using dynamic code generation; otherwise, <c>false</c>.
|
||||
/// </value>
|
||||
public bool DynamicCodeGeneration
|
||||
{
|
||||
get { return JsonTypeReflector.DynamicCodeGeneration; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the default members search flags.
|
||||
/// </summary>
|
||||
@@ -267,6 +282,11 @@ namespace Newtonsoft.Json.Serialization
|
||||
return JsonTypeReflector.GetJsonConverter(objectType, objectType);
|
||||
}
|
||||
|
||||
private Func<object> GetDefaultCreator(Type createdType)
|
||||
{
|
||||
return JsonTypeReflector.ReflectionDelegateFactory.CreateDefaultConstructor(createdType);
|
||||
}
|
||||
|
||||
private void InitializeContract(JsonContract contract)
|
||||
{
|
||||
JsonContainerAttribute containerAttribute = JsonTypeReflector.GetJsonContainerAttribute(contract.UnderlyingType);
|
||||
@@ -292,19 +312,8 @@ namespace Newtonsoft.Json.Serialization
|
||||
if (ReflectionUtils.HasDefaultConstructor(contract.CreatedType, true)
|
||||
|| contract.CreatedType.IsValueType)
|
||||
{
|
||||
#if !PocketPC && !SILVERLIGHT
|
||||
contract.DefaultCreator = LateBoundDelegateFactory.CreateDefaultConstructor(contract.CreatedType);
|
||||
#else
|
||||
ConstructorInfo constructorInfo = ReflectionUtils.GetDefaultConstructor(contract.CreatedType, true);
|
||||
contract.DefaultCreator = GetDefaultCreator(contract.CreatedType);
|
||||
|
||||
contract.DefaultCreator = () =>
|
||||
{
|
||||
if (contract.CreatedType.IsValueType)
|
||||
return Activator.CreateInstance(contract.CreatedType);
|
||||
|
||||
return constructorInfo.Invoke(null);
|
||||
};
|
||||
#endif
|
||||
contract.DefaultCreatorNonPublic = (!contract.CreatedType.IsValueType &&
|
||||
ReflectionUtils.GetDefaultConstructor(contract.CreatedType) == null);
|
||||
}
|
||||
@@ -410,9 +419,9 @@ namespace Newtonsoft.Json.Serialization
|
||||
ConstructorInfo constructorInfo = objectType.GetConstructor(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, null, new [] {typeof (SerializationInfo), typeof (StreamingContext)}, null);
|
||||
if (constructorInfo != null)
|
||||
{
|
||||
MethodCaller<object> methodCaller = LateBoundDelegateFactory.CreateMethodCall(constructorInfo);
|
||||
MethodCall<object> methodCall = JsonTypeReflector.ReflectionDelegateFactory.CreateMethodCall(constructorInfo);
|
||||
|
||||
contract.ISerializableCreator = (args => methodCaller(null, args));
|
||||
contract.ISerializableCreator = (args => methodCall(null, args));
|
||||
}
|
||||
|
||||
return contract;
|
||||
@@ -572,10 +581,11 @@ namespace Newtonsoft.Json.Serialization
|
||||
protected virtual IValueProvider CreateMemberValueProvider(MemberInfo member)
|
||||
{
|
||||
#if !PocketPC && !SILVERLIGHT
|
||||
return new DynamicValueProvider(member);
|
||||
#else
|
||||
return new ReflectionValueProvider(member);
|
||||
if (DynamicCodeGeneration)
|
||||
return new DynamicValueProvider(member);
|
||||
#endif
|
||||
|
||||
return new ReflectionValueProvider(member);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -63,7 +63,7 @@ namespace Newtonsoft.Json.Serialization
|
||||
try
|
||||
{
|
||||
if (_setter == null)
|
||||
_setter = LateBoundDelegateFactory.CreateSet<object>(_memberInfo);
|
||||
_setter = DynamicReflectionDelegateFactory.Instance.CreateSet<object>(_memberInfo);
|
||||
|
||||
#if DEBUG
|
||||
// dynamic method doesn't check whether the type is 'legal' to set
|
||||
@@ -97,7 +97,7 @@ namespace Newtonsoft.Json.Serialization
|
||||
try
|
||||
{
|
||||
if (_getter == null)
|
||||
_getter = LateBoundDelegateFactory.CreateGet<object>(_memberInfo);
|
||||
_getter = DynamicReflectionDelegateFactory.Instance.CreateGet<object>(_memberInfo);
|
||||
|
||||
return _getter(target);
|
||||
}
|
||||
|
||||
@@ -41,7 +41,7 @@ namespace Newtonsoft.Json.Serialization
|
||||
private bool _isCollectionItemTypeNullableType;
|
||||
private Type _genericCollectionDefinitionType;
|
||||
private Type _genericWrapperType;
|
||||
private MethodCaller<object> _genericWrapperCreator;
|
||||
private MethodCall<object> _genericWrapperCreator;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="JsonArrayContract"/> class.
|
||||
@@ -78,11 +78,7 @@ namespace Newtonsoft.Json.Serialization
|
||||
_genericWrapperType = ReflectionUtils.MakeGenericType(typeof(CollectionWrapper<>), CollectionItemType);
|
||||
|
||||
ConstructorInfo genericWrapperConstructor = _genericWrapperType.GetConstructor(new[] { _genericCollectionDefinitionType });
|
||||
#if !PocketPC && !SILVERLIGHT
|
||||
_genericWrapperCreator = LateBoundDelegateFactory.CreateMethodCall(genericWrapperConstructor);
|
||||
#else
|
||||
_genericWrapperCreator = (target, args) => genericWrapperConstructor.Invoke(new[] { args[0] });
|
||||
#endif
|
||||
_genericWrapperCreator = JsonTypeReflector.ReflectionDelegateFactory.CreateMethodCall(genericWrapperConstructor);
|
||||
}
|
||||
|
||||
return (IWrappedCollection)_genericWrapperCreator(null, list);
|
||||
|
||||
@@ -41,7 +41,7 @@ namespace Newtonsoft.Json.Serialization
|
||||
|
||||
private readonly Type _genericCollectionDefinitionType;
|
||||
private Type _genericWrapperType;
|
||||
private MethodCaller<object> _genericWrapperCreator;
|
||||
private MethodCall<object> _genericWrapperCreator;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="JsonDictionaryContract"/> class.
|
||||
@@ -81,11 +81,7 @@ namespace Newtonsoft.Json.Serialization
|
||||
_genericWrapperType = ReflectionUtils.MakeGenericType(typeof(DictionaryWrapper<,>), DictionaryKeyType, DictionaryValueType);
|
||||
|
||||
ConstructorInfo genericWrapperConstructor = _genericWrapperType.GetConstructor(new[] { _genericCollectionDefinitionType });
|
||||
#if !PocketPC && !SILVERLIGHT
|
||||
_genericWrapperCreator = LateBoundDelegateFactory.CreateMethodCall(genericWrapperConstructor);
|
||||
#else
|
||||
_genericWrapperCreator = (target, args) => genericWrapperConstructor.Invoke(new[] { args[0] });
|
||||
#endif
|
||||
_genericWrapperCreator = JsonTypeReflector.ReflectionDelegateFactory.CreateMethodCall(genericWrapperConstructor);
|
||||
}
|
||||
|
||||
return (IWrappedDictionary)_genericWrapperCreator(null, dictionary);
|
||||
|
||||
@@ -114,7 +114,7 @@ namespace Newtonsoft.Json.Serialization
|
||||
if (reader.TokenType == JsonToken.None && !reader.Read())
|
||||
return null;
|
||||
|
||||
return CreateValue(reader, objectType, GetContractSafe(objectType), null, null);
|
||||
return CreateValueNonProperty(reader, objectType, GetContractSafe(objectType));
|
||||
}
|
||||
|
||||
private JsonSerializerProxy GetInternalSerializer()
|
||||
@@ -177,31 +177,36 @@ namespace Newtonsoft.Json.Serialization
|
||||
return token;
|
||||
}
|
||||
|
||||
private object CreateValue(JsonReader reader, Type objectType, JsonContract contract, object existingValue, JsonConverter memberConverter)
|
||||
private object CreateValueProperty(JsonReader reader, JsonProperty property, object target, bool gottenCurrentValue, object currentValue)
|
||||
{
|
||||
JsonConverter converter = null;
|
||||
if (memberConverter != null)
|
||||
{
|
||||
// member attribute converter
|
||||
converter = memberConverter;
|
||||
}
|
||||
else if (contract != null)
|
||||
{
|
||||
JsonConverter matchingConverter;
|
||||
if (contract.Converter != null)
|
||||
// class attribute converter
|
||||
converter = contract.Converter;
|
||||
else if ((matchingConverter = Serializer.GetMatchingConverter(contract.UnderlyingType)) != null)
|
||||
// passed in converters
|
||||
converter = matchingConverter;
|
||||
else if (contract.InternalConverter != null)
|
||||
// internally specified converter
|
||||
converter = contract.InternalConverter;
|
||||
}
|
||||
JsonContract contract = GetContractSafe(property.PropertyType, currentValue);
|
||||
Type objectType = property.PropertyType;
|
||||
|
||||
JsonConverter converter = GetConverter(contract, property.MemberConverter);
|
||||
|
||||
if (converter != null && converter.CanRead)
|
||||
return converter.ReadJson(reader, objectType, GetInternalSerializer());
|
||||
{
|
||||
if (!gottenCurrentValue && target != null && property.Readable)
|
||||
currentValue = property.ValueProvider.GetValue(target);
|
||||
|
||||
return converter.ReadJson(reader, objectType, currentValue, GetInternalSerializer());
|
||||
}
|
||||
|
||||
return CreateValueInternal(reader, objectType, contract, currentValue);
|
||||
}
|
||||
|
||||
private object CreateValueNonProperty(JsonReader reader, Type objectType, JsonContract contract)
|
||||
{
|
||||
JsonConverter converter = GetConverter(contract, null);
|
||||
|
||||
if (converter != null && converter.CanRead)
|
||||
return converter.ReadJson(reader, objectType, null, GetInternalSerializer());
|
||||
|
||||
return CreateValueInternal(reader, objectType, contract, null);
|
||||
}
|
||||
|
||||
private object CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, object existingValue)
|
||||
{
|
||||
if (contract is JsonLinqContract)
|
||||
return CreateJToken(reader, contract);
|
||||
|
||||
@@ -253,6 +258,30 @@ namespace Newtonsoft.Json.Serialization
|
||||
throw new JsonSerializationException("Unexpected end when deserializing object.");
|
||||
}
|
||||
|
||||
private JsonConverter GetConverter(JsonContract contract, JsonConverter memberConverter)
|
||||
{
|
||||
JsonConverter converter = null;
|
||||
if (memberConverter != null)
|
||||
{
|
||||
// member attribute converter
|
||||
converter = memberConverter;
|
||||
}
|
||||
else if (contract != null)
|
||||
{
|
||||
JsonConverter matchingConverter;
|
||||
if (contract.Converter != null)
|
||||
// class attribute converter
|
||||
converter = contract.Converter;
|
||||
else if ((matchingConverter = Serializer.GetMatchingConverter(contract.UnderlyingType)) != null)
|
||||
// passed in converters
|
||||
converter = matchingConverter;
|
||||
else if (contract.InternalConverter != null)
|
||||
// internally specified converter
|
||||
converter = contract.InternalConverter;
|
||||
}
|
||||
return converter;
|
||||
}
|
||||
|
||||
private object CreateObject(JsonReader reader, Type objectType, JsonContract contract, object existingValue)
|
||||
{
|
||||
CheckedRead(reader);
|
||||
@@ -454,6 +483,7 @@ namespace Newtonsoft.Json.Serialization
|
||||
|
||||
object currentValue = null;
|
||||
bool useExistingValue = false;
|
||||
bool gottenCurrentValue = false;
|
||||
|
||||
ObjectCreationHandling objectCreationHandling =
|
||||
property.ObjectCreationHandling.GetValueOrDefault(Serializer.ObjectCreationHandling);
|
||||
@@ -463,6 +493,7 @@ namespace Newtonsoft.Json.Serialization
|
||||
&& property.Readable)
|
||||
{
|
||||
currentValue = property.ValueProvider.GetValue(target);
|
||||
gottenCurrentValue = true;
|
||||
|
||||
useExistingValue = (currentValue != null && !property.PropertyType.IsArray && !ReflectionUtils.InheritsGenericDefinition(property.PropertyType, typeof(ReadOnlyCollection<>)));
|
||||
}
|
||||
@@ -474,7 +505,7 @@ namespace Newtonsoft.Json.Serialization
|
||||
}
|
||||
|
||||
object existingValue = (useExistingValue) ? currentValue : null;
|
||||
object value = CreateValue(reader, property.PropertyType, GetContractSafe(property.PropertyType, existingValue), existingValue, property.Converter);
|
||||
object value = CreateValueProperty(reader, property, target, gottenCurrentValue, existingValue);
|
||||
|
||||
// always set the value if useExistingValue is false,
|
||||
// otherwise also set it if CreateValue returns a new value compared to the currentValue
|
||||
@@ -543,7 +574,7 @@ namespace Newtonsoft.Json.Serialization
|
||||
|
||||
try
|
||||
{
|
||||
dictionary[keyValue] = CreateValue(reader, contract.DictionaryValueType, GetContractSafe(contract.DictionaryValueType), null, null);
|
||||
dictionary[keyValue] = CreateValueNonProperty(reader, contract.DictionaryValueType, GetContractSafe(contract.DictionaryValueType));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@@ -607,7 +638,7 @@ namespace Newtonsoft.Json.Serialization
|
||||
default:
|
||||
try
|
||||
{
|
||||
object value = CreateValue(reader, contract.CollectionItemType, GetContractSafe(contract.CollectionItemType), null, null);
|
||||
object value = CreateValueNonProperty(reader, contract.CollectionItemType, GetContractSafe(contract.CollectionItemType));
|
||||
|
||||
wrappedList.Add(value);
|
||||
}
|
||||
@@ -717,7 +748,7 @@ namespace Newtonsoft.Json.Serialization
|
||||
{
|
||||
if (!property.Ignored)
|
||||
{
|
||||
propertyValues[property] = CreateValue(reader, property.PropertyType, GetContractSafe(property.PropertyType), null, property.MemberConverter);
|
||||
propertyValues[property] = CreateValueProperty(reader, property, null, true, null);
|
||||
}
|
||||
}
|
||||
else
|
||||
|
||||
@@ -29,6 +29,7 @@ using System.Globalization;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Runtime.Serialization;
|
||||
using System.Security.Permissions;
|
||||
using Newtonsoft.Json.Utilities;
|
||||
|
||||
namespace Newtonsoft.Json.Serialization
|
||||
@@ -176,7 +177,9 @@ namespace Newtonsoft.Json.Serialization
|
||||
if (attribute == null)
|
||||
return null;
|
||||
|
||||
IMetadataTypeAttribute metadataTypeAttribute = DynamicWrapper.CreateWrapper<IMetadataTypeAttribute>(attribute);
|
||||
IMetadataTypeAttribute metadataTypeAttribute = (DynamicCodeGeneration)
|
||||
? DynamicWrapper.CreateWrapper<IMetadataTypeAttribute>(attribute)
|
||||
: new LateBoundMetadataTypeAttribute(attribute);
|
||||
|
||||
return metadataTypeAttribute.MetadataClassType;
|
||||
}
|
||||
@@ -249,5 +252,45 @@ namespace Newtonsoft.Json.Serialization
|
||||
return ReflectionUtils.GetAttribute<T>(attributeProvider, true);
|
||||
}
|
||||
#endif
|
||||
|
||||
private static bool? _dynamicCodeGeneration;
|
||||
|
||||
public static bool DynamicCodeGeneration
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_dynamicCodeGeneration == null)
|
||||
{
|
||||
#if !PocketPC && !SILVERLIGHT
|
||||
try
|
||||
{
|
||||
new ReflectionPermission(ReflectionPermissionFlag.MemberAccess).Demand();
|
||||
_dynamicCodeGeneration = true;
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
_dynamicCodeGeneration = false;
|
||||
}
|
||||
#else
|
||||
_dynamicCodeGeneration = false;
|
||||
#endif
|
||||
}
|
||||
|
||||
return _dynamicCodeGeneration.Value;
|
||||
}
|
||||
}
|
||||
|
||||
public static ReflectionDelegateFactory ReflectionDelegateFactory
|
||||
{
|
||||
get
|
||||
{
|
||||
#if !PocketPC && !SILVERLIGHT
|
||||
if (DynamicCodeGeneration)
|
||||
return DynamicReflectionDelegateFactory.Instance;
|
||||
#endif
|
||||
|
||||
return LateBoundReflectionDelegateFactory.Instance;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
#region License
|
||||
// Copyright (c) 2007 James Newton-King
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person
|
||||
// obtaining a copy of this software and associated documentation
|
||||
// files (the "Software"), to deal in the Software without
|
||||
// restriction, including without limitation the rights to use,
|
||||
// copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the
|
||||
// Software is furnished to do so, subject to the following
|
||||
// conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be
|
||||
// included in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
||||
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
||||
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||||
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||
// OTHER DEALINGS IN THE SOFTWARE.
|
||||
#endregion
|
||||
|
||||
#if !SILVERLIGHT && !PocketPC && !NET20
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Newtonsoft.Json.Utilities;
|
||||
using System.Reflection;
|
||||
|
||||
namespace Newtonsoft.Json.Serialization
|
||||
{
|
||||
internal class LateBoundMetadataTypeAttribute : IMetadataTypeAttribute
|
||||
{
|
||||
private static PropertyInfo _metadataClassTypeProperty;
|
||||
|
||||
private readonly object _attribute;
|
||||
|
||||
public LateBoundMetadataTypeAttribute(object attribute)
|
||||
{
|
||||
_attribute = attribute;
|
||||
}
|
||||
|
||||
public Type MetadataClassType
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_metadataClassTypeProperty == null)
|
||||
_metadataClassTypeProperty = _attribute.GetType().GetProperty("MetadataClassType");
|
||||
|
||||
return (Type)ReflectionUtils.GetMemberValue(_metadataClassTypeProperty, _attribute);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
+21
-83
@@ -34,20 +34,9 @@ using System.Globalization;
|
||||
|
||||
namespace Newtonsoft.Json.Utilities
|
||||
{
|
||||
internal static class LateBoundDelegateFactory
|
||||
internal class DynamicReflectionDelegateFactory : ReflectionDelegateFactory
|
||||
{
|
||||
public static Func<T, object> CreateGet<T>(MemberInfo memberInfo)
|
||||
{
|
||||
PropertyInfo propertyInfo = memberInfo as PropertyInfo;
|
||||
if (propertyInfo != null)
|
||||
return CreateGet<T>(propertyInfo);
|
||||
|
||||
FieldInfo fieldInfo = memberInfo as FieldInfo;
|
||||
if (fieldInfo != null)
|
||||
return CreateGet<T>(fieldInfo);
|
||||
|
||||
throw new Exception("Could not create getter for {0}.".FormatWith(CultureInfo.InvariantCulture, memberInfo));
|
||||
}
|
||||
public static DynamicReflectionDelegateFactory Instance = new DynamicReflectionDelegateFactory();
|
||||
|
||||
private static DynamicMethod CreateDynamicMethod(string name, Type returnType, Type[] parameterTypes, Type owner)
|
||||
{
|
||||
@@ -58,20 +47,7 @@ namespace Newtonsoft.Json.Utilities
|
||||
return dynamicMethod;
|
||||
}
|
||||
|
||||
public static Action<T, object> CreateSet<T>(MemberInfo memberInfo)
|
||||
{
|
||||
PropertyInfo propertyInfo = memberInfo as PropertyInfo;
|
||||
if (propertyInfo != null)
|
||||
return CreateSet<T>(propertyInfo);
|
||||
|
||||
FieldInfo fieldInfo = memberInfo as FieldInfo;
|
||||
if (fieldInfo != null)
|
||||
return CreateSet<T>(fieldInfo);
|
||||
|
||||
throw new Exception("Could not create setter for {0}.".FormatWith(CultureInfo.InvariantCulture, memberInfo));
|
||||
}
|
||||
|
||||
public static MethodCaller<object> CreateMethodCall(MethodBase method)
|
||||
public override MethodCall<object> CreateMethodCall(MethodBase method)
|
||||
{
|
||||
DynamicMethod dynamicMethod = CreateDynamicMethod(method.ToString(), typeof(object), new[] { typeof(object), typeof(object[]) }, method.DeclaringType);
|
||||
ILGenerator generator = dynamicMethod.GetILGenerator();
|
||||
@@ -105,9 +81,7 @@ namespace Newtonsoft.Json.Utilities
|
||||
if (method.IsConstructor)
|
||||
generator.Emit(OpCodes.Newobj, (ConstructorInfo)method);
|
||||
else if (method.IsFinal || !method.IsVirtual)
|
||||
generator.Emit(OpCodes.Call, (MethodInfo)method);
|
||||
else
|
||||
generator.Emit(OpCodes.Callvirt, (MethodInfo)method);
|
||||
generator.CallMethod((MethodInfo)method);
|
||||
|
||||
Type returnType = method.IsConstructor
|
||||
? method.DeclaringType
|
||||
@@ -118,13 +92,12 @@ namespace Newtonsoft.Json.Utilities
|
||||
else
|
||||
generator.Emit(OpCodes.Ldnull);
|
||||
|
||||
generator.Emit(OpCodes.Ret);
|
||||
generator.Return();
|
||||
|
||||
return (MethodCaller<object>)dynamicMethod.CreateDelegate(typeof(MethodCaller<object>));
|
||||
return (MethodCall<object>)dynamicMethod.CreateDelegate(typeof(MethodCall<object>));
|
||||
}
|
||||
|
||||
|
||||
public static Func<object> CreateDefaultConstructor(Type type)
|
||||
public override Func<object> CreateDefaultConstructor(Type type)
|
||||
{
|
||||
DynamicMethod dynamicMethod = CreateDynamicMethod("Create" + type.FullName, typeof(object), Type.EmptyTypes, type);
|
||||
dynamicMethod.InitLocals = true;
|
||||
@@ -148,12 +121,12 @@ namespace Newtonsoft.Json.Utilities
|
||||
generator.Emit(OpCodes.Newobj, constructorInfo);
|
||||
}
|
||||
|
||||
generator.Emit(OpCodes.Ret);
|
||||
generator.Return();
|
||||
|
||||
return (Func<object>)dynamicMethod.CreateDelegate(typeof(Func<object>));
|
||||
}
|
||||
|
||||
public static Func<T, object> CreateGet<T>(PropertyInfo propertyInfo)
|
||||
public override Func<T, object> CreateGet<T>(PropertyInfo propertyInfo)
|
||||
{
|
||||
MethodInfo getMethod = propertyInfo.GetGetMethod(true);
|
||||
if (getMethod == null)
|
||||
@@ -167,43 +140,14 @@ namespace Newtonsoft.Json.Utilities
|
||||
if (!getMethod.IsStatic)
|
||||
generator.PushInstance(propertyInfo.DeclaringType);
|
||||
|
||||
if (getMethod.IsFinal || !getMethod.IsVirtual)
|
||||
generator.Emit(OpCodes.Call, getMethod);
|
||||
else
|
||||
generator.Emit(OpCodes.Callvirt, getMethod);
|
||||
|
||||
generator.CallMethod(getMethod);
|
||||
generator.BoxIfNeeded(propertyInfo.PropertyType);
|
||||
generator.Emit(OpCodes.Ret);
|
||||
generator.Return();
|
||||
|
||||
return (Func<T, object>)dynamicMethod.CreateDelegate(typeof(Func<T, object>));
|
||||
}
|
||||
|
||||
static void PushInstance(this ILGenerator generator, Type type)
|
||||
{
|
||||
generator.Emit(OpCodes.Ldarg_0);
|
||||
if (type.IsValueType)
|
||||
generator.Emit(OpCodes.Unbox, type);
|
||||
else
|
||||
generator.Emit(OpCodes.Castclass, type);
|
||||
}
|
||||
|
||||
static void BoxIfNeeded(this ILGenerator generator, Type type)
|
||||
{
|
||||
if (type.IsValueType)
|
||||
generator.Emit(OpCodes.Box, type);
|
||||
else
|
||||
generator.Emit(OpCodes.Castclass, type);
|
||||
}
|
||||
|
||||
static void UnboxIfNeeded(this ILGenerator generator, Type type)
|
||||
{
|
||||
if (type.IsValueType)
|
||||
generator.Emit(OpCodes.Unbox_Any, type);
|
||||
else
|
||||
generator.Emit(OpCodes.Castclass, type);
|
||||
}
|
||||
|
||||
public static Func<T, object> CreateGet<T>(FieldInfo fieldInfo)
|
||||
public override Func<T, object> CreateGet<T>(FieldInfo fieldInfo)
|
||||
{
|
||||
DynamicMethod dynamicMethod = CreateDynamicMethod("Get" + fieldInfo.Name, typeof(T), new[] { typeof(object) }, fieldInfo.DeclaringType);
|
||||
|
||||
@@ -214,13 +158,12 @@ namespace Newtonsoft.Json.Utilities
|
||||
|
||||
generator.Emit(OpCodes.Ldfld, fieldInfo);
|
||||
generator.BoxIfNeeded(fieldInfo.FieldType);
|
||||
generator.Emit(OpCodes.Ret);
|
||||
generator.Return();
|
||||
|
||||
return (Func<T, object>)
|
||||
dynamicMethod.CreateDelegate(typeof(Func<T, object>));
|
||||
return (Func<T, object>)dynamicMethod.CreateDelegate(typeof(Func<T, object>));
|
||||
}
|
||||
|
||||
public static Action<T, object> CreateSet<T>(FieldInfo fieldInfo)
|
||||
public override Action<T, object> CreateSet<T>(FieldInfo fieldInfo)
|
||||
{
|
||||
DynamicMethod dynamicMethod = CreateDynamicMethod("Set" + fieldInfo.Name, null, new[] { typeof(object), typeof(object) }, fieldInfo.DeclaringType);
|
||||
ILGenerator generator = dynamicMethod.GetILGenerator();
|
||||
@@ -231,29 +174,24 @@ namespace Newtonsoft.Json.Utilities
|
||||
generator.Emit(OpCodes.Ldarg_1);
|
||||
generator.UnboxIfNeeded(fieldInfo.FieldType);
|
||||
generator.Emit(OpCodes.Stfld, fieldInfo);
|
||||
generator.Emit(OpCodes.Ret);
|
||||
generator.Return();
|
||||
|
||||
return (Action<T, object>)dynamicMethod.CreateDelegate(typeof(Action<T, object>));
|
||||
}
|
||||
|
||||
public static Action<T, object> CreateSet<T>(PropertyInfo propertyInfo)
|
||||
public override Action<T, object> CreateSet<T>(PropertyInfo propertyInfo)
|
||||
{
|
||||
MethodInfo getMethod = propertyInfo.GetSetMethod(true);
|
||||
MethodInfo setMethod = propertyInfo.GetSetMethod(true);
|
||||
DynamicMethod dynamicMethod = CreateDynamicMethod("Set" + propertyInfo.Name, null, new[] { typeof(object), typeof(object) }, propertyInfo.DeclaringType);
|
||||
ILGenerator generator = dynamicMethod.GetILGenerator();
|
||||
|
||||
if (!getMethod.IsStatic)
|
||||
if (!setMethod.IsStatic)
|
||||
generator.PushInstance(propertyInfo.DeclaringType);
|
||||
|
||||
generator.Emit(OpCodes.Ldarg_1);
|
||||
generator.UnboxIfNeeded(propertyInfo.PropertyType);
|
||||
|
||||
if (getMethod.IsFinal || !getMethod.IsVirtual)
|
||||
generator.Emit(OpCodes.Call, getMethod);
|
||||
else
|
||||
generator.Emit(OpCodes.Callvirt, getMethod);
|
||||
|
||||
generator.Emit(OpCodes.Ret);
|
||||
generator.CallMethod(setMethod);
|
||||
generator.Return();
|
||||
|
||||
return (Action<T, object>)dynamicMethod.CreateDelegate(typeof(Action<T, object>));
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
#region License
|
||||
// Copyright (c) 2007 James Newton-King
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person
|
||||
// obtaining a copy of this software and associated documentation
|
||||
// files (the "Software"), to deal in the Software without
|
||||
// restriction, including without limitation the rights to use,
|
||||
// copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the
|
||||
// Software is furnished to do so, subject to the following
|
||||
// conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be
|
||||
// included in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
||||
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
||||
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||||
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||
// OTHER DEALINGS IN THE SOFTWARE.
|
||||
#endregion
|
||||
|
||||
#if !PocketPC && !SILVERLIGHT
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection.Emit;
|
||||
using System.Text;
|
||||
using System.Reflection;
|
||||
|
||||
namespace Newtonsoft.Json.Utilities
|
||||
{
|
||||
public static class ILGeneratorExtensions
|
||||
{
|
||||
public static void PushInstance(this ILGenerator generator, Type type)
|
||||
{
|
||||
generator.Emit(OpCodes.Ldarg_0);
|
||||
if (type.IsValueType)
|
||||
generator.Emit(OpCodes.Unbox, type);
|
||||
else
|
||||
generator.Emit(OpCodes.Castclass, type);
|
||||
}
|
||||
|
||||
public static void BoxIfNeeded(this ILGenerator generator, Type type)
|
||||
{
|
||||
if (type.IsValueType)
|
||||
generator.Emit(OpCodes.Box, type);
|
||||
else
|
||||
generator.Emit(OpCodes.Castclass, type);
|
||||
}
|
||||
|
||||
public static void UnboxIfNeeded(this ILGenerator generator, Type type)
|
||||
{
|
||||
if (type.IsValueType)
|
||||
generator.Emit(OpCodes.Unbox_Any, type);
|
||||
else
|
||||
generator.Emit(OpCodes.Castclass, type);
|
||||
}
|
||||
|
||||
public static void CallMethod(this ILGenerator generator, MethodInfo methodInfo)
|
||||
{
|
||||
if (methodInfo.IsFinal || !methodInfo.IsVirtual)
|
||||
generator.Emit(OpCodes.Call, methodInfo);
|
||||
else
|
||||
generator.Emit(OpCodes.Callvirt, methodInfo);
|
||||
}
|
||||
|
||||
public static void Return(this ILGenerator generator)
|
||||
{
|
||||
generator.Emit(OpCodes.Ret);
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,75 @@
|
||||
#region License
|
||||
// Copyright (c) 2007 James Newton-King
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person
|
||||
// obtaining a copy of this software and associated documentation
|
||||
// files (the "Software"), to deal in the Software without
|
||||
// restriction, including without limitation the rights to use,
|
||||
// copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the
|
||||
// Software is furnished to do so, subject to the following
|
||||
// conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be
|
||||
// included in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
||||
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
||||
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||||
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||
// OTHER DEALINGS IN THE SOFTWARE.
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using System.Reflection;
|
||||
|
||||
namespace Newtonsoft.Json.Utilities
|
||||
{
|
||||
internal class LateBoundReflectionDelegateFactory : ReflectionDelegateFactory
|
||||
{
|
||||
public static readonly LateBoundReflectionDelegateFactory Instance = new LateBoundReflectionDelegateFactory();
|
||||
|
||||
public override MethodCall<object> CreateMethodCall(MethodBase method)
|
||||
{
|
||||
ConstructorInfo c = method as ConstructorInfo;
|
||||
if (c != null)
|
||||
return (o, a) => c.Invoke(a);
|
||||
|
||||
return (o, a) => method.Invoke(o, a);
|
||||
}
|
||||
|
||||
public override Func<object> CreateDefaultConstructor(Type type)
|
||||
{
|
||||
if (type.IsValueType)
|
||||
return () => ReflectionUtils.CreateInstance(type);
|
||||
|
||||
ConstructorInfo constructorInfo = ReflectionUtils.GetDefaultConstructor(type, true);
|
||||
|
||||
return () => constructorInfo.Invoke(null);
|
||||
}
|
||||
|
||||
public override Func<T, object> CreateGet<T>(PropertyInfo propertyInfo)
|
||||
{
|
||||
return o => propertyInfo.GetValue(o, null);
|
||||
}
|
||||
|
||||
public override Func<T, object> CreateGet<T>(FieldInfo fieldInfo)
|
||||
{
|
||||
return o => fieldInfo.GetValue(o);
|
||||
}
|
||||
|
||||
public override Action<T, object> CreateSet<T>(FieldInfo fieldInfo)
|
||||
{
|
||||
return (o, v) => fieldInfo.SetValue(o, v);
|
||||
}
|
||||
|
||||
public override Action<T, object> CreateSet<T>(PropertyInfo propertyInfo)
|
||||
{
|
||||
return (o, v) => propertyInfo.SetValue(o, v, null);
|
||||
}
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -25,5 +25,5 @@
|
||||
|
||||
namespace Newtonsoft.Json.Utilities
|
||||
{
|
||||
internal delegate object MethodCaller<T>(T target, params object[] args);
|
||||
internal delegate object MethodCall<T>(T target, params object[] args);
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
#region License
|
||||
// Copyright (c) 2007 James Newton-King
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person
|
||||
// obtaining a copy of this software and associated documentation
|
||||
// files (the "Software"), to deal in the Software without
|
||||
// restriction, including without limitation the rights to use,
|
||||
// copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the
|
||||
// Software is furnished to do so, subject to the following
|
||||
// conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be
|
||||
// included in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
||||
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
||||
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||||
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||
// OTHER DEALINGS IN THE SOFTWARE.
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using System.Reflection;
|
||||
|
||||
namespace Newtonsoft.Json.Utilities
|
||||
{
|
||||
internal abstract class ReflectionDelegateFactory
|
||||
{
|
||||
public Func<T, object> CreateGet<T>(MemberInfo memberInfo)
|
||||
{
|
||||
PropertyInfo propertyInfo = memberInfo as PropertyInfo;
|
||||
if (propertyInfo != null)
|
||||
return CreateGet<T>(propertyInfo);
|
||||
|
||||
FieldInfo fieldInfo = memberInfo as FieldInfo;
|
||||
if (fieldInfo != null)
|
||||
return CreateGet<T>(fieldInfo);
|
||||
|
||||
throw new Exception("Could not create getter for {0}.".FormatWith(CultureInfo.InvariantCulture, memberInfo));
|
||||
}
|
||||
|
||||
public Action<T, object> CreateSet<T>(MemberInfo memberInfo)
|
||||
{
|
||||
PropertyInfo propertyInfo = memberInfo as PropertyInfo;
|
||||
if (propertyInfo != null)
|
||||
return CreateSet<T>(propertyInfo);
|
||||
|
||||
FieldInfo fieldInfo = memberInfo as FieldInfo;
|
||||
if (fieldInfo != null)
|
||||
return CreateSet<T>(fieldInfo);
|
||||
|
||||
throw new Exception("Could not create setter for {0}.".FormatWith(CultureInfo.InvariantCulture, memberInfo));
|
||||
}
|
||||
|
||||
public abstract MethodCall<object> CreateMethodCall(MethodBase method);
|
||||
public abstract Func<object> CreateDefaultConstructor(Type type);
|
||||
public abstract Func<T, object> CreateGet<T>(PropertyInfo propertyInfo);
|
||||
public abstract Func<T, object> CreateGet<T>(FieldInfo fieldInfo);
|
||||
public abstract Action<T, object> CreateSet<T>(FieldInfo fieldInfo);
|
||||
public abstract Action<T, object> CreateSet<T>(PropertyInfo propertyInfo);
|
||||
}
|
||||
}
|
||||
@@ -687,7 +687,7 @@ namespace Newtonsoft.Json.Utilities
|
||||
// CF doesn't have a Activator.CreateInstance overload that takes args
|
||||
// lame
|
||||
|
||||
if (type.IsValueType)
|
||||
if (type.IsValueType && CollectionUtils.IsNullOrEmpty<object>(args))
|
||||
return Activator.CreateInstance(type);
|
||||
|
||||
ConstructorInfo[] constructors = type.GetConstructors();
|
||||
|
||||
Reference in New Issue
Block a user