-Fixed WinRT build

This commit is contained in:
James Newton-King
2013-04-25 13:35:26 +12:00
parent 405de98daf
commit 2004377f3e
8 changed files with 84 additions and 65 deletions
@@ -281,6 +281,7 @@
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Utilities\DateTimeUtilsTests.cs" /> <Compile Include="Utilities\DateTimeUtilsTests.cs" />
<Compile Include="Utilities\DynamicReflectionDelegateFactoryTests.cs" /> <Compile Include="Utilities\DynamicReflectionDelegateFactoryTests.cs" />
<Compile Include="Utilities\ExpressionReflectionDelegateFactoryTests.cs" />
<Compile Include="Utilities\ReflectionUtilsTests.cs" /> <Compile Include="Utilities\ReflectionUtilsTests.cs" />
<Compile Include="Utilities\StringUtilsTests.cs" /> <Compile Include="Utilities\StringUtilsTests.cs" />
</ItemGroup> </ItemGroup>
@@ -75,4 +75,4 @@ using System.Security;
// You can specify all the values or you can default the Revision and Build Numbers // You can specify all the values or you can default the Revision and Build Numbers
// by using the '*' as shown below: // by using the '*' as shown below:
[assembly: AssemblyVersion("4.5.0.0")] [assembly: AssemblyVersion("4.5.0.0")]
[assembly: AssemblyFileVersion("5.0.4.16024")] [assembly: AssemblyFileVersion("5.0.4.16025")]
@@ -36,5 +36,20 @@ namespace Newtonsoft.Json.Tests.TestObjects
public string Studio { get; set; } public string Studio { get; set; }
public DateTime? ReleaseDate { get; set; } public DateTime? ReleaseDate { get; set; }
public List<string> ReleaseCountries { get; set; } public List<string> ReleaseCountries { get; set; }
public override string ToString()
{
return base.ToString();
}
public override bool Equals(object obj)
{
return base.Equals(obj);
}
public override int GetHashCode()
{
return base.GetHashCode();
}
} }
} }
@@ -65,7 +65,7 @@ namespace Newtonsoft.Json.Tests.Utilities
[Test] [Test]
public void DefaultConstructor_Abstract() public void DefaultConstructor_Abstract()
{ {
ExceptionAssert.Throws<MissingMethodException>("Cannot create an abstract class.", ExceptionAssert.Throws<Exception>("Cannot create an abstract class.",
() => () =>
{ {
Func<object> create = ExpressionReflectionDelegateFactory.Instance.CreateDefaultConstructor<object>(typeof(Type)); Func<object> create = ExpressionReflectionDelegateFactory.Instance.CreateDefaultConstructor<object>(typeof(Type));
@@ -221,6 +221,7 @@
<Compile Include="Serialization\DynamicValueProvider.cs" /> <Compile Include="Serialization\DynamicValueProvider.cs" />
<Compile Include="Serialization\ErrorContext.cs" /> <Compile Include="Serialization\ErrorContext.cs" />
<Compile Include="Serialization\ErrorEventArgs.cs" /> <Compile Include="Serialization\ErrorEventArgs.cs" />
<Compile Include="Serialization\ExpressionValueProvider.cs" />
<Compile Include="Serialization\IContractResolver.cs" /> <Compile Include="Serialization\IContractResolver.cs" />
<Compile Include="Serialization\IReferenceResolver.cs" /> <Compile Include="Serialization\IReferenceResolver.cs" />
<Compile Include="Serialization\ITraceWriter.cs" /> <Compile Include="Serialization\ITraceWriter.cs" />
@@ -269,6 +270,7 @@
<Compile Include="Utilities\EnumUtils.cs" /> <Compile Include="Utilities\EnumUtils.cs" />
<Compile Include="Utilities\EnumValue.cs" /> <Compile Include="Utilities\EnumValue.cs" />
<Compile Include="Utilities\EnumValues.cs" /> <Compile Include="Utilities\EnumValues.cs" />
<Compile Include="Utilities\ExpressionReflectionDelegateFactory.cs" />
<Compile Include="Utilities\ILGeneratorExtensions.cs" /> <Compile Include="Utilities\ILGeneratorExtensions.cs" />
<Compile Include="Utilities\JavaScriptUtils.cs" /> <Compile Include="Utilities\JavaScriptUtils.cs" />
<Compile Include="Utilities\LateBoundReflectionDelegateFactory.cs" /> <Compile Include="Utilities\LateBoundReflectionDelegateFactory.cs" />
@@ -92,6 +92,6 @@ using System.Security;
// You can specify all the values or you can default the Revision and Build Numbers // You can specify all the values or you can default the Revision and Build Numbers
// by using the '*' as shown below: // by using the '*' as shown below:
[assembly: AssemblyVersion("4.5.0.0")] [assembly: AssemblyVersion("4.5.0.0")]
[assembly: AssemblyFileVersion("5.0.4.16024")] [assembly: AssemblyFileVersion("5.0.4.16025")]
[assembly: CLSCompliant(true)] [assembly: CLSCompliant(true)]
@@ -362,13 +362,8 @@ namespace Newtonsoft.Json.Serialization
if (ReadSpecialProperties(reader, ref objectType, ref contract, member, containerContract, containerMember, existingValue, out newValue, out id)) if (ReadSpecialProperties(reader, ref objectType, ref contract, member, containerContract, containerMember, existingValue, out newValue, out id))
return newValue; return newValue;
if (contract == null || objectType == typeof(object) || objectType == typeof(IDynamicMetaObjectProvider)) if (HasNoDefinedType(contract))
{ return CreateJObject(reader);
if (!HasDefinedType(objectType))
return CreateJObject(reader);
throw JsonSerializationException.Create(reader, "Could not resolve type '{0}' to a JsonContract.".FormatWith(CultureInfo.InvariantCulture, objectType));
}
switch (contract.ContractType) switch (contract.ContractType)
{ {
@@ -609,72 +604,69 @@ To fix this error either change the JSON to a {1} or change the deserialized typ
private object CreateList(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, object existingValue, string id) private object CreateList(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, object existingValue, string id)
{ {
object value; object value;
if (HasDefinedType(objectType))
if (HasNoDefinedType(contract))
return CreateJToken(reader, contract);
JsonArrayContract arrayContract = EnsureArrayContract(reader, objectType, contract);
if (existingValue == null)
{ {
JsonArrayContract arrayContract = EnsureArrayContract(reader, objectType, contract); bool createdFromNonDefaultConstructor;
IList list = CreateNewList(reader, arrayContract, out createdFromNonDefaultConstructor);
if (existingValue == null) if (id != null && createdFromNonDefaultConstructor)
{ throw JsonSerializationException.Create(reader, "Cannot preserve reference to array or readonly list, or list created from a non-default constructor: {0}.".FormatWith(CultureInfo.InvariantCulture, contract.UnderlyingType));
bool createdFromNonDefaultConstructor;
IList list = CreateNewList(reader, arrayContract, out createdFromNonDefaultConstructor);
if (id != null && createdFromNonDefaultConstructor) if (contract.OnSerializingCallbacks.Count > 0 && createdFromNonDefaultConstructor)
throw JsonSerializationException.Create(reader, "Cannot preserve reference to array or readonly list, or list created from a non-default constructor: {0}.".FormatWith(CultureInfo.InvariantCulture, contract.UnderlyingType)); throw JsonSerializationException.Create(reader, "Cannot call OnSerializing on an array or readonly list, or list created from a non-default constructor: {0}.".FormatWith(CultureInfo.InvariantCulture, contract.UnderlyingType));
if (contract.OnSerializingCallbacks.Count > 0 && createdFromNonDefaultConstructor) if (contract.OnErrorCallbacks.Count > 0 && createdFromNonDefaultConstructor)
throw JsonSerializationException.Create(reader, "Cannot call OnSerializing on an array or readonly list, or list created from a non-default constructor: {0}.".FormatWith(CultureInfo.InvariantCulture, contract.UnderlyingType)); throw JsonSerializationException.Create(reader, "Cannot call OnError on an array or readonly list, or list created from a non-default constructor: {0}.".FormatWith(CultureInfo.InvariantCulture, contract.UnderlyingType));
if (contract.OnErrorCallbacks.Count > 0 && createdFromNonDefaultConstructor) if (!arrayContract.IsMultidimensionalArray)
throw JsonSerializationException.Create(reader, "Cannot call OnError on an array or readonly list, or list created from a non-default constructor: {0}.".FormatWith(CultureInfo.InvariantCulture, contract.UnderlyingType)); PopulateList(list, reader, arrayContract, member, id);
if (!arrayContract.IsMultidimensionalArray)
PopulateList(list, reader, arrayContract, member, id);
else
PopulateMultidimensionalArray(list, reader, arrayContract, member, id);
if (createdFromNonDefaultConstructor)
{
if (arrayContract.IsMultidimensionalArray)
{
list = CollectionUtils.ToMultidimensionalArray(list, arrayContract.CollectionItemType, contract.CreatedType.GetArrayRank());
}
else if (contract.CreatedType.IsArray)
{
Array a = Array.CreateInstance(arrayContract.CollectionItemType, list.Count);
list.CopyTo(a, 0);
list = a;
}
else
{
// call constructor that takes IEnumerable<T>
return arrayContract.ParametrizedConstructor.Invoke(new object[] { list });
}
}
else if (list is IWrappedCollection)
{
return ((IWrappedCollection)list).UnderlyingCollection;
}
value = list;
}
else else
PopulateMultidimensionalArray(list, reader, arrayContract, member, id);
if (createdFromNonDefaultConstructor)
{ {
value = PopulateList((arrayContract.ShouldCreateWrapper) ? arrayContract.CreateWrapper(existingValue) : (IList) existingValue, reader, arrayContract, member, id); if (arrayContract.IsMultidimensionalArray)
{
list = CollectionUtils.ToMultidimensionalArray(list, arrayContract.CollectionItemType, contract.CreatedType.GetArrayRank());
}
else if (contract.CreatedType.IsArray)
{
Array a = Array.CreateInstance(arrayContract.CollectionItemType, list.Count);
list.CopyTo(a, 0);
list = a;
}
else
{
// call constructor that takes IEnumerable<T>
return arrayContract.ParametrizedConstructor.Invoke(new object[] { list });
}
} }
else if (list is IWrappedCollection)
{
return ((IWrappedCollection)list).UnderlyingCollection;
}
value = list;
} }
else else
{ {
value = CreateJToken(reader, contract); value = PopulateList((arrayContract.ShouldCreateWrapper) ? arrayContract.CreateWrapper(existingValue) : (IList) existingValue, reader, arrayContract, member, id);
} }
return value; return value;
} }
private bool HasDefinedType(Type type) private bool HasNoDefinedType(JsonContract contract)
{ {
return (type != null && type != typeof (object) && !typeof (JToken).IsSubclassOf(type) return (contract == null || contract.UnderlyingType == typeof (object) || contract.ContractType == JsonContractType.Linq
#if !(NET35 || NET20 || PORTABLE40) #if !(NET35 || NET20 || PORTABLE40)
&& type != typeof (IDynamicMetaObjectProvider) || contract.UnderlyingType == typeof(IDynamicMetaObjectProvider)
#endif #endif
); );
} }
@@ -1336,7 +1328,7 @@ To fix this error either change the environment to be fully trusted, change the
{ {
IDynamicMetaObjectProvider newObject; IDynamicMetaObjectProvider newObject;
if (contract.UnderlyingType.IsInterface() || contract.UnderlyingType.IsAbstract()) if (!contract.IsInstantiable)
throw JsonSerializationException.Create(reader, "Could not create an instance of type {0}. Type is an interface or abstract class and cannot be instantiated.".FormatWith(CultureInfo.InvariantCulture, contract.UnderlyingType)); throw JsonSerializationException.Create(reader, "Could not create an instance of type {0}. Type is an interface or abstract class and cannot be instantiated.".FormatWith(CultureInfo.InvariantCulture, contract.UnderlyingType));
if (contract.DefaultCreator != null && if (contract.DefaultCreator != null &&
@@ -96,16 +96,25 @@ namespace Newtonsoft.Json.Utilities
if (type.IsAbstract()) if (type.IsAbstract())
return () => (T)Activator.CreateInstance(type); return () => (T)Activator.CreateInstance(type);
Type resultType = typeof(T); try
{
Type resultType = typeof(T);
Expression expression = Expression.New(type); Expression expression = Expression.New(type);
expression = EnsureCastExpression(expression, resultType); expression = EnsureCastExpression(expression, resultType);
LambdaExpression lambdaExpression = Expression.Lambda(typeof(Func<T>), expression); LambdaExpression lambdaExpression = Expression.Lambda(typeof(Func<T>), expression);
Func<T> compiled = (Func<T>)lambdaExpression.Compile(); Func<T> compiled = (Func<T>)lambdaExpression.Compile();
return compiled; return compiled;
}
catch
{
// an error can be thrown if constructor is not valid on Win8
// will have INVOCATION_FLAGS_NON_W8P_FX_API invocation flag
return () => (T)Activator.CreateInstance(type);
}
} }
public override Func<T, object> CreateGet<T>(PropertyInfo propertyInfo) public override Func<T, object> CreateGet<T>(PropertyInfo propertyInfo)