diff --git a/Src/Newtonsoft.Json.Silverlight/Properties/AssemblyInfo.cs b/Src/Newtonsoft.Json.Silverlight/Properties/AssemblyInfo.cs new file mode 100644 index 00000000..4de3b9e1 --- /dev/null +++ b/Src/Newtonsoft.Json.Silverlight/Properties/AssemblyInfo.cs @@ -0,0 +1,37 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// 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. +[assembly: AssemblyTitle("Newtonsoft.Json.Silverlight")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("Newtonsoft.Json.Silverlight")] +[assembly: AssemblyCopyright("Copyright © 2008")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +[assembly: InternalsVisibleTo("Newtonsoft.Json.Tests.Silverlight")] + +// 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("662ae5be-35c7-474d-8947-45dbd63b76dc")] + +// 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("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/Src/Newtonsoft.Json.Tests.Silverlight/Newtonsoft.Json.Tests.Silverlight.csproj b/Src/Newtonsoft.Json.Tests.Silverlight/Newtonsoft.Json.Tests.Silverlight.csproj new file mode 100644 index 00000000..2bbef64b --- /dev/null +++ b/Src/Newtonsoft.Json.Tests.Silverlight/Newtonsoft.Json.Tests.Silverlight.csproj @@ -0,0 +1,133 @@ + + + Debug + AnyCPU + 9.0.21022 + 2.0 + {0D8C3C2E-62C6-4C93-9377-6F74DD6BFD93} + {A1591282-1198-4647-A2B1-27E5FF5F6F3B};{fae04ec0-301f-11d3-bf4b-00c04f79efbc} + Library + Properties + Newtonsoft.Json.Tests.Silverlight + Newtonsoft.Json.Tests.Silverlight + v3.5 + false + + + true + full + false + ClientBin\ + DEBUG;TRACE;SILVERLIGHT + true + true + prompt + 4 + + + pdbonly + true + ClientBin\ + TRACE;SILVERLIGHT + true + true + prompt + 4 + + + + + True + + + True + + + True + + + + True + + + True + + + True + + + True + + + + + Converters\IsoDateTimeConverterTests.cs + + + Converters\JavaScriptDateTimeConverterTests.cs + + + Converters\XmlNodeConverterTest.cs + + + DateTimeTestClass.cs + + + JavaScriptConvertTest.cs + + + JsonSerializerTest.cs + + + JsonTextReaderTest.cs + + + JsonTextWriterTest.cs + + + JsonTokenReaderTest.cs + + + JsonTokenWriterTest.cs + + + Linq\JTokenTests.cs + + + Linq\JValueTests.cs + + + Linq\LinqToJsonTest.cs + + + TestFixtureBase.cs + + + + + + + + + + + {DC3C6F3D-2CA1-4278-9B79-63770FB3EA2D} + Newtonsoft.Json.Silverlight + + + + + + + + + + + + \ No newline at end of file diff --git a/Src/Newtonsoft.Json.Tests.Silverlight/Properties/AssemblyInfo.cs b/Src/Newtonsoft.Json.Tests.Silverlight/Properties/AssemblyInfo.cs new file mode 100644 index 00000000..597881f1 --- /dev/null +++ b/Src/Newtonsoft.Json.Tests.Silverlight/Properties/AssemblyInfo.cs @@ -0,0 +1,35 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// 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. +[assembly: AssemblyTitle("Newtonsoft.Json.Tests.Silverlight")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("Newtonsoft.Json.Tests.Silverlight")] +[assembly: AssemblyCopyright("Copyright © 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("d4f36ccb-cde3-463c-9ef9-314f1e517610")] + +// 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("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/Src/Newtonsoft.Json.Tests.Silverlight/SilverlightTests.cs b/Src/Newtonsoft.Json.Tests.Silverlight/SilverlightTests.cs new file mode 100644 index 00000000..a0b7e3bb --- /dev/null +++ b/Src/Newtonsoft.Json.Tests.Silverlight/SilverlightTests.cs @@ -0,0 +1,18 @@ +using System; +using System.Reflection; +using NUnit.Framework; + +namespace SilverlightNUnitProject2 +{ + [TestFixture] + public class SilverlightTests + { + [Test] + public void SystemVersion() + { + Assembly systemAssembly = typeof(Uri).Assembly; + StringAssert.Contains("=2.0.5.0,", systemAssembly.FullName, + "Check we're testing a Silverlight 2.0 assembly"); + } + } +} diff --git a/Src/Newtonsoft.Json.Tests.Silverlight/lib/nunit.framework.dll b/Src/Newtonsoft.Json.Tests.Silverlight/lib/nunit.framework.dll new file mode 100644 index 00000000..d4e0b72a Binary files /dev/null and b/Src/Newtonsoft.Json.Tests.Silverlight/lib/nunit.framework.dll differ diff --git a/Src/Newtonsoft.Json.Tests.Silverlight/readme.txt b/Src/Newtonsoft.Json.Tests.Silverlight/readme.txt new file mode 100644 index 00000000..147624d3 --- /dev/null +++ b/Src/Newtonsoft.Json.Tests.Silverlight/readme.txt @@ -0,0 +1,22 @@ + +Silverlight NUnit Project Template +================================== + +NOTE: New references must be marked with... Copy Local: True. + +This project template is designed to work with TestDriven.Net 2.13 or greater. +http://www.testdriven.net/download.aspx + +To execute one or more unit tests use the 'Run Test(s)', 'Test With > Debugger' +or 'Test With > Coverage' commands. Tests will execute using the Silverlight +assemblies, but using the .NET 2.0 runtime. + +Individual methods can be executed using the Silverlight 2.0/CoreCLR runtime +using the 'Test With > silverlight' command. + +For more information see: +http://www.testdriven.net +http://weblogs.asp.net/nunitaddon + +Alternatively you can email: +Jamie Cansdale diff --git a/Src/Newtonsoft.Json.Tests/JsonSerializerTest.cs b/Src/Newtonsoft.Json.Tests/JsonSerializerTest.cs index 304d685a..51211b00 100644 --- a/Src/Newtonsoft.Json.Tests/JsonSerializerTest.cs +++ b/Src/Newtonsoft.Json.Tests/JsonSerializerTest.cs @@ -1014,5 +1014,88 @@ keyword such as type of business."" Assert.AreEqual(0, c.IgnoredField); Assert.AreEqual(99, c.Field); } + +#if !SILVERLIGHT + [Test] + public void SerializeArrayAsArrayList() + { + string jsonText = @"[3, ""somestring"",[1,2,3]]"; + ArrayList o = JavaScriptConvert.DeserializeObject(jsonText); + + Assert.AreEqual(3, o.Count); + Assert.AreEqual(3, ((JArray)o[2]).Count); + } +#endif + + public class Name + { + public string personsName; + + public List pNumbers = new List(); + + public Name(string personsName) + { + this.personsName = personsName; + } + } + + public class PhoneNumber + { + public string phoneNumber; + + public PhoneNumber(string phoneNumber) + { + this.phoneNumber = phoneNumber; + } + } + + [Test] + public void SerializeMemberGenericList() + { + Name name = new Name("The Idiot in Next To Me"); + + PhoneNumber p1 = new PhoneNumber("555-1212"); + PhoneNumber p2 = new PhoneNumber("444-1212"); + + name.pNumbers.Add(p1); + name.pNumbers.Add(p2); + + string json = JavaScriptConvert.SerializeObject(name); + + Name newName = JavaScriptConvert.DeserializeObject(json); + + Assert.AreEqual("The Idiot in Next To Me", newName.personsName); + + // not passed in as part of the constructor, values not deserialized + Assert.AreEqual(0, newName.pNumbers.Count); + } + + public class ConstructorCaseSensitivityClass + { + public string param1 { get; set; } + public string Param1 { get; set; } + public string Param2 { get; set; } + + public ConstructorCaseSensitivityClass(string param1, string Param1, string param2) + { + this.param1 = param1; + this.Param1 = Param1; + this.Param2 = param2; + } + } + + [Test] + public void ConstructorCaseSensitivity() + { + ConstructorCaseSensitivityClass c = new ConstructorCaseSensitivityClass("param1", "Param1", "Param2"); + + string json = JavaScriptConvert.SerializeObject(c); + + ConstructorCaseSensitivityClass deserialized = JavaScriptConvert.DeserializeObject(json); + + Assert.AreEqual("param1", deserialized.param1); + Assert.AreEqual("Param1", deserialized.Param1); + Assert.AreEqual("Param2", deserialized.Param2); + } } } \ No newline at end of file diff --git a/Src/Newtonsoft.Json/JsonSerializer.cs b/Src/Newtonsoft.Json/JsonSerializer.cs index 3238a4d5..7f7640be 100644 --- a/Src/Newtonsoft.Json/JsonSerializer.cs +++ b/Src/Newtonsoft.Json/JsonSerializer.cs @@ -495,6 +495,7 @@ namespace Newtonsoft.Json if (c == null) throw new JsonSerializationException("Could not find a public constructor for type {0}.".FormatWith(CultureInfo.InvariantCulture, objectType)); + // create a dictionary to put retrieved values into IDictionary constructorParameters = c.GetParameters().ToDictionary(p => p, p => (object)null); bool exit = false; @@ -504,16 +505,15 @@ namespace Newtonsoft.Json { case JsonToken.PropertyName: string memberName = reader.Value.ToString(); - ParameterInfo matchingConstructorParameter = constructorParameters - .Where(kv => kv.Key.Name == memberName) - .Select(kv => kv.Key) - .SingleOrDefault(); + ParameterInfo matchingConstructorParameter = constructorParameters.ForgivingCaseSensitiveFind(kv => kv.Key.Name, memberName).Key; if (!reader.Read()) throw new JsonSerializationException("Unexpected end when setting {0}'s value.".FormatWith(CultureInfo.InvariantCulture, memberName)); if (matchingConstructorParameter != null) constructorParameters[matchingConstructorParameter] = CreateObject(reader, matchingConstructorParameter.ParameterType, null); + else + reader.Skip(); break; case JsonToken.EndObject: diff --git a/Src/Newtonsoft.Json/JsonWriter.cs b/Src/Newtonsoft.Json/JsonWriter.cs index 856c5957..7414cd82 100644 --- a/Src/Newtonsoft.Json/JsonWriter.cs +++ b/Src/Newtonsoft.Json/JsonWriter.cs @@ -367,7 +367,10 @@ namespace Newtonsoft.Json throw MiscellaneousUtils.CreateArgumentOutOfRangeException("TokenType", reader.TokenType, "Unexpected token type."); } } - while (reader.Read() && (currentDepth - 1 < reader.Depth || (currentDepth == reader.Depth && !IsEndToken(reader.TokenType)))); + while ( + // stop if we have reached the end of the token being read + currentDepth - 1 < reader.Depth - (IsEndToken(reader.TokenType) ? 1 : 0) + && reader.Read()); } private bool IsEndToken(JsonToken token) diff --git a/Src/Newtonsoft.Json/Utilities/StringUtils.cs b/Src/Newtonsoft.Json/Utilities/StringUtils.cs index afbc6beb..35d3cf18 100644 --- a/Src/Newtonsoft.Json/Utilities/StringUtils.cs +++ b/Src/Newtonsoft.Json/Utilities/StringUtils.cs @@ -348,5 +348,25 @@ namespace Newtonsoft.Json.Utilities writer.Write(h3); writer.Write(h4); } + + public static TSource ForgivingCaseSensitiveFind(this IEnumerable source, Func valueSelector, string testValue) + { + if (source == null) + throw new ArgumentNullException("source"); + if (valueSelector == null) + throw new ArgumentNullException("valueSelector"); + + var caseInsensitiveResults = source.Where(s => string.Compare(valueSelector(s), testValue, StringComparison.OrdinalIgnoreCase) == 0); + if (caseInsensitiveResults.Count() <= 1) + { + return caseInsensitiveResults.SingleOrDefault(); + } + else + { + // multiple results returned. now filter using case sensitivity + var caseSensitiveResults = source.Where(s => string.Compare(valueSelector(s), testValue, StringComparison.Ordinal) == 0); + return caseSensitiveResults.SingleOrDefault(); + } + } } } \ No newline at end of file