Merge pull request #1126 from JonHanna/remove_list_array
Remove some unnecessary list and array allocations.
This commit is contained in:
@@ -24,6 +24,7 @@
|
||||
#endregion
|
||||
|
||||
#if !(DOTNET || PORTABLE40 || PORTABLE)
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using Newtonsoft.Json.Utilities;
|
||||
@@ -181,7 +182,7 @@ namespace Newtonsoft.Json.Converters
|
||||
}
|
||||
|
||||
Array destinationArray = Array.CreateInstance(column.DataType.GetElementType(), o.Count);
|
||||
Array.Copy(o.ToArray(), destinationArray, o.Count);
|
||||
((IList)o).CopyTo(destinationArray, 0);
|
||||
|
||||
dr[columnName] = destinationArray;
|
||||
}
|
||||
|
||||
@@ -710,15 +710,14 @@ namespace Newtonsoft.Json
|
||||
|
||||
Dictionary<string, bool> requiredProperties = _currentScope.RequiredProperties;
|
||||
|
||||
if (requiredProperties != null)
|
||||
if (requiredProperties != null && requiredProperties.Values.Any(v => !v))
|
||||
{
|
||||
List<string> unmatchedRequiredProperties =
|
||||
requiredProperties.Where(kv => !kv.Value).Select(kv => kv.Key).ToList();
|
||||
|
||||
if (unmatchedRequiredProperties.Count > 0)
|
||||
{
|
||||
RaiseError("Required properties are missing from object: {0}.".FormatWith(CultureInfo.InvariantCulture, string.Join(", ", unmatchedRequiredProperties.ToArray())), schema);
|
||||
}
|
||||
IEnumerable<string> unmatchedRequiredProperties = requiredProperties.Where(kv => !kv.Value).Select(kv => kv.Key);
|
||||
RaiseError("Required properties are missing from object: {0}.".FormatWith(CultureInfo.InvariantCulture, string.Join(", ", unmatchedRequiredProperties
|
||||
#if NET20 || NET35
|
||||
.ToArray()
|
||||
#endif
|
||||
)), schema);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -39,7 +39,11 @@ namespace Newtonsoft.Json.Linq.JsonPath
|
||||
{
|
||||
if (errorWhenNoMatch)
|
||||
{
|
||||
throw new JsonException("Properties {0} not valid on {1}.".FormatWith(CultureInfo.InvariantCulture, string.Join(", ", Names.Select(n => "'" + n + "'").ToArray()), t.GetType().Name));
|
||||
throw new JsonException("Properties {0} not valid on {1}.".FormatWith(CultureInfo.InvariantCulture, string.Join(", ", Names.Select(n => "'" + n + "'")
|
||||
#if NET20 || NET35
|
||||
.ToArray()
|
||||
#endif
|
||||
), t.GetType().Name));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -75,7 +75,11 @@ namespace Newtonsoft.Json.Schema
|
||||
|
||||
public static string GetId(IEnumerable<JsonSchema> schemata)
|
||||
{
|
||||
return string.Join("-", schemata.Select(s => s.InternalId).OrderBy(id => id, StringComparer.Ordinal).ToArray());
|
||||
return string.Join("-", schemata.Select(s => s.InternalId).OrderBy(id => id, StringComparer.Ordinal)
|
||||
#if NET20 || NET35
|
||||
.ToArray()
|
||||
#endif
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -216,35 +216,34 @@ namespace Newtonsoft.Json.Schema
|
||||
|
||||
private void WriteType(string propertyName, JsonWriter writer, JsonSchemaType type)
|
||||
{
|
||||
IList<JsonSchemaType> types;
|
||||
if (System.Enum.IsDefined(typeof(JsonSchemaType), type))
|
||||
if (Enum.IsDefined(typeof(JsonSchemaType), type))
|
||||
{
|
||||
types = new List<JsonSchemaType> { type };
|
||||
writer.WritePropertyName(propertyName);
|
||||
writer.WriteValue(JsonSchemaBuilder.MapType(type));
|
||||
}
|
||||
else
|
||||
{
|
||||
types = EnumUtils.GetFlagsValues(type).Where(v => v != JsonSchemaType.None).ToList();
|
||||
IEnumerator<JsonSchemaType> en = EnumUtils.GetFlagsValues(type).Where(v => v != JsonSchemaType.None).GetEnumerator();
|
||||
if (en.MoveNext())
|
||||
{
|
||||
writer.WritePropertyName(propertyName);
|
||||
JsonSchemaType first = en.Current;
|
||||
if (en.MoveNext())
|
||||
{
|
||||
writer.WriteStartArray();
|
||||
writer.WriteValue(JsonSchemaBuilder.MapType(first));
|
||||
do
|
||||
{
|
||||
writer.WriteValue(JsonSchemaBuilder.MapType(en.Current));
|
||||
} while (en.MoveNext());
|
||||
writer.WriteEndArray();
|
||||
}
|
||||
else
|
||||
{
|
||||
writer.WriteValue(JsonSchemaBuilder.MapType(first));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (types.Count == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
writer.WritePropertyName(propertyName);
|
||||
|
||||
if (types.Count == 1)
|
||||
{
|
||||
writer.WriteValue(JsonSchemaBuilder.MapType(types[0]));
|
||||
return;
|
||||
}
|
||||
|
||||
writer.WriteStartArray();
|
||||
foreach (JsonSchemaType jsonSchemaType in types)
|
||||
{
|
||||
writer.WriteValue(JsonSchemaBuilder.MapType(jsonSchemaType));
|
||||
}
|
||||
writer.WriteEndArray();
|
||||
}
|
||||
|
||||
private void WritePropertyIfNotNull(JsonWriter writer, string propertyName, object value)
|
||||
|
||||
@@ -285,8 +285,8 @@ namespace Newtonsoft.Json.Serialization
|
||||
|
||||
MemberSerialization memberSerialization = JsonTypeReflector.GetObjectMemberSerialization(objectType, ignoreSerializableAttribute);
|
||||
|
||||
List<MemberInfo> allMembers = ReflectionUtils.GetFieldsAndProperties(objectType, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static)
|
||||
.Where(m => !ReflectionUtils.IsIndexedProperty(m)).ToList();
|
||||
IEnumerable<MemberInfo> allMembers = ReflectionUtils.GetFieldsAndProperties(objectType, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static)
|
||||
.Where(m => !ReflectionUtils.IsIndexedProperty(m));
|
||||
|
||||
List<MemberInfo> serializableMembers = new List<MemberInfo>();
|
||||
|
||||
@@ -623,15 +623,17 @@ namespace Newtonsoft.Json.Serialization
|
||||
|
||||
private ConstructorInfo GetAttributeConstructor(Type objectType)
|
||||
{
|
||||
IList<ConstructorInfo> markedConstructors = objectType.GetConstructors(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).Where(c => c.IsDefined(typeof(JsonConstructorAttribute), true)).ToList();
|
||||
IEnumerator<ConstructorInfo> en = objectType.GetConstructors(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).Where(c => c.IsDefined(typeof(JsonConstructorAttribute), true)).GetEnumerator();
|
||||
|
||||
if (markedConstructors.Count > 1)
|
||||
if (en.MoveNext())
|
||||
{
|
||||
throw new JsonException("Multiple constructors with the JsonConstructorAttribute.");
|
||||
}
|
||||
else if (markedConstructors.Count == 1)
|
||||
{
|
||||
return markedConstructors[0];
|
||||
ConstructorInfo conInfo = en.Current;
|
||||
if (en.MoveNext())
|
||||
{
|
||||
throw new JsonException("Multiple constructors with the JsonConstructorAttribute.");
|
||||
}
|
||||
|
||||
return conInfo;
|
||||
}
|
||||
|
||||
// little hack to get Version objects to deserialize correctly
|
||||
@@ -645,16 +647,25 @@ namespace Newtonsoft.Json.Serialization
|
||||
|
||||
private ConstructorInfo GetParameterizedConstructor(Type objectType)
|
||||
{
|
||||
IList<ConstructorInfo> constructors = objectType.GetConstructors(BindingFlags.Public | BindingFlags.Instance).ToList();
|
||||
|
||||
if (constructors.Count == 1)
|
||||
#if PORTABLE
|
||||
IEnumerable<ConstructorInfo> constructors = objectType.GetConstructors(BindingFlags.Public | BindingFlags.Instance);
|
||||
IEnumerator<ConstructorInfo> en = constructors.GetEnumerator();
|
||||
if (en.MoveNext())
|
||||
{
|
||||
ConstructorInfo conInfo = en.Current;
|
||||
if (!en.MoveNext())
|
||||
{
|
||||
return conInfo;
|
||||
}
|
||||
}
|
||||
#else
|
||||
ConstructorInfo[] constructors = objectType.GetConstructors(BindingFlags.Public | BindingFlags.Instance);
|
||||
if (constructors.Length == 1)
|
||||
{
|
||||
return constructors[0];
|
||||
}
|
||||
else
|
||||
{
|
||||
return null;
|
||||
}
|
||||
#endif
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -1898,7 +1898,11 @@ namespace Newtonsoft.Json.Serialization
|
||||
|
||||
if (TraceWriter != null && TraceWriter.LevelFilter >= TraceLevel.Info)
|
||||
{
|
||||
string parameters = string.Join(", ", contract.CreatorParameters.Select(p => p.PropertyName).ToArray());
|
||||
string parameters = string.Join(", ", contract.CreatorParameters.Select(p => p.PropertyName)
|
||||
#if NET20 || NET35
|
||||
.ToArray()
|
||||
#endif
|
||||
);
|
||||
TraceWriter.Trace(TraceLevel.Info, JsonPosition.FormatMessage(reader as IJsonLineInfo, reader.Path, "Deserializing {0} using creator with parameters: {1}.".FormatWith(CultureInfo.InvariantCulture, contract.UnderlyingType, parameters)), null);
|
||||
}
|
||||
|
||||
|
||||
@@ -659,16 +659,15 @@ namespace Newtonsoft.Json.Utilities
|
||||
foreach (IGrouping<string, MemberInfo> groupedMember in targetMembers.GroupBy(m => m.Name))
|
||||
{
|
||||
int count = groupedMember.Count();
|
||||
IList<MemberInfo> members = groupedMember.ToList();
|
||||
|
||||
if (count == 1)
|
||||
{
|
||||
distinctMembers.Add(members.First());
|
||||
distinctMembers.Add(groupedMember.First());
|
||||
}
|
||||
else
|
||||
{
|
||||
IList<MemberInfo> resolvedMembers = new List<MemberInfo>();
|
||||
foreach (MemberInfo memberInfo in members)
|
||||
foreach (MemberInfo memberInfo in groupedMember)
|
||||
{
|
||||
// this is a bit hacky
|
||||
// if the hiding property is hiding a base property and it is virtual
|
||||
|
||||
Reference in New Issue
Block a user