-Added missing files from Silverlight projects

-Fixed JsonWriter depth error when writing current JsonReader position
-Fixed JsonSerializer not fulling skipping properties when putting together constructor values
-Changed JsonSerializer so that property names and constructor params matching is no longer case sensitive
This commit is contained in:
JamesNK
2008-08-02 11:29:37 +00:00
parent 0ae1c636bd
commit f2aa4a2b40
10 changed files with 356 additions and 5 deletions
@@ -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")]
@@ -0,0 +1,133 @@
<Project ToolsVersion="3.5" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>9.0.21022</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{0D8C3C2E-62C6-4C93-9377-6F74DD6BFD93}</ProjectGuid>
<ProjectTypeGuids>{A1591282-1198-4647-A2B1-27E5FF5F6F3B};{fae04ec0-301f-11d3-bf4b-00c04f79efbc}</ProjectTypeGuids>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Newtonsoft.Json.Tests.Silverlight</RootNamespace>
<AssemblyName>Newtonsoft.Json.Tests.Silverlight</AssemblyName>
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
<SilverlightApplication>false</SilverlightApplication>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>ClientBin\</OutputPath>
<DefineConstants>DEBUG;TRACE;SILVERLIGHT</DefineConstants>
<NoStdLib>true</NoStdLib>
<NoConfig>true</NoConfig>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>ClientBin\</OutputPath>
<DefineConstants>TRACE;SILVERLIGHT</DefineConstants>
<NoStdLib>true</NoStdLib>
<NoConfig>true</NoConfig>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="nunit.framework, Version=2.4.7.0, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77, processorArchitecture=MSIL" />
<Reference Include="System.Runtime.Serialization">
<Private>True</Private>
</Reference>
<Reference Include="System.ServiceModel.Web">
<Private>True</Private>
</Reference>
<Reference Include="System.Windows">
<Private>True</Private>
</Reference>
<Reference Include="mscorlib" />
<Reference Include="system">
<Private>True</Private>
</Reference>
<Reference Include="System.Core">
<Private>True</Private>
</Reference>
<Reference Include="System.Xml">
<Private>True</Private>
</Reference>
<Reference Include="System.Windows.Browser">
<Private>True</Private>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="..\Newtonsoft.Json.Tests\Converters\IsoDateTimeConverterTests.cs">
<Link>Converters\IsoDateTimeConverterTests.cs</Link>
</Compile>
<Compile Include="..\Newtonsoft.Json.Tests\Converters\JavaScriptDateTimeConverterTests.cs">
<Link>Converters\JavaScriptDateTimeConverterTests.cs</Link>
</Compile>
<Compile Include="..\Newtonsoft.Json.Tests\Converters\XmlNodeConverterTest.cs">
<Link>Converters\XmlNodeConverterTest.cs</Link>
</Compile>
<Compile Include="..\Newtonsoft.Json.Tests\DateTimeTestClass.cs">
<Link>DateTimeTestClass.cs</Link>
</Compile>
<Compile Include="..\Newtonsoft.Json.Tests\JavaScriptConvertTest.cs">
<Link>JavaScriptConvertTest.cs</Link>
</Compile>
<Compile Include="..\Newtonsoft.Json.Tests\JsonSerializerTest.cs">
<Link>JsonSerializerTest.cs</Link>
</Compile>
<Compile Include="..\Newtonsoft.Json.Tests\JsonTextReaderTest.cs">
<Link>JsonTextReaderTest.cs</Link>
</Compile>
<Compile Include="..\Newtonsoft.Json.Tests\JsonTextWriterTest.cs">
<Link>JsonTextWriterTest.cs</Link>
</Compile>
<Compile Include="..\Newtonsoft.Json.Tests\JsonTokenReaderTest.cs">
<Link>JsonTokenReaderTest.cs</Link>
</Compile>
<Compile Include="..\Newtonsoft.Json.Tests\JsonTokenWriterTest.cs">
<Link>JsonTokenWriterTest.cs</Link>
</Compile>
<Compile Include="..\Newtonsoft.Json.Tests\Linq\JTokenTests.cs">
<Link>Linq\JTokenTests.cs</Link>
</Compile>
<Compile Include="..\Newtonsoft.Json.Tests\Linq\JValueTests.cs">
<Link>Linq\JValueTests.cs</Link>
</Compile>
<Compile Include="..\Newtonsoft.Json.Tests\Linq\LinqToJsonTest.cs">
<Link>Linq\LinqToJsonTest.cs</Link>
</Compile>
<Compile Include="..\Newtonsoft.Json.Tests\TestFixtureBase.cs">
<Link>TestFixtureBase.cs</Link>
</Compile>
<Compile Include="SilverlightTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<Content Include="lib\nunit.framework.dll" />
<Content Include="readme.txt" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Newtonsoft.Json.Silverlight\Newtonsoft.Json.Silverlight.csproj">
<Project>{DC3C6F3D-2CA1-4278-9B79-63770FB3EA2D}</Project>
<Name>Newtonsoft.Json.Silverlight</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath)\Microsoft\Silverlight\v2.0\Microsoft.Silverlight.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
<ProjectExtensions>
<VisualStudio>
<FlavorProperties GUID="{A1591282-1198-4647-A2B1-27E5FF5F6F3B}">
<SilverlightProjectProperties />
</FlavorProperties>
</VisualStudio>
</ProjectExtensions>
</Project>
@@ -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")]
@@ -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");
}
}
}
@@ -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 <jamie.cansdale@testdriven.net>
@@ -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<ArrayList>(jsonText);
Assert.AreEqual(3, o.Count);
Assert.AreEqual(3, ((JArray)o[2]).Count);
}
#endif
public class Name
{
public string personsName;
public List<PhoneNumber> pNumbers = new List<PhoneNumber>();
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<Name>(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<ConstructorCaseSensitivityClass>(json);
Assert.AreEqual("param1", deserialized.param1);
Assert.AreEqual("Param1", deserialized.Param1);
Assert.AreEqual("Param2", deserialized.Param2);
}
}
}
+4 -4
View File
@@ -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<ParameterInfo, object> 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:
+4 -1
View File
@@ -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)
@@ -348,5 +348,25 @@ namespace Newtonsoft.Json.Utilities
writer.Write(h3);
writer.Write(h4);
}
public static TSource ForgivingCaseSensitiveFind<TSource>(this IEnumerable<TSource> source, Func<TSource, string> 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();
}
}
}
}