-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
@@ -1013,5 +1013,56 @@ namespace Newtonsoft.Json.Tests.Bson
Assert.IsFalse(reader.Read());
}
[Test]
public void CanRoundTripStackOverflowData()
{
var doc =
@"{
""AboutMe"": ""<p>I'm the Director for Research and Development for <a href=\""http://www.prophoenix.com\"" rel=\""nofollow\"">ProPhoenix</a>, a public safety software company. This position allows me to investigate new and existing technologies and incorporate them into our product line, with the end goal being to help public safety agencies to do their jobs more effeciently and safely.</p>\r\n\r\n<p>I'm an advocate for PowerShell, as I believe it encourages administrative best practices and allows developers to provide additional access to their applications, without needing to explicity write code for each administrative feature. Part of my advocacy for PowerShell includes <a href=\""http://blog.usepowershell.com\"" rel=\""nofollow\"">my blog</a>, appearances on various podcasts, and acting as a Community Director for <a href=\""http://powershellcommunity.org\"" rel=\""nofollow\"">PowerShellCommunity.Org</a></p>\r\n\r\n<p>I’m also a co-host of Mind of Root (a weekly audio podcast about systems administration, tech news, and topics).</p>\r\n"",
""WebsiteUrl"": ""http://blog.usepowershell.com""
}";
JObject parsed = JObject.Parse(doc);
var memoryStream = new MemoryStream();
var bsonWriter = new BsonWriter(memoryStream);
parsed.WriteTo(bsonWriter);
bsonWriter.Flush();
memoryStream.Position = 0;
BsonReader reader = new BsonReader(memoryStream);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.StartObject, reader.TokenType);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
Assert.AreEqual("AboutMe", reader.Value);
Assert.AreEqual(typeof(string), reader.ValueType);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.String, reader.TokenType);
Assert.AreEqual(@"<p>I'm the Director for Research and Development for <a href=""http://www.prophoenix.com"" rel=""nofollow"">ProPhoenix</a>, a public safety software company. This position allows me to investigate new and existing technologies and incorporate them into our product line, with the end goal being to help public safety agencies to do their jobs more effeciently and safely.</p>
<p>I'm an advocate for PowerShell, as I believe it encourages administrative best practices and allows developers to provide additional access to their applications, without needing to explicity write code for each administrative feature. Part of my advocacy for PowerShell includes <a href=""http://blog.usepowershell.com"" rel=""nofollow"">my blog</a>, appearances on various podcasts, and acting as a Community Director for <a href=""http://powershellcommunity.org"" rel=""nofollow"">PowerShellCommunity.Org</a></p>
<p>I’m also a co-host of Mind of Root (a weekly audio podcast about systems administration, tech news, and topics).</p>
", reader.Value);
Assert.AreEqual(typeof(string), reader.ValueType);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
Assert.AreEqual("WebsiteUrl", reader.Value);
Assert.AreEqual(typeof(string), reader.ValueType);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.String, reader.TokenType);
Assert.AreEqual("http://blog.usepowershell.com", reader.Value);
Assert.AreEqual(typeof(string), reader.ValueType);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.EndObject, reader.TokenType);
Assert.IsFalse(reader.Read());
}
}
}
+51 -9
View File
@@ -14,6 +14,7 @@ using System.Text;
using Newtonsoft.Json.Bson;
using System.Runtime.Serialization.Formatters.Binary;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
namespace Newtonsoft.Json.Tests
{
@@ -442,17 +443,17 @@ namespace Newtonsoft.Json.Tests
switch (method)
{
case SerializeMethod.JsonNet:
return DeserializeJsonNet<T>((string) json);
return DeserializeJsonNet<T>((string)json);
case SerializeMethod.JsonNetBinary:
return DeserializeJsonNetBinary<T>((byte[]) json);
return DeserializeJsonNetBinary<T>((byte[])json);
case SerializeMethod.BinaryFormatter:
return DeserializeBinaryFormatter<T>((byte[]) json);
return DeserializeBinaryFormatter<T>((byte[])json);
case SerializeMethod.JavaScriptSerializer:
return DeserializeWebExtensions<T>((string) json);
return DeserializeWebExtensions<T>((string)json);
case SerializeMethod.DataContractSerializer:
return DeserializeDataContract<T>((string) json);
return DeserializeDataContract<T>((string)json);
case SerializeMethod.DataContractJsonSerializer:
return DeserializeDataContractJson<T>((string) json);
return DeserializeDataContractJson<T>((string)json);
default:
throw new ArgumentOutOfRangeException("method");
}
@@ -462,8 +463,8 @@ namespace Newtonsoft.Json.Tests
{
MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(xml));
DataContractSerializer serializer = new DataContractSerializer(typeof (T));
return (T) serializer.ReadObject(ms);
DataContractSerializer serializer = new DataContractSerializer(typeof(T));
return (T)serializer.ReadObject(ms);
}
private T DeserializeBinaryFormatter<T>(byte[] bytes)
@@ -487,7 +488,7 @@ namespace Newtonsoft.Json.Tests
Integer = int.MaxValue,
Text = "The quick red fox jumped over the lazy dog."
};
if (rootValue == null)
rootValue = currentValue;
if (parentValue != null)
@@ -498,6 +499,47 @@ namespace Newtonsoft.Json.Tests
BenchmarkSerializeMethod(SerializeMethod.JsonNetBinary, rootValue);
}
[Test]
public void JObjectToString()
{
JObject test = JObject.Parse(JsonText);
IsoDateTimeConverter isoDateTimeConverter = null;// = new IsoDateTimeConverter();
TimeOperation<object>(() =>
{
for (int i = 0; i < 5000; i++)
{
test["dummy"] = new JValue(i);
Encoding.UTF8.GetBytes(test.ToString(Formatting.None));
}
return null;
}, "JObject.ToString");
}
[Test]
public void JObjectToString2()
{
JObject test = JObject.Parse(JsonText);
IsoDateTimeConverter isoDateTimeConverter = null;// = new IsoDateTimeConverter();
MemoryStream ms = new MemoryStream();
TimeOperation<object>(() =>
{
for (int i = 0; i < 5000; i++)
{
test["dummy"] = new JValue(i);
ms.Seek(0, SeekOrigin.Begin);
JsonTextWriter jsonTextWriter = new JsonTextWriter(new StreamWriter(ms));
test.WriteTo(jsonTextWriter);
jsonTextWriter.Flush();
ms.ToArray();
//Encoding.UTF8.GetBytes(test.ToString(Formatting.None));
}
return null;
}, "JObject.ToString");
}
}
public class LargeRecursiveTestClass
+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)]