-Changed Newtonsoft.Json.Net20.dll to use LinqBridge source code rather than ilmerge to fix error

This commit is contained in:
JamesNK
2011-06-25 04:10:33 +00:00
parent 857d72530c
commit ba6ec3202e
15 changed files with 3291 additions and 67 deletions
+1 -13
View File
@@ -58,20 +58,8 @@ task Build -depends Clean {
}
}
# Merge LinqBridge into .NET 2.0 build
task Merge -depends Build {
$binaryDir = "$sourceDir\Newtonsoft.Json\bin\Release\Net20"
MergeAssembly "$binaryDir\Newtonsoft.Json.Net20.dll" $signKeyPath "$binaryDir\LinqBridge.dll"
del $binaryDir\LinqBridge.dll
$binaryDir = "$sourceDir\Newtonsoft.Json.Tests\bin\Release\Net20"
MergeAssembly "$binaryDir\Newtonsoft.Json.Tests.Net20.dll" $signKeyPath "$binaryDir\LinqBridge.dll"
MergeAssembly "$binaryDir\Newtonsoft.Json.Net20.dll" $signKeyPath "$binaryDir\LinqBridge.dll"
del $binaryDir\LinqBridge.dll
}
# Optional build documentation, add files to final zip
task Package -depends Merge {
task Package -depends Build {
foreach ($build in $builds)
{
$name = $build.TestsName
Binary file not shown.
-1
View File
@@ -3,7 +3,6 @@ Microsoft Visual Studio Solution File, Format Version 11.00
# Visual Studio 2010
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Lib", "Lib", "{620042D9-2753-48F5-BEDE-3905248781D2}"
ProjectSection(SolutionItems) = preProject
Lib\LinqBridge.dll = Lib\LinqBridge.dll
Lib\NUnit\DotNet\nunit.framework.dll = Lib\NUnit\DotNet\nunit.framework.dll
Lib\NUnit\DotNet\nunit.framework.xml = Lib\NUnit\DotNet\nunit.framework.xml
EndProjectSection
@@ -223,6 +223,15 @@ namespace Newtonsoft.Json.Tests.Linq
Assert.AreEqual(1, c);
}
[Test]
public void ConvertValueToFormattableType()
{
IFormattable f = (new JValue(1).Value<IFormattable>());
Assert.AreEqual(1, f);
Assert.AreEqual("01", f.ToString("00", CultureInfo.InvariantCulture));
}
[Test]
public void Ordering()
{
@@ -63,10 +63,6 @@
<CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<ItemGroup>
<Reference Include="LinqBridge, Version=1.0.0.0, Culture=neutral, PublicKeyToken=c2b14eb747628076, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\Lib\LinqBridge.dll</HintPath>
</Reference>
<Reference Include="nunit.framework, Version=2.4.3.0, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\Lib\NUnit\DotNet\nunit.framework.dll</HintPath>
+68 -2
View File
@@ -6,6 +6,7 @@ using System.Diagnostics;
using System.IO;
using System.Runtime.Serialization;
using System.Web.Script.Serialization;
using System.Xml.Linq;
using System.Xml.Serialization;
using Newtonsoft.Json.Utilities;
using NUnit.Framework;
@@ -146,6 +147,71 @@ namespace Newtonsoft.Json.Tests
}, "JObject");
}
[Test]
public void BuildJObjectComparedToXml()
{
const long totalIterations = 100000;
const String xml =
@"<?xml version=""1.0"" encoding=""ISO-8859-1""?>
<root>
<property name=""Property1"">1</property>
<property name=""Property2"">2</property>
<property name=""Property3"">3</property>
<property name=""Property4"">4</property>
<property name=""Property5"">5</property>
</root>";
const String json =
@"{
""Property1"":""1"",
""Property2"":""2"",
""Property3"":""3"",
""Property4"":""4"",
""Property5"":""5""
}";
var watch = new Stopwatch();
watch.Start();
for (long iteration = 0; iteration < totalIterations; ++iteration)
{
var obj = JObject.Parse(json);
obj["Property1"].Value<Int32>();
obj["Property2"].Value<Int32>();
obj["Property3"].Value<Int32>();
obj["Property4"].Value<Int32>();
obj["Property5"].Value<Int32>();
}
watch.Stop();
var performance1 = (totalIterations / watch.ElapsedMilliseconds) * 1000;
Console.WriteLine("JSON: " + watch.Elapsed.TotalSeconds);
watch.Reset();
watch.Start();
for (long iteration = 0; iteration < totalIterations; ++iteration)
{
var doc = XDocument.Parse(xml);
var alarmProperties = doc.Descendants("property");
foreach (var property in alarmProperties)
{
var attr = property.Attribute("name");
var name = attr.Value;
switch (name)
{
case "Property1": Int32.Parse(property.Value); break;
case "Property2": Int32.Parse(property.Value); break;
case "Property3": Int32.Parse(property.Value); break;
case "Property4": Int32.Parse(property.Value); break;
case "Property5": Int32.Parse(property.Value); break;
}
}
}
watch.Stop();
var performance2 = (totalIterations / watch.ElapsedMilliseconds) * 1000;
Console.WriteLine("XML: " + watch.Elapsed.TotalSeconds);
}
private void SerializeSize(object value)
{
// this is extremely slow with 5000 interations
@@ -547,7 +613,7 @@ namespace Newtonsoft.Json.Tests
{
JObject test = JObject.Parse(JsonText);
IsoDateTimeConverter isoDateTimeConverter = null;// = new IsoDateTimeConverter();
MemoryStream ms = new MemoryStream();
MemoryStream ms = new MemoryStream();
TimeOperation<object>(() =>
{
@@ -559,7 +625,7 @@ namespace Newtonsoft.Json.Tests
test.WriteTo(jsonTextWriter);
jsonTextWriter.Flush();
ms.ToArray();
//Encoding.UTF8.GetBytes(test.ToString(Formatting.None));
}
return null;
@@ -41,5 +41,5 @@ using System.Runtime.InteropServices;
// by using the '*' as shown below:
[assembly: AssemblyVersion("4.0.2.0")]
#if !PocketPC
[assembly: AssemblyFileVersion("4.0.2.13801")]
[assembly: AssemblyFileVersion("4.0.2.13825")]
#endif
@@ -28,7 +28,9 @@ using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using System.Text;
using Newtonsoft.Json.Tests.Linq;
using NUnit.Framework;
using Newtonsoft.Json.Utilities;
using Newtonsoft.Json.Tests.TestObjects;
@@ -107,6 +109,42 @@ namespace Newtonsoft.Json.Tests.Utilities
JsonSerializerTest.DictionaryKey key = (JsonSerializerTest.DictionaryKey) result;
Assert.AreEqual("First!", key.Value);
}
//[Test]
//public void sdfsdf()
//{
// string name = "MyAssembly";
// string filename = name + ".dll";
// AssemblyName s = new AssemblyName(name);
// AssemblyBuilder assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(s, AssemblyBuilderAccess.RunAndSave);
// ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule(filename, filename);
// TypeBuilder typeBuilder = moduleBuilder.DefineType("MyType", TypeAttributes.Class, typeof(object));
// MethodBuilder methodBuilder = typeBuilder.DefineMethod("TestSet", MethodAttributes.Public | MethodAttributes.Static, typeof(void), new[] { typeof(object), typeof(object) });
// DynamicReflectionDelegateFactory.GenerateCreateSetFieldIL(typeof(ClassWithGuid).GetField("GuidField"), methodBuilder.GetILGenerator());
// typeBuilder.CreateType();
// assemblyBuilder.Save(filename);
//}
//[Test]
//public void sdfsdf1()
//{
// string name = "MyAssembly1";
// string filename = name + ".dll";
// AssemblyName s = new AssemblyName(name);
// AssemblyBuilder assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(s, AssemblyBuilderAccess.RunAndSave);
// ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule(filename, filename);
// TypeBuilder typeBuilder = moduleBuilder.DefineType("MyType", TypeAttributes.Class, typeof(object));
// MethodBuilder methodBuilder = typeBuilder.DefineMethod("TestSet", MethodAttributes.Public | MethodAttributes.Static, typeof(void), new[] { typeof(object), typeof(object) });
// DynamicReflectionDelegateFactory.GenerateCreateSetPropertyIL(typeof(Car).GetProperty("Model"), methodBuilder.GetILGenerator());
// typeBuilder.CreateType();
// assemblyBuilder.Save(filename);
//}
}
}
#endif
+6 -13
View File
@@ -236,33 +236,26 @@ namespace Newtonsoft.Json.Linq
{
ValidationUtils.ArgumentNotNull(source, "source");
bool cast = typeof(JToken).IsAssignableFrom(typeof(U));
foreach (JToken token in source)
{
yield return Convert<JToken, U>(token, cast);
yield return Convert<JToken, U>(token);
}
}
internal static U Convert<T, U>(this T token) where T : JToken
{
bool cast = typeof(JToken).IsAssignableFrom(typeof(U));
if (token == null)
return default(U);
return Convert<T, U>(token, cast);
}
internal static U Convert<T, U>(this T token, bool cast) where T : JToken
{
if (cast)
if (token is U
// don't want to cast JValue to its interfaces, want to get the internal value
&& typeof(U) != typeof(IComparable) && typeof(U) != typeof(IFormattable))
{
// HACK
return (U)(object)token;
}
else
{
if (token == null)
return default(U);
JValue value = token as JValue;
if (value == null)
throw new InvalidCastException("Cannot cast {0} to {1}.".FormatWith(CultureInfo.InvariantCulture, token.GetType(), typeof(T)));
+5 -3
View File
@@ -306,9 +306,11 @@ namespace Newtonsoft.Json.Linq
}
if (reader.TokenType != JsonToken.StartObject)
throw new Exception(
"Error reading JObject from JsonReader. Current JsonReader item is not an object: {0}".FormatWith(
CultureInfo.InvariantCulture, reader.TokenType));
{
throw new Exception(
"Error reading JObject from JsonReader. Current JsonReader item is not an object: {0}".FormatWith(
CultureInfo.InvariantCulture, reader.TokenType));
}
JObject o = new JObject();
o.SetLineInfo(reader as IJsonLineInfo);
@@ -68,10 +68,6 @@
<CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<ItemGroup>
<Reference Include="LinqBridge, Version=1.0.0.0, Culture=neutral, PublicKeyToken=c2b14eb747628076, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\Lib\LinqBridge.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
@@ -96,6 +92,7 @@
<Compile Include="Converters\RegexConverter.cs" />
<Compile Include="Converters\StringEnumConverter.cs" />
<Compile Include="JsonConstructorAttribute.cs" />
<Compile Include="Utilities\LinqBridge.cs" />
<Compile Include="Linq\JPath.cs" />
<Compile Include="Linq\JPropertyDescriptor.cs" />
<Compile Include="Linq\JRaw.cs" />
@@ -110,7 +110,7 @@ using System.Security;
// by using the '*' as shown below:
[assembly: AssemblyVersion("4.0.2.0")]
#if !PocketPC
[assembly: AssemblyFileVersion("4.0.2.13801")]
[assembly: AssemblyFileVersion("4.0.2.13825")]
#endif
[assembly: CLSCompliant(true)]
@@ -338,6 +338,7 @@ namespace Newtonsoft.Json.Serialization
{
new ReflectionPermission(ReflectionPermissionFlag.MemberAccess).Demand();
new ReflectionPermission(ReflectionPermissionFlag.RestrictedMemberAccess).Demand();
new SecurityPermission(SecurityPermissionFlag.SkipVerification).Demand();
new SecurityPermission(SecurityPermissionFlag.UnmanagedCode).Demand();
new SecurityPermission(PermissionState.Unrestricted).Demand();
_dynamicCodeGeneration = true;
@@ -52,6 +52,13 @@ namespace Newtonsoft.Json.Utilities
DynamicMethod dynamicMethod = CreateDynamicMethod(method.ToString(), typeof(object), new[] { typeof(object), typeof(object[]) }, method.DeclaringType);
ILGenerator generator = dynamicMethod.GetILGenerator();
GenerateCreateMethodCallIL(method, generator);
return (MethodCall<T, object>)dynamicMethod.CreateDelegate(typeof(MethodCall<T, object>));
}
private void GenerateCreateMethodCallIL(MethodBase method, ILGenerator generator)
{
ParameterInfo[] args = method.GetParameters();
Label argsOk = generator.DefineLabel();
@@ -84,8 +91,8 @@ namespace Newtonsoft.Json.Utilities
generator.CallMethod((MethodInfo)method);
Type returnType = method.IsConstructor
? method.DeclaringType
: ((MethodInfo)method).ReturnType;
? method.DeclaringType
: ((MethodInfo)method).ReturnType;
if (returnType != typeof(void))
generator.BoxIfNeeded(returnType);
@@ -93,16 +100,21 @@ namespace Newtonsoft.Json.Utilities
generator.Emit(OpCodes.Ldnull);
generator.Return();
return (MethodCall<T, object>)dynamicMethod.CreateDelegate(typeof(MethodCall<T, object>));
}
public override Func<T> CreateDefaultConstructor<T>(Type type)
{
DynamicMethod dynamicMethod = CreateDynamicMethod("Create" + type.FullName, typeof(object), Type.EmptyTypes, type);
DynamicMethod dynamicMethod = CreateDynamicMethod("Create" + type.FullName, typeof(T), Type.EmptyTypes, type);
dynamicMethod.InitLocals = true;
ILGenerator generator = dynamicMethod.GetILGenerator();
GenerateCreateDefaultConstructorIL(type, generator);
return (Func<T>)dynamicMethod.CreateDelegate(typeof(Func<T>));
}
private void GenerateCreateDefaultConstructorIL(Type type, ILGenerator generator)
{
if (type.IsValueType)
{
generator.DeclareLocal(type);
@@ -122,20 +134,23 @@ namespace Newtonsoft.Json.Utilities
}
generator.Return();
return (Func<T>)dynamicMethod.CreateDelegate(typeof(Func<T>));
}
public override Func<T, object> CreateGet<T>(PropertyInfo propertyInfo)
{
DynamicMethod dynamicMethod = CreateDynamicMethod("Get" + propertyInfo.Name, typeof(T), new[] { typeof(object) }, propertyInfo.DeclaringType);
ILGenerator generator = dynamicMethod.GetILGenerator();
GenerateCreateGetPropertyIL(propertyInfo, generator);
return (Func<T, object>)dynamicMethod.CreateDelegate(typeof(Func<T, object>));
}
private void GenerateCreateGetPropertyIL(PropertyInfo propertyInfo, ILGenerator generator)
{
MethodInfo getMethod = propertyInfo.GetGetMethod(true);
if (getMethod == null)
throw new Exception("Property '{0}' does not have a getter.".FormatWith(CultureInfo.InvariantCulture,
propertyInfo.Name));
DynamicMethod dynamicMethod = CreateDynamicMethod("Get" + propertyInfo.Name, typeof(T), new[] { typeof(object) }, propertyInfo.DeclaringType);
ILGenerator generator = dynamicMethod.GetILGenerator();
throw new Exception("Property '{0}' does not have a getter.".FormatWith(CultureInfo.InvariantCulture, propertyInfo.Name));
if (!getMethod.IsStatic)
generator.PushInstance(propertyInfo.DeclaringType);
@@ -143,31 +158,40 @@ namespace Newtonsoft.Json.Utilities
generator.CallMethod(getMethod);
generator.BoxIfNeeded(propertyInfo.PropertyType);
generator.Return();
return (Func<T, object>)dynamicMethod.CreateDelegate(typeof(Func<T, object>));
}
public override Func<T, object> CreateGet<T>(FieldInfo fieldInfo)
{
DynamicMethod dynamicMethod = CreateDynamicMethod("Get" + fieldInfo.Name, typeof(T), new[] { typeof(object) }, fieldInfo.DeclaringType);
ILGenerator generator = dynamicMethod.GetILGenerator();
GenerateCreateGetFieldIL(fieldInfo, generator);
return (Func<T, object>)dynamicMethod.CreateDelegate(typeof(Func<T, object>));
}
private void GenerateCreateGetFieldIL(FieldInfo fieldInfo, ILGenerator generator)
{
if (!fieldInfo.IsStatic)
generator.PushInstance(fieldInfo.DeclaringType);
generator.Emit(OpCodes.Ldfld, fieldInfo);
generator.BoxIfNeeded(fieldInfo.FieldType);
generator.Return();
return (Func<T, object>)dynamicMethod.CreateDelegate(typeof(Func<T, object>));
}
public override Action<T, object> CreateSet<T>(FieldInfo fieldInfo)
{
DynamicMethod dynamicMethod = CreateDynamicMethod("Set" + fieldInfo.Name, null, new[] { typeof(object), typeof(object) }, fieldInfo.DeclaringType);
DynamicMethod dynamicMethod = CreateDynamicMethod("Set" + fieldInfo.Name, null, new[] { typeof(T), typeof(object) }, fieldInfo.DeclaringType);
ILGenerator generator = dynamicMethod.GetILGenerator();
GenerateCreateSetFieldIL(fieldInfo, generator);
return (Action<T, object>)dynamicMethod.CreateDelegate(typeof(Action<T, object>));
}
internal static void GenerateCreateSetFieldIL(FieldInfo fieldInfo, ILGenerator generator)
{
if (!fieldInfo.IsStatic)
generator.PushInstance(fieldInfo.DeclaringType);
@@ -175,16 +199,21 @@ namespace Newtonsoft.Json.Utilities
generator.UnboxIfNeeded(fieldInfo.FieldType);
generator.Emit(OpCodes.Stfld, fieldInfo);
generator.Return();
return (Action<T, object>)dynamicMethod.CreateDelegate(typeof(Action<T, object>));
}
public override Action<T, object> CreateSet<T>(PropertyInfo propertyInfo)
{
MethodInfo setMethod = propertyInfo.GetSetMethod(true);
DynamicMethod dynamicMethod = CreateDynamicMethod("Set" + propertyInfo.Name, null, new[] { typeof(object), typeof(object) }, propertyInfo.DeclaringType);
DynamicMethod dynamicMethod = CreateDynamicMethod("Set" + propertyInfo.Name, null, new[] { typeof(T), typeof(object) }, propertyInfo.DeclaringType);
ILGenerator generator = dynamicMethod.GetILGenerator();
GenerateCreateSetPropertyIL(propertyInfo, generator);
return (Action<T, object>)dynamicMethod.CreateDelegate(typeof(Action<T, object>));
}
internal static void GenerateCreateSetPropertyIL(PropertyInfo propertyInfo, ILGenerator generator)
{
MethodInfo setMethod = propertyInfo.GetSetMethod(true);
if (!setMethod.IsStatic)
generator.PushInstance(propertyInfo.DeclaringType);
@@ -192,8 +221,6 @@ namespace Newtonsoft.Json.Utilities
generator.UnboxIfNeeded(propertyInfo.PropertyType);
generator.CallMethod(setMethod);
generator.Return();
return (Action<T, object>)dynamicMethod.CreateDelegate(typeof(Action<T, object>));
}
}
}
File diff suppressed because it is too large Load Diff