-Fixed deserializing a null string throwing a NullReferenceException

-Fixed JsonTextReader reading from a slow stream
-Fixed CultureInfo not being overridden on JsonSerializerProxy
-Fixed full trust security check in .NET 2.0 & .NET 3.5
-Improved writing JSON with indent performance
This commit is contained in:
James Newton-King
2012-03-31 14:30:14 +13:00
parent 8471c4ffe1
commit a032f4bbf3
12 changed files with 300 additions and 178 deletions
@@ -114,6 +114,20 @@ namespace Newtonsoft.Json.Tests
}
#endif
[Test]
public void DeserializeObject_EmptyString()
{
object result = JsonConvert.DeserializeObject(string.Empty);
Assert.IsNull(result);
}
[Test]
public void DeserializeObject_Integer()
{
object result = JsonConvert.DeserializeObject("1");
Assert.AreEqual(1L, result);
}
[Test]
public void EscapeJavaScriptString()
{
+141 -5
View File
@@ -100,7 +100,7 @@ namespace Newtonsoft.Json.Tests
{
string json = @"{""DefaultConverter"":new Date(0, ""hi""),""MemberConverter"":""1970-01-01T00:00:00Z""}";
JsonReader reader = new JsonTextReader(new StringReader(json));
JsonReader reader = new JsonTextReader(new StreamReader(new SlowStream(json, new UTF8Encoding(false), 1)));
Assert.IsTrue(reader.Read());
Assert.IsTrue(reader.Read());
@@ -479,7 +479,7 @@ Parameter name: reader"
public void NullCharReading()
{
string json = "\0{\0'\0h\0i\0'\0:\0[\01\0,\0'\0'\0\0,\0null\0]\0,\0do\0:true\0}\0\0/*\0sd\0f\0*/\0/*\0sd\0f\0*/ \0";
JsonTextReader reader = new JsonTextReader(new StringReader(json));
JsonTextReader reader = new JsonTextReader(new StreamReader(new SlowStream(json, new UTF8Encoding(false), 1)));
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.StartObject, reader.TokenType);
@@ -710,7 +710,7 @@ Parameter name: reader"
count++;
}
JsonTextReader reader = new JsonTextReader(new StringReader(json));
JsonTextReader reader = new JsonTextReader(new StreamReader(new SlowStream(json, new UTF8Encoding(false), 1)));
Assert.IsTrue(reader.Read());
Assert.AreEqual(7, reader.LineNumber);
@@ -1966,7 +1966,7 @@ bye", reader.Value);
public void ParseContentDelimitedByNonStandardWhitespace()
{
string json = "\x00a0{\x00a0'h\x00a0i\x00a0'\x00a0:\x00a0[\x00a0true\x00a0,\x00a0new\x00a0Date\x00a0(\x00a0)\x00a0]\x00a0/*\x00a0comment\x00a0*/\x00a0}\x00a0";
JsonTextReader reader = new JsonTextReader(new StringReader(json));
JsonTextReader reader = new JsonTextReader(new StreamReader(new SlowStream(json, new UTF8Encoding(false), 1)));
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.StartObject, reader.TokenType);
@@ -2010,7 +2010,7 @@ bye", reader.Value);
""Sizes"":/*comment*/[/*comment*/
""Small""/*comment*/]/*comment*/}/*comment*/";
JsonTextReader reader = new JsonTextReader(new StringReader(json));
JsonTextReader reader = new JsonTextReader(new StreamReader(new SlowStream(json, new UTF8Encoding(false), 1)));
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.Comment, reader.TokenType);
@@ -2194,5 +2194,141 @@ bye", reader.Value);
reader.Read();
}
[Test]
public void ReadingFromSlowStream()
{
string json = "[false, true, true, false, 'test!', 1.11, 0e-10, 0E-10, 0.25e-5, 0.3e10, 6.0221418e23, 'Purple\\r \\n monkey\\'s:\\tdishwasher']";
JsonTextReader reader = new JsonTextReader(new StreamReader(new SlowStream(json, new UTF8Encoding(false), 1)));
Assert.IsTrue(reader.Read());
Assert.IsTrue(reader.Read());
Assert.AreEqual(false, reader.Value);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.Boolean, reader.TokenType);
Assert.AreEqual(true, reader.Value);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.Boolean, reader.TokenType);
Assert.AreEqual(true, reader.Value);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.Boolean, reader.TokenType);
Assert.AreEqual(false, reader.Value);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.String, reader.TokenType);
Assert.AreEqual("test!", reader.Value);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.Float, reader.TokenType);
Assert.AreEqual(1.11d, reader.Value);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.Float, reader.TokenType);
Assert.AreEqual(0d, reader.Value);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.Float, reader.TokenType);
Assert.AreEqual(0d, reader.Value);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.Float, reader.TokenType);
Assert.AreEqual(0.0000025d, reader.Value);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.Float, reader.TokenType);
Assert.AreEqual(3000000000d, reader.Value);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.Float, reader.TokenType);
Assert.AreEqual(602214180000000000000000d, reader.Value);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.String, reader.TokenType);
Assert.AreEqual(reader.Value, "Purple\r \n monkey's:\tdishwasher");
Assert.IsTrue(reader.Read());
}
}
public class SlowStream : Stream
{
byte[] bytes;
int totalBytesRead;
int bytesPerRead;
public SlowStream(byte[] content, int bytesPerRead)
{
this.bytes = content;
this.totalBytesRead = 0;
this.bytesPerRead = bytesPerRead;
}
public SlowStream(string content, Encoding encoding, int bytesPerRead)
: this(encoding.GetBytes(content), bytesPerRead)
{
}
public override bool CanRead
{
get { return true; }
}
public override bool CanSeek
{
get { return false; }
}
public override bool CanWrite
{
get { return false; }
}
public override void Flush()
{
}
public override long Length
{
get { throw new NotSupportedException(); }
}
public override long Position
{
get { throw new NotSupportedException(); }
set { throw new NotSupportedException(); }
}
public override int Read(byte[] buffer, int offset, int count)
{
int toReturn = Math.Min(count, this.bytesPerRead);
toReturn = Math.Min(toReturn, this.bytes.Length - this.totalBytesRead);
if (toReturn > 0)
{
Array.Copy(this.bytes, this.totalBytesRead, buffer, offset, toReturn);
}
this.totalBytesRead += toReturn;
return toReturn;
}
public override long Seek(long offset, SeekOrigin origin)
{
throw new NotSupportedException();
}
public override void SetLength(long value)
{
throw new NotSupportedException();
}
public override void Write(byte[] buffer, int offset, int count)
{
throw new NotSupportedException();
}
}
}
@@ -1,76 +0,0 @@
#region License
// Copyright (c) 2007 James Newton-King
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#endregion
using System.Reflection;
using System.Runtime.InteropServices;
using System.Security;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
#if WINDOWS_PHONE
[assembly: AssemblyTitle("Json.NET Tests Windows Phone")]
#elif SILVERLIGHT
[assembly: AssemblyTitle("Json.NET Tests Silverlight")]
#elif PocketPC
[assembly: AssemblyTitle("Json.NET Tests Compact")]
#elif NET20
[assembly: AssemblyTitle("Json.NET Tests .NET 2.0")]
#elif NET35
[assembly: AssemblyTitle("Json.NET Tests .NET 3.5")]
#else
[assembly: AssemblyTitle("Json.NET")]
#endif
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Newtonsoft")]
[assembly: AssemblyProduct("Json.NET Tests")]
[assembly: AssemblyCopyright("Copyright © James Newton-King 2008")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("0be3d72b-d2ef-409c-985c-d3ede89a25f1")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Revision and Build Numbers
// by using the '*' as shown below:
[assembly: AssemblyVersion("4.0.8.0")]
#if !PocketPC
[assembly: AssemblyFileVersion("4.0.8.14718")]
#endif
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
#if !NETFX_CORE
using NUnit.Framework;
@@ -58,9 +59,25 @@ namespace Newtonsoft.Json.Tests.Serialization
}
}
public class AddressWithDataMember
{
[DataMember(Name = "CustomerAddress1")]
public string AddressLine1 { get; set; }
}
[TestFixture]
public class ContractResolverTests : TestFixtureBase
{
[Test]
public void DeserializeDataMemberClassWithNoDataContract()
{
var resolver = new DefaultContractResolver();
var contract = (JsonObjectContract)resolver.ResolveContract(typeof(AddressWithDataMember));
Assert.AreEqual("AddressLine1", contract.Properties[0].PropertyName);
}
[Test]
public void ResolveProperties_IgnoreStatic()
{