-Performance improvements
This commit is contained in:
@@ -94,7 +94,7 @@ namespace Newtonsoft.Json.Converters
|
||||
private object ReadBson(BsonReader reader)
|
||||
{
|
||||
string regexText = (string) reader.Value;
|
||||
int patternOptionDelimiterIndex = regexText.LastIndexOf(@"/");
|
||||
int patternOptionDelimiterIndex = regexText.LastIndexOf('/');
|
||||
|
||||
string patternText = regexText.Substring(1, patternOptionDelimiterIndex - 1);
|
||||
string optionsText = regexText.Substring(patternOptionDelimiterIndex + 1);
|
||||
|
||||
@@ -311,7 +311,7 @@ namespace Newtonsoft.Json
|
||||
|
||||
private static string EnsureDecimalPlace(double value, string text)
|
||||
{
|
||||
if (double.IsNaN(value) || double.IsInfinity(value) || text.IndexOf('.') != -1 || text.IndexOf('E') != -1)
|
||||
if (double.IsNaN(value) || double.IsInfinity(value) || text.IndexOf('.') != -1 || text.IndexOf('E') != -1 || text.IndexOf('e') != -1)
|
||||
return text;
|
||||
|
||||
return text + ".0";
|
||||
|
||||
@@ -458,7 +458,9 @@ namespace Newtonsoft.Json
|
||||
|
||||
internal static JsonConverter GetMatchingConverter(IList<JsonConverter> converters, Type objectType)
|
||||
{
|
||||
#if DEBUG
|
||||
ValidationUtils.ArgumentNotNull(objectType, "objectType");
|
||||
#endif
|
||||
|
||||
if (converters != null)
|
||||
{
|
||||
|
||||
@@ -828,11 +828,17 @@ namespace Newtonsoft.Json
|
||||
object numberValue;
|
||||
JsonToken numberType;
|
||||
|
||||
bool nonBase10 = (firstChar == '0' && !number.StartsWith("0.", StringComparison.OrdinalIgnoreCase));
|
||||
bool singleDigit = (char.IsDigit(firstChar) && number.Length == 1);
|
||||
bool nonBase10 = (firstChar == '0' && number.Length > 1 && number[1] != '.');
|
||||
|
||||
if (_readType == ReadType.ReadAsDecimal)
|
||||
{
|
||||
if (nonBase10)
|
||||
if (singleDigit)
|
||||
{
|
||||
// digit char values start at 48
|
||||
numberValue = (decimal)firstChar - 48;
|
||||
}
|
||||
else if (nonBase10)
|
||||
{
|
||||
// decimal.Parse doesn't support parsing hexadecimal values
|
||||
long integer = number.StartsWith("0x", StringComparison.OrdinalIgnoreCase)
|
||||
@@ -850,14 +856,21 @@ namespace Newtonsoft.Json
|
||||
}
|
||||
else
|
||||
{
|
||||
if (nonBase10)
|
||||
if (singleDigit)
|
||||
{
|
||||
// digit char values start at 48
|
||||
numberValue = (long)firstChar - 48;
|
||||
numberType = JsonToken.Integer;
|
||||
}
|
||||
else if (nonBase10)
|
||||
{
|
||||
numberValue = number.StartsWith("0x", StringComparison.OrdinalIgnoreCase)
|
||||
? Convert.ToInt64(number, 16)
|
||||
: Convert.ToInt64(number, 8);
|
||||
numberType = JsonToken.Integer;
|
||||
}
|
||||
else if (number.IndexOf(".", StringComparison.OrdinalIgnoreCase) != -1 || number.IndexOf("e", StringComparison.OrdinalIgnoreCase) != -1)
|
||||
// it's faster to do 3 indexof with single characters than an indexofany
|
||||
else if (number.IndexOf('.') != -1 || number.IndexOf('E') != -1 || number.IndexOf('e') != -1)
|
||||
{
|
||||
numberValue = Convert.ToDouble(number, CultureInfo.InvariantCulture);
|
||||
numberType = JsonToken.Float;
|
||||
@@ -935,8 +948,7 @@ namespace Newtonsoft.Json
|
||||
break;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
while (i < value.Length && ((currentChar = MoveNext()) != '\0' || !_end));
|
||||
} while (i < value.Length && ((currentChar = MoveNext()) != '\0' || !_end));
|
||||
|
||||
return (i == value.Length);
|
||||
}
|
||||
|
||||
@@ -210,8 +210,6 @@ namespace Newtonsoft.Json
|
||||
/// Writes indent characters.
|
||||
/// </summary>
|
||||
protected override void WriteIndent()
|
||||
{
|
||||
if (Formatting == Formatting.Indented)
|
||||
{
|
||||
_writer.Write(Environment.NewLine);
|
||||
|
||||
@@ -223,7 +221,6 @@ namespace Newtonsoft.Json
|
||||
_writer.Write(_indentChar);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Writes the JSON value delimiter.
|
||||
|
||||
@@ -522,8 +522,11 @@ namespace Newtonsoft.Json
|
||||
{
|
||||
JsonToken token = GetCloseTokenForType(Pop());
|
||||
|
||||
if (_formatting == Formatting.Indented)
|
||||
{
|
||||
if (_currentState != State.ObjectStart && _currentState != State.ArrayStart)
|
||||
WriteIndent();
|
||||
}
|
||||
|
||||
WriteEnd(token);
|
||||
}
|
||||
@@ -616,6 +619,8 @@ namespace Newtonsoft.Json
|
||||
WriteIndentSpace();
|
||||
}
|
||||
|
||||
if (_formatting == Formatting.Indented)
|
||||
{
|
||||
WriteState writeState = WriteState;
|
||||
|
||||
// don't indent a property when it is the first token to be written (i.e. at the start)
|
||||
@@ -624,6 +629,7 @@ namespace Newtonsoft.Json
|
||||
{
|
||||
WriteIndent();
|
||||
}
|
||||
}
|
||||
|
||||
_currentState = newState;
|
||||
}
|
||||
@@ -1196,7 +1202,7 @@ namespace Newtonsoft.Json
|
||||
|
||||
private void Dispose(bool disposing)
|
||||
{
|
||||
if (WriteState != WriteState.Closed)
|
||||
if (_currentState != State.Closed)
|
||||
Close();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -43,7 +43,7 @@ namespace Newtonsoft.Json.Serialization
|
||||
/// Initializes a new instance of the <see cref="JsonPropertyCollection"/> class.
|
||||
/// </summary>
|
||||
/// <param name="type">The type.</param>
|
||||
public JsonPropertyCollection(Type type)
|
||||
public JsonPropertyCollection(Type type) : base(StringComparer.Ordinal)
|
||||
{
|
||||
ValidationUtils.ArgumentNotNull(type, "type");
|
||||
_type = type;
|
||||
@@ -100,6 +100,18 @@ namespace Newtonsoft.Json.Serialization
|
||||
return property;
|
||||
}
|
||||
|
||||
private bool TryGetValue(string key, out JsonProperty item)
|
||||
{
|
||||
if (Dictionary == null)
|
||||
{
|
||||
item = default(JsonProperty);
|
||||
return false;
|
||||
}
|
||||
|
||||
return Dictionary.TryGetValue(key, out item);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Gets a property by property name.
|
||||
/// </summary>
|
||||
@@ -108,6 +120,16 @@ namespace Newtonsoft.Json.Serialization
|
||||
/// <returns>A matching property if found.</returns>
|
||||
public JsonProperty GetProperty(string propertyName, StringComparison comparisonType)
|
||||
{
|
||||
// KeyedCollection has an ordinal comparer
|
||||
if (comparisonType == StringComparison.Ordinal)
|
||||
{
|
||||
JsonProperty property;
|
||||
if (TryGetValue(propertyName, out property))
|
||||
return property;
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
foreach (JsonProperty property in this)
|
||||
{
|
||||
if (string.Equals(propertyName, property.PropertyName, comparisonType))
|
||||
|
||||
@@ -480,7 +480,7 @@ namespace Newtonsoft.Json.Serialization
|
||||
|
||||
private bool HasDefinedType(Type type)
|
||||
{
|
||||
return (type != null && type != typeof (object) && !typeof (JToken).IsAssignableFrom(type)
|
||||
return (type != null && type != typeof (object) && !typeof (JToken).IsSubclassOf(type)
|
||||
#if !(NET35 || NET20 || WINDOWS_PHONE)
|
||||
&& type != typeof (IDynamicMetaObjectProvider)
|
||||
#endif
|
||||
|
||||
@@ -35,21 +35,29 @@ namespace Newtonsoft.Json.Utilities
|
||||
{
|
||||
internal static class JavaScriptUtils
|
||||
{
|
||||
public static void WriteEscapedJavaScriptString(TextWriter writer, string value, char delimiter, bool appendDelimiters)
|
||||
public static void WriteEscapedJavaScriptString(TextWriter writer, string s, char delimiter, bool appendDelimiters)
|
||||
{
|
||||
// leading delimiter
|
||||
if (appendDelimiters)
|
||||
writer.Write(delimiter);
|
||||
|
||||
if (value != null)
|
||||
if (s != null)
|
||||
{
|
||||
int lastWritePosition = 0;
|
||||
int skipped = 0;
|
||||
char[] chars = null;
|
||||
int lastWritePosition = -1;
|
||||
|
||||
for (int i = 0; i < value.Length; i++)
|
||||
for (var index = 0; index < s.Length; ++index)
|
||||
{
|
||||
char c = value[i];
|
||||
var c = s[index];
|
||||
|
||||
if (c >= ' ' && c < 128 && c != '\"' && c != '\\' && c != '\'')
|
||||
{
|
||||
if (lastWritePosition == -1)
|
||||
lastWritePosition = index;
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
string escapedValue;
|
||||
|
||||
switch (c)
|
||||
@@ -94,35 +102,39 @@ namespace Newtonsoft.Json.Utilities
|
||||
break;
|
||||
}
|
||||
|
||||
if (escapedValue != null)
|
||||
if (escapedValue == null)
|
||||
{
|
||||
if (lastWritePosition == -1)
|
||||
lastWritePosition = index;
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
if (lastWritePosition != -1)
|
||||
{
|
||||
if (chars == null)
|
||||
chars = value.ToCharArray();
|
||||
chars = s.ToCharArray();
|
||||
|
||||
// write skipped text
|
||||
if (skipped > 0)
|
||||
{
|
||||
writer.Write(chars, lastWritePosition, skipped);
|
||||
skipped = 0;
|
||||
writer.Write(chars, lastWritePosition, index - lastWritePosition);
|
||||
lastWritePosition = -1;
|
||||
}
|
||||
|
||||
// write escaped value and note position
|
||||
writer.Write(escapedValue);
|
||||
lastWritePosition = i + 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
skipped++;
|
||||
}
|
||||
}
|
||||
|
||||
// write any remaining skipped text
|
||||
if (skipped > 0)
|
||||
if (lastWritePosition != -1)
|
||||
{
|
||||
if (lastWritePosition == 0)
|
||||
writer.Write(value);
|
||||
{
|
||||
writer.Write(s);
|
||||
}
|
||||
else
|
||||
writer.Write(chars, lastWritePosition, skipped);
|
||||
{
|
||||
if (chars == null)
|
||||
chars = s.ToCharArray();
|
||||
|
||||
writer.Write(chars, lastWritePosition, s.Length - lastWritePosition);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user