diff --git a/Src/Newtonsoft.Json/Linq/JArray.cs b/Src/Newtonsoft.Json/Linq/JArray.cs index 8c34688e..e8617d50 100644 --- a/Src/Newtonsoft.Json/Linq/JArray.cs +++ b/Src/Newtonsoft.Json/Linq/JArray.cs @@ -39,7 +39,7 @@ namespace Newtonsoft.Json.Linq /// public class JArray : JContainer, IList { - private readonly IList _values = new List(); + private readonly List _values = new List(); /// /// Gets the container's children tokens. @@ -188,9 +188,9 @@ namespace Newtonsoft.Json.Linq { writer.WriteStartArray(); - foreach (JToken token in _values) + for (int i = 0; i < _values.Count; i++) { - token.WriteTo(writer, converters); + _values[i].WriteTo(writer, converters); } writer.WriteEndArray(); diff --git a/Src/Newtonsoft.Json/Linq/JConstructor.cs b/Src/Newtonsoft.Json/Linq/JConstructor.cs index bb37617b..30bba5e4 100644 --- a/Src/Newtonsoft.Json/Linq/JConstructor.cs +++ b/Src/Newtonsoft.Json/Linq/JConstructor.cs @@ -36,7 +36,7 @@ namespace Newtonsoft.Json.Linq public class JConstructor : JContainer { private string _name; - private readonly IList _values = new List(); + private readonly List _values = new List(); /// /// Gets the container's children tokens. diff --git a/Src/Newtonsoft.Json/Linq/JObject.cs b/Src/Newtonsoft.Json/Linq/JObject.cs index 6fe911f2..b2bf7f9b 100644 --- a/Src/Newtonsoft.Json/Linq/JObject.cs +++ b/Src/Newtonsoft.Json/Linq/JObject.cs @@ -370,9 +370,9 @@ namespace Newtonsoft.Json.Linq { writer.WriteStartObject(); - foreach (JProperty property in _properties) + for (int i = 0; i < _properties.Count; i++) { - property.WriteTo(writer, converters); + _properties[i].WriteTo(writer, converters); } writer.WriteEndObject(); diff --git a/Src/Newtonsoft.Json/Linq/JValue.cs b/Src/Newtonsoft.Json/Linq/JValue.cs index 88a7dd04..b4b37b32 100644 --- a/Src/Newtonsoft.Json/Linq/JValue.cs +++ b/Src/Newtonsoft.Json/Linq/JValue.cs @@ -511,10 +511,20 @@ namespace Newtonsoft.Json.Linq /// A collection of which will be used when writing the token. public override void WriteTo(JsonWriter writer, params JsonConverter[] converters) { + if (converters != null && converters.Length > 0 && _value != null) + { + JsonConverter matchingConverter = JsonSerializer.GetMatchingConverter(converters, _value.GetType()); + if (matchingConverter != null) + { + matchingConverter.WriteJson(writer, _value, new JsonSerializer()); + return; + } + } + switch (_valueType) { case JTokenType.Comment: - writer.WriteComment(_value.ToString()); + writer.WriteComment((_value != null) ? _value.ToString() : null); return; case JTokenType.Raw: writer.WriteRawValue((_value != null) ? _value.ToString() : null); @@ -525,20 +535,6 @@ namespace Newtonsoft.Json.Linq case JTokenType.Undefined: writer.WriteUndefined(); return; - } - - JsonConverter matchingConverter; - if (_value != null - && converters != null - && converters.Length > 0 - && ((matchingConverter = JsonSerializer.GetMatchingConverter(converters, _value.GetType())) != null)) - { - matchingConverter.WriteJson(writer, _value, new JsonSerializer()); - return; - } - - switch (_valueType) - { case JTokenType.Integer: writer.WriteValue(Convert.ToInt64(_value, CultureInfo.InvariantCulture)); return;