diff --git a/Src/Newtonsoft.Json/Converters/DataTableConverter.cs b/Src/Newtonsoft.Json/Converters/DataTableConverter.cs index fcac67bc..83769204 100644 --- a/Src/Newtonsoft.Json/Converters/DataTableConverter.cs +++ b/Src/Newtonsoft.Json/Converters/DataTableConverter.cs @@ -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; } diff --git a/Src/Newtonsoft.Json/JsonValidatingReader.cs b/Src/Newtonsoft.Json/JsonValidatingReader.cs index 259be33e..fef13e85 100644 --- a/Src/Newtonsoft.Json/JsonValidatingReader.cs +++ b/Src/Newtonsoft.Json/JsonValidatingReader.cs @@ -710,15 +710,14 @@ namespace Newtonsoft.Json Dictionary requiredProperties = _currentScope.RequiredProperties; - if (requiredProperties != null) + if (requiredProperties != null && requiredProperties.Values.Any(v => !v)) { - List 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 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); } } diff --git a/Src/Newtonsoft.Json/Linq/JsonPath/FieldMultipleFilter.cs b/Src/Newtonsoft.Json/Linq/JsonPath/FieldMultipleFilter.cs index 43bffbcc..d24818ff 100644 --- a/Src/Newtonsoft.Json/Linq/JsonPath/FieldMultipleFilter.cs +++ b/Src/Newtonsoft.Json/Linq/JsonPath/FieldMultipleFilter.cs @@ -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)); } } } diff --git a/Src/Newtonsoft.Json/Schema/JsonSchemaNode.cs b/Src/Newtonsoft.Json/Schema/JsonSchemaNode.cs index 05ca2deb..7ace75c1 100644 --- a/Src/Newtonsoft.Json/Schema/JsonSchemaNode.cs +++ b/Src/Newtonsoft.Json/Schema/JsonSchemaNode.cs @@ -75,7 +75,11 @@ namespace Newtonsoft.Json.Schema public static string GetId(IEnumerable 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 + ); } } } \ No newline at end of file diff --git a/Src/Newtonsoft.Json/Schema/JsonSchemaWriter.cs b/Src/Newtonsoft.Json/Schema/JsonSchemaWriter.cs index c4d64edc..737ab071 100644 --- a/Src/Newtonsoft.Json/Schema/JsonSchemaWriter.cs +++ b/Src/Newtonsoft.Json/Schema/JsonSchemaWriter.cs @@ -216,35 +216,34 @@ namespace Newtonsoft.Json.Schema private void WriteType(string propertyName, JsonWriter writer, JsonSchemaType type) { - IList types; - if (System.Enum.IsDefined(typeof(JsonSchemaType), type)) + if (Enum.IsDefined(typeof(JsonSchemaType), type)) { - types = new List { type }; + writer.WritePropertyName(propertyName); + writer.WriteValue(JsonSchemaBuilder.MapType(type)); } else { - types = EnumUtils.GetFlagsValues(type).Where(v => v != JsonSchemaType.None).ToList(); + IEnumerator 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) diff --git a/Src/Newtonsoft.Json/Serialization/DefaultContractResolver.cs b/Src/Newtonsoft.Json/Serialization/DefaultContractResolver.cs index 733b7269..fe865785 100644 --- a/Src/Newtonsoft.Json/Serialization/DefaultContractResolver.cs +++ b/Src/Newtonsoft.Json/Serialization/DefaultContractResolver.cs @@ -285,8 +285,8 @@ namespace Newtonsoft.Json.Serialization MemberSerialization memberSerialization = JsonTypeReflector.GetObjectMemberSerialization(objectType, ignoreSerializableAttribute); - List allMembers = ReflectionUtils.GetFieldsAndProperties(objectType, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static) - .Where(m => !ReflectionUtils.IsIndexedProperty(m)).ToList(); + IEnumerable allMembers = ReflectionUtils.GetFieldsAndProperties(objectType, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static) + .Where(m => !ReflectionUtils.IsIndexedProperty(m)); List serializableMembers = new List(); @@ -623,15 +623,17 @@ namespace Newtonsoft.Json.Serialization private ConstructorInfo GetAttributeConstructor(Type objectType) { - IList markedConstructors = objectType.GetConstructors(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).Where(c => c.IsDefined(typeof(JsonConstructorAttribute), true)).ToList(); + IEnumerator 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 constructors = objectType.GetConstructors(BindingFlags.Public | BindingFlags.Instance).ToList(); - - if (constructors.Count == 1) +#if PORTABLE + IEnumerable constructors = objectType.GetConstructors(BindingFlags.Public | BindingFlags.Instance); + IEnumerator 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; } /// diff --git a/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs b/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs index 2bd45965..c0d7577a 100644 --- a/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs +++ b/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs @@ -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); } diff --git a/Src/Newtonsoft.Json/Utilities/ReflectionUtils.cs b/Src/Newtonsoft.Json/Utilities/ReflectionUtils.cs index 286bbe1e..63b46f79 100644 --- a/Src/Newtonsoft.Json/Utilities/ReflectionUtils.cs +++ b/Src/Newtonsoft.Json/Utilities/ReflectionUtils.cs @@ -659,16 +659,15 @@ namespace Newtonsoft.Json.Utilities foreach (IGrouping groupedMember in targetMembers.GroupBy(m => m.Name)) { int count = groupedMember.Count(); - IList members = groupedMember.ToList(); if (count == 1) { - distinctMembers.Add(members.First()); + distinctMembers.Add(groupedMember.First()); } else { IList resolvedMembers = new List(); - 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