-Performance

-Add CLSCompliant attributes
-Fix BSON string reading bug
This commit is contained in:
JamesNK
2010-05-11 07:49:02 +00:00
parent 6c817fee93
commit fc6c041196
13 changed files with 184 additions and 55 deletions
+1 -1
View File
@@ -577,7 +577,7 @@ namespace Newtonsoft.Json.Bson
{
int count = ((length - totalBytesRead) > MaxCharBytesSize - offset)
? MaxCharBytesSize - offset
: length - totalBytesRead;
: length - totalBytesRead - offset;
int byteCount = _reader.BaseStream.Read(_byteBuffer, offset, count);
+4
View File
@@ -228,6 +228,7 @@ namespace Newtonsoft.Json.Bson
/// Writes a <see cref="UInt32"/> value.
/// </summary>
/// <param name="value">The <see cref="UInt32"/> value to write.</param>
[CLSCompliant(false)]
public override void WriteValue(uint value)
{
if (value > int.MaxValue)
@@ -251,6 +252,7 @@ namespace Newtonsoft.Json.Bson
/// Writes a <see cref="UInt64"/> value.
/// </summary>
/// <param name="value">The <see cref="UInt64"/> value to write.</param>
[CLSCompliant(false)]
public override void WriteValue(ulong value)
{
if (value > long.MaxValue)
@@ -304,6 +306,7 @@ namespace Newtonsoft.Json.Bson
/// Writes a <see cref="UInt16"/> value.
/// </summary>
/// <param name="value">The <see cref="UInt16"/> value to write.</param>
[CLSCompliant(false)]
public override void WriteValue(ushort value)
{
base.WriteValue(value);
@@ -334,6 +337,7 @@ namespace Newtonsoft.Json.Bson
/// Writes a <see cref="SByte"/> value.
/// </summary>
/// <param name="value">The <see cref="SByte"/> value to write.</param>
[CLSCompliant(false)]
public override void WriteValue(sbyte value)
{
base.WriteValue(value);
+4
View File
@@ -255,6 +255,7 @@ namespace Newtonsoft.Json
/// </summary>
/// <param name="value">The value to convert.</param>
/// <returns>A JSON string representation of the <see cref="UInt16"/>.</returns>
[CLSCompliant(false)]
public static string ToString(ushort value)
{
return value.ToString(null, CultureInfo.InvariantCulture);
@@ -265,6 +266,7 @@ namespace Newtonsoft.Json
/// </summary>
/// <param name="value">The value to convert.</param>
/// <returns>A JSON string representation of the <see cref="UInt32"/>.</returns>
[CLSCompliant(false)]
public static string ToString(uint value)
{
return value.ToString(null, CultureInfo.InvariantCulture);
@@ -285,6 +287,7 @@ namespace Newtonsoft.Json
/// </summary>
/// <param name="value">The value to convert.</param>
/// <returns>A JSON string representation of the <see cref="UInt64"/>.</returns>
[CLSCompliant(false)]
public static string ToString(ulong value)
{
return value.ToString(null, CultureInfo.InvariantCulture);
@@ -341,6 +344,7 @@ namespace Newtonsoft.Json
/// </summary>
/// <param name="value">The value to convert.</param>
/// <returns>A JSON string representation of the <see cref="SByte"/>.</returns>
[CLSCompliant(false)]
public static string ToString(sbyte value)
{
return value.ToString(null, CultureInfo.InvariantCulture);
+4
View File
@@ -302,6 +302,7 @@ namespace Newtonsoft.Json
/// Writes a <see cref="UInt32"/> value.
/// </summary>
/// <param name="value">The <see cref="UInt32"/> value to write.</param>
[CLSCompliant(false)]
public override void WriteValue(uint value)
{
base.WriteValue(value);
@@ -322,6 +323,7 @@ namespace Newtonsoft.Json
/// Writes a <see cref="UInt64"/> value.
/// </summary>
/// <param name="value">The <see cref="UInt64"/> value to write.</param>
[CLSCompliant(false)]
public override void WriteValue(ulong value)
{
base.WriteValue(value);
@@ -372,6 +374,7 @@ namespace Newtonsoft.Json
/// Writes a <see cref="UInt16"/> value.
/// </summary>
/// <param name="value">The <see cref="UInt16"/> value to write.</param>
[CLSCompliant(false)]
public override void WriteValue(ushort value)
{
base.WriteValue(value);
@@ -402,6 +405,7 @@ namespace Newtonsoft.Json
/// Writes a <see cref="SByte"/> value.
/// </summary>
/// <param name="value">The <see cref="SByte"/> value to write.</param>
[CLSCompliant(false)]
public override void WriteValue(sbyte value)
{
base.WriteValue(value);
+8
View File
@@ -674,6 +674,7 @@ namespace Newtonsoft.Json
/// Writes a <see cref="UInt32"/> value.
/// </summary>
/// <param name="value">The <see cref="UInt32"/> value to write.</param>
[CLSCompliant(false)]
public virtual void WriteValue(uint value)
{
AutoComplete(JsonToken.Integer);
@@ -692,6 +693,7 @@ namespace Newtonsoft.Json
/// Writes a <see cref="UInt64"/> value.
/// </summary>
/// <param name="value">The <see cref="UInt64"/> value to write.</param>
[CLSCompliant(false)]
public virtual void WriteValue(ulong value)
{
AutoComplete(JsonToken.Integer);
@@ -737,6 +739,7 @@ namespace Newtonsoft.Json
/// Writes a <see cref="UInt16"/> value.
/// </summary>
/// <param name="value">The <see cref="UInt16"/> value to write.</param>
[CLSCompliant(false)]
public virtual void WriteValue(ushort value)
{
AutoComplete(JsonToken.Integer);
@@ -764,6 +767,7 @@ namespace Newtonsoft.Json
/// Writes a <see cref="SByte"/> value.
/// </summary>
/// <param name="value">The <see cref="SByte"/> value to write.</param>
[CLSCompliant(false)]
public virtual void WriteValue(sbyte value)
{
AutoComplete(JsonToken.Integer);
@@ -814,6 +818,7 @@ namespace Newtonsoft.Json
/// Writes a <see cref="Nullable{UInt32}"/> value.
/// </summary>
/// <param name="value">The <see cref="Nullable{UInt32}"/> value to write.</param>
[CLSCompliant(false)]
public virtual void WriteValue(uint? value)
{
if (value == null)
@@ -838,6 +843,7 @@ namespace Newtonsoft.Json
/// Writes a <see cref="Nullable{UInt64}"/> value.
/// </summary>
/// <param name="value">The <see cref="Nullable{UInt64}"/> value to write.</param>
[CLSCompliant(false)]
public virtual void WriteValue(ulong? value)
{
if (value == null)
@@ -898,6 +904,7 @@ namespace Newtonsoft.Json
/// Writes a <see cref="Nullable{UInt16}"/> value.
/// </summary>
/// <param name="value">The <see cref="Nullable{UInt16}"/> value to write.</param>
[CLSCompliant(false)]
public virtual void WriteValue(ushort? value)
{
if (value == null)
@@ -934,6 +941,7 @@ namespace Newtonsoft.Json
/// Writes a <see cref="Nullable{SByte}"/> value.
/// </summary>
/// <param name="value">The <see cref="Nullable{SByte}"/> value to write.</param>
[CLSCompliant(false)]
public virtual void WriteValue(sbyte? value)
{
if (value == null)
+1 -1
View File
@@ -236,7 +236,7 @@ namespace Newtonsoft.Json.Linq
return new JEnumerable<JToken>(ChildrenInternal());
}
private IEnumerable<JToken> ChildrenInternal()
internal IEnumerable<JToken> ChildrenInternal()
{
JToken first = First;
JToken current = first;
+1 -1
View File
@@ -315,7 +315,7 @@ namespace Newtonsoft.Json.Linq
{
writer.WriteStartObject();
foreach (JProperty property in Properties())
foreach (JProperty property in ChildrenInternal())
{
property.WriteTo(writer, converters);
}
+10
View File
@@ -645,6 +645,7 @@ namespace Newtonsoft.Json.Linq
/// </summary>
/// <param name="value">The value.</param>
/// <returns>The result of the conversion.</returns>
[CLSCompliant(false)]
public static explicit operator uint?(JToken value)
{
if (value == null)
@@ -662,6 +663,7 @@ namespace Newtonsoft.Json.Linq
/// </summary>
/// <param name="value">The value.</param>
/// <returns>The result of the conversion.</returns>
[CLSCompliant(false)]
public static explicit operator ulong?(JToken value)
{
if (value == null)
@@ -724,6 +726,7 @@ namespace Newtonsoft.Json.Linq
/// </summary>
/// <param name="value">The value.</param>
/// <returns>The result of the conversion.</returns>
[CLSCompliant(false)]
public static explicit operator uint(JToken value)
{
JValue v = EnsureValue(value);
@@ -738,6 +741,7 @@ namespace Newtonsoft.Json.Linq
/// </summary>
/// <param name="value">The value.</param>
/// <returns>The result of the conversion.</returns>
[CLSCompliant(false)]
public static explicit operator ulong(JToken value)
{
JValue v = EnsureValue(value);
@@ -852,6 +856,7 @@ namespace Newtonsoft.Json.Linq
/// </summary>
/// <param name="value">The value to create a <see cref="JValue"/> from.</param>
/// <returns>The <see cref="JValue"/> initialized with the specified value.</returns>
[CLSCompliant(false)]
public static implicit operator JToken(ushort value)
{
return new JValue(value);
@@ -922,6 +927,7 @@ namespace Newtonsoft.Json.Linq
/// </summary>
/// <param name="value">The value to create a <see cref="JValue"/> from.</param>
/// <returns>The <see cref="JValue"/> initialized with the specified value.</returns>
[CLSCompliant(false)]
public static implicit operator JToken(ushort? value)
{
return new JValue(value);
@@ -932,6 +938,7 @@ namespace Newtonsoft.Json.Linq
/// </summary>
/// <param name="value">The value to create a <see cref="JValue"/> from.</param>
/// <returns>The <see cref="JValue"/> initialized with the specified value.</returns>
[CLSCompliant(false)]
public static implicit operator JToken(uint? value)
{
return new JValue(value);
@@ -942,6 +949,7 @@ namespace Newtonsoft.Json.Linq
/// </summary>
/// <param name="value">The value to create a <see cref="JValue"/> from.</param>
/// <returns>The <see cref="JValue"/> initialized with the specified value.</returns>
[CLSCompliant(false)]
public static implicit operator JToken(ulong? value)
{
return new JValue(value);
@@ -982,6 +990,7 @@ namespace Newtonsoft.Json.Linq
/// </summary>
/// <param name="value">The value to create a <see cref="JValue"/> from.</param>
/// <returns>The <see cref="JValue"/> initialized with the specified value.</returns>
[CLSCompliant(false)]
public static implicit operator JToken(uint value)
{
return new JValue(value);
@@ -992,6 +1001,7 @@ namespace Newtonsoft.Json.Linq
/// </summary>
/// <param name="value">The value to create a <see cref="JValue"/> from.</param>
/// <returns>The <see cref="JValue"/> initialized with the specified value.</returns>
[CLSCompliant(false)]
public static implicit operator JToken(ulong value)
{
return new JValue(value);
+4
View File
@@ -217,6 +217,7 @@ namespace Newtonsoft.Json.Linq
/// Writes a <see cref="UInt32"/> value.
/// </summary>
/// <param name="value">The <see cref="UInt32"/> value to write.</param>
[CLSCompliant(false)]
public override void WriteValue(uint value)
{
base.WriteValue(value);
@@ -237,6 +238,7 @@ namespace Newtonsoft.Json.Linq
/// Writes a <see cref="UInt64"/> value.
/// </summary>
/// <param name="value">The <see cref="UInt64"/> value to write.</param>
[CLSCompliant(false)]
public override void WriteValue(ulong value)
{
base.WriteValue(value);
@@ -287,6 +289,7 @@ namespace Newtonsoft.Json.Linq
/// Writes a <see cref="UInt16"/> value.
/// </summary>
/// <param name="value">The <see cref="UInt16"/> value to write.</param>
[CLSCompliant(false)]
public override void WriteValue(ushort value)
{
base.WriteValue(value);
@@ -317,6 +320,7 @@ namespace Newtonsoft.Json.Linq
/// Writes a <see cref="SByte"/> value.
/// </summary>
/// <param name="value">The <see cref="SByte"/> value to write.</param>
[CLSCompliant(false)]
public override void WriteValue(sbyte value)
{
base.WriteValue(value);
+41 -42
View File
@@ -68,6 +68,7 @@ namespace Newtonsoft.Json.Linq
/// Initializes a new instance of the <see cref="JValue"/> class with the given value.
/// </summary>
/// <param name="value">The value.</param>
[CLSCompliant(false)]
public JValue(ulong value)
: this(value, JTokenType.Integer)
{
@@ -271,16 +272,6 @@ namespace Newtonsoft.Json.Linq
}
}
private static void WriteConvertableValue(JsonWriter writer, IList<JsonConverter> converters, Action<object> defaultWrite, object value)
{
JsonConverter matchingConverter;
if (value != null && ((matchingConverter = JsonSerializer.GetMatchingConverter(converters, value.GetType())) != null))
matchingConverter.WriteJson(writer, value, new JsonSerializer());
else
defaultWrite(value);
}
/// <summary>
/// Writes this token to a <see cref="JsonWriter"/>.
/// </summary>
@@ -292,45 +283,53 @@ namespace Newtonsoft.Json.Linq
{
case JTokenType.Comment:
writer.WriteComment(_value.ToString());
break;
case JTokenType.Integer:
WriteConvertableValue(writer, converters, v => writer.WriteValue(Convert.ToInt64(v, CultureInfo.InvariantCulture)), _value);
break;
case JTokenType.Float:
WriteConvertableValue(writer, converters, v => writer.WriteValue(Convert.ToDouble(v, CultureInfo.InvariantCulture)), _value);
break;
case JTokenType.String:
WriteConvertableValue(writer, converters, v => writer.WriteValue((v != null) ? v.ToString() : null), _value);
break;
case JTokenType.Boolean:
WriteConvertableValue(writer, converters, v => writer.WriteValue(Convert.ToBoolean(v, CultureInfo.InvariantCulture)), _value);
break;
case JTokenType.Date:
WriteConvertableValue(writer, converters, v =>
{
#if !PocketPC && !NET20
if (v is DateTimeOffset)
writer.WriteValue((DateTimeOffset)v);
else
#endif
writer.WriteValue(Convert.ToDateTime(v, CultureInfo.InvariantCulture));
}, _value);
break;
return;
case JTokenType.Raw:
writer.WriteRawValue((_value != null) ? _value.ToString() : null);
break;
case JTokenType.Bytes:
WriteConvertableValue(writer, converters, v => writer.WriteValue((byte[])v), _value);
break;
return;
case JTokenType.Null:
writer.WriteNull();
break;
return;
case JTokenType.Undefined:
writer.WriteUndefined();
break;
default:
throw MiscellaneousUtils.CreateArgumentOutOfRangeException("TokenType", _valueType, "Unexpected token type.");
return;
}
JsonConverter matchingConverter;
if (_value != null && ((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;
case JTokenType.Float:
writer.WriteValue(Convert.ToDouble(_value, CultureInfo.InvariantCulture));
return;
case JTokenType.String:
writer.WriteValue((_value != null) ? _value.ToString() : null);
return;
case JTokenType.Boolean:
writer.WriteValue(Convert.ToBoolean(_value, CultureInfo.InvariantCulture));
return;
case JTokenType.Date:
#if !PocketPC && !NET20
if (_value is DateTimeOffset)
writer.WriteValue((DateTimeOffset)_value);
else
#endif
writer.WriteValue(Convert.ToDateTime(_value, CultureInfo.InvariantCulture)); ;
return;
case JTokenType.Bytes:
writer.WriteValue((byte[])_value);
return;
}
throw MiscellaneousUtils.CreateArgumentOutOfRangeException("TokenType", _valueType, "Unexpected token type.");
}
internal override int GetDeepHashCode()
@@ -17,6 +17,7 @@
// * Any of these conditions can be waived if you get permission from the copyright holder.
#endregion
using System;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
@@ -97,4 +98,6 @@ using System.Security;
[assembly: AssemblyVersion("3.5.0.0")]
#if !PocketPC
[assembly: AssemblyFileVersion("3.5.0.0")]
#endif
#endif
[assembly: CLSCompliant(true)]