Work in progress:
-Silverlight build projects -JObject now implements IDictionary, JArray now implements IList
This commit is contained in:
@@ -0,0 +1,225 @@
|
||||
<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>{DC3C6F3D-2CA1-4278-9B79-63770FB3EA2D}</ProjectGuid>
|
||||
<ProjectTypeGuids>{A1591282-1198-4647-A2B1-27E5FF5F6F3B};{fae04ec0-301f-11d3-bf4b-00c04f79efbc}</ProjectTypeGuids>
|
||||
<OutputType>Library</OutputType>
|
||||
<AppDesignerFolder>Properties</AppDesignerFolder>
|
||||
<RootNamespace>Newtonsoft.Json.Silverlight</RootNamespace>
|
||||
<AssemblyName>Newtonsoft.Json.Silverlight</AssemblyName>
|
||||
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
|
||||
<SilverlightApplication>false</SilverlightApplication>
|
||||
<ValidateXaml>true</ValidateXaml>
|
||||
<ThrowErrorsInValidation>false</ThrowErrorsInValidation>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
<DebugType>full</DebugType>
|
||||
<Optimize>false</Optimize>
|
||||
<OutputPath>Bin\Debug</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>Bin\Release</OutputPath>
|
||||
<DefineConstants>TRACE;SILVERLIGHT</DefineConstants>
|
||||
<NoStdLib>true</NoStdLib>
|
||||
<NoConfig>true</NoConfig>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="System.Windows" />
|
||||
<Reference Include="mscorlib" />
|
||||
<Reference Include="system" />
|
||||
<Reference Include="System.Core" />
|
||||
<Reference Include="System.Xml" />
|
||||
<Reference Include="System.Net" />
|
||||
<Reference Include="System.Windows.Browser" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="..\Newtonsoft.Json\Converters\HtmlColorConverter.cs">
|
||||
<Link>Converters\HtmlColorConverter.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="..\Newtonsoft.Json\Converters\IsoDateTimeConverter.cs">
|
||||
<Link>Converters\IsoDateTimeConverter.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="..\Newtonsoft.Json\Converters\JavaScriptDateTimeConverter.cs">
|
||||
<Link>Converters\JavaScriptDateTimeConverter.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="..\Newtonsoft.Json\Converters\JsonDateTimeSerializationMode.cs">
|
||||
<Link>Converters\JsonDateTimeSerializationMode.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="..\Newtonsoft.Json\Converters\XmlNodeConverter.cs">
|
||||
<Link>Converters\XmlNodeConverter.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="..\Newtonsoft.Json\Identifier.cs">
|
||||
<Link>Identifier.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="..\Newtonsoft.Json\JavaScriptConvert.cs">
|
||||
<Link>JavaScriptConvert.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="..\Newtonsoft.Json\JsonConverter.cs">
|
||||
<Link>JsonConverter.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="..\Newtonsoft.Json\JsonConverterCollection.cs">
|
||||
<Link>JsonConverterCollection.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="..\Newtonsoft.Json\JsonHandler.cs">
|
||||
<Link>JsonHandler.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="..\Newtonsoft.Json\JsonIgnoreAttribute.cs">
|
||||
<Link>JsonIgnoreAttribute.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="..\Newtonsoft.Json\JsonObjectAttribute.cs">
|
||||
<Link>JsonObjectAttribute.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="..\Newtonsoft.Json\JsonPropertyAttribute.cs">
|
||||
<Link>JsonPropertyAttribute.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="..\Newtonsoft.Json\JsonReader.cs">
|
||||
<Link>JsonReader.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="..\Newtonsoft.Json\JsonReaderException.cs">
|
||||
<Link>JsonReaderException.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="..\Newtonsoft.Json\JsonSerializationException.cs">
|
||||
<Link>JsonSerializationException.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="..\Newtonsoft.Json\JsonSerializer.cs">
|
||||
<Link>JsonSerializer.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="..\Newtonsoft.Json\JsonTextReader.cs">
|
||||
<Link>JsonTextReader.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="..\Newtonsoft.Json\JsonTextWriter.cs">
|
||||
<Link>JsonTextWriter.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="..\Newtonsoft.Json\JsonToken.cs">
|
||||
<Link>JsonToken.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="..\Newtonsoft.Json\JsonWriter.cs">
|
||||
<Link>JsonWriter.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="..\Newtonsoft.Json\JsonWriterException.cs">
|
||||
<Link>JsonWriterException.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="..\Newtonsoft.Json\Linq\Extensions.cs">
|
||||
<Link>Linq\Extensions.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="..\Newtonsoft.Json\Linq\JArray.cs">
|
||||
<Link>Linq\JArray.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="..\Newtonsoft.Json\Linq\JConstructor.cs">
|
||||
<Link>Linq\JConstructor.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="..\Newtonsoft.Json\Linq\JContainer.cs">
|
||||
<Link>Linq\JContainer.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="..\Newtonsoft.Json\Linq\JEnumerable.cs">
|
||||
<Link>Linq\JEnumerable.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="..\Newtonsoft.Json\Linq\JObject.cs">
|
||||
<Link>Linq\JObject.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="..\Newtonsoft.Json\Linq\JProperty.cs">
|
||||
<Link>Linq\JProperty.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="..\Newtonsoft.Json\Linq\JsonTokenReader.cs">
|
||||
<Link>Linq\JsonTokenReader.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="..\Newtonsoft.Json\Linq\JsonTokenType.cs">
|
||||
<Link>Linq\JsonTokenType.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="..\Newtonsoft.Json\Linq\JsonTokenWriter.cs">
|
||||
<Link>Linq\JsonTokenWriter.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="..\Newtonsoft.Json\Linq\JToken.cs">
|
||||
<Link>Linq\JToken.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="..\Newtonsoft.Json\Linq\JValue.cs">
|
||||
<Link>Linq\JValue.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="..\Newtonsoft.Json\MemberMapping.cs">
|
||||
<Link>MemberMapping.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="..\Newtonsoft.Json\MemberMappingCollection.cs">
|
||||
<Link>MemberMappingCollection.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="..\Newtonsoft.Json\MemberSerialization.cs">
|
||||
<Link>MemberSerialization.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="..\Newtonsoft.Json\MissingMemberHandling.cs">
|
||||
<Link>MissingMemberHandling.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="..\Newtonsoft.Json\NullValueHandling.cs">
|
||||
<Link>NullValueHandling.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="..\Newtonsoft.Json\ObjectCreationHandling.cs">
|
||||
<Link>ObjectCreationHandling.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="..\Newtonsoft.Json\ReferenceLoopHandling.cs">
|
||||
<Link>ReferenceLoopHandling.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="..\Newtonsoft.Json\StringBuffer.cs">
|
||||
<Link>StringBuffer.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="..\Newtonsoft.Json\Utilities\CollectionUtils.cs">
|
||||
<Link>Utilities\CollectionUtils.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="..\Newtonsoft.Json\Utilities\ConvertUtils.cs">
|
||||
<Link>Utilities\ConvertUtils.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="..\Newtonsoft.Json\Utilities\DateTimeUtils.cs">
|
||||
<Link>Utilities\DateTimeUtils.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="..\Newtonsoft.Json\Utilities\DictionaryWrapper.cs">
|
||||
<Link>Utilities\DictionaryWrapper.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="..\Newtonsoft.Json\Utilities\JavaScriptUtils.cs">
|
||||
<Link>Utilities\JavaScriptUtils.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="..\Newtonsoft.Json\Utilities\ListWrapper.cs">
|
||||
<Link>Utilities\ListWrapper.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="..\Newtonsoft.Json\Utilities\MathUtils.cs">
|
||||
<Link>Utilities\MathUtils.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="..\Newtonsoft.Json\Utilities\MiscellaneousUtils.cs">
|
||||
<Link>Utilities\MiscellaneousUtils.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="..\Newtonsoft.Json\Utilities\ReflectionUtils.cs">
|
||||
<Link>Utilities\ReflectionUtils.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="..\Newtonsoft.Json\Utilities\StringUtils.cs">
|
||||
<Link>Utilities\StringUtils.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="..\Newtonsoft.Json\Utilities\ValidationUtils.cs">
|
||||
<Link>Utilities\ValidationUtils.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
</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>
|
||||
@@ -23,6 +23,7 @@
|
||||
// OTHER DEALINGS IN THE SOFTWARE.
|
||||
#endregion
|
||||
|
||||
#if !SILVERLIGHT
|
||||
using System;
|
||||
using NUnit.Framework;
|
||||
using Newtonsoft.Json;
|
||||
@@ -455,4 +456,5 @@ namespace Newtonsoft.Json.Tests.Converters
|
||||
XmlDocument newDoc = (XmlDocument)JavaScriptConvert.DeserializeXmlNode(strJSON);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -96,7 +96,7 @@ now brown cow?", '"', true);
|
||||
[ExpectedException(typeof(ArgumentException), ExpectedMessage = "Unsupported type: System.Version. Use the JsonSerializer class to get the object's JSON representation.")]
|
||||
public void ToStringInvalid()
|
||||
{
|
||||
JavaScriptConvert.ToString(new Version());
|
||||
JavaScriptConvert.ToString(new Version(1, 0));
|
||||
}
|
||||
|
||||
[Test]
|
||||
|
||||
@@ -33,12 +33,10 @@ using System.Collections;
|
||||
using System.Xml;
|
||||
using System.Xml.Serialization;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Runtime.Serialization.Json;
|
||||
using System.Net.Mail;
|
||||
using System.Web.Script.Serialization;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using System.Linq;
|
||||
using Newtonsoft.Json.Converters;
|
||||
using System.Runtime.Serialization.Json;
|
||||
|
||||
namespace Newtonsoft.Json.Tests
|
||||
{
|
||||
@@ -216,6 +214,8 @@ namespace Newtonsoft.Json.Tests
|
||||
|
||||
Store deserializedStore = (Store)JavaScriptConvert.DeserializeObject(jsonText, typeof(Store));
|
||||
|
||||
Assert.AreEqual(store.Establised, deserializedStore.Establised);
|
||||
|
||||
Console.WriteLine(jsonText);
|
||||
}
|
||||
|
||||
@@ -327,6 +327,7 @@ namespace Newtonsoft.Json.Tests
|
||||
Assert.AreEqual(executorObject2.clientGetResultFunction, "ClientBanSubsCB");
|
||||
}
|
||||
|
||||
#if !SILVERLIGHT
|
||||
[Test]
|
||||
public void HashtableDeserialization()
|
||||
{
|
||||
@@ -355,6 +356,7 @@ namespace Newtonsoft.Json.Tests
|
||||
""01/24/2010 12:00:00""
|
||||
]", p.Hash["UntypedArray"].ToString());
|
||||
}
|
||||
#endif
|
||||
|
||||
public class GetOnlyPropertyClass
|
||||
{
|
||||
@@ -969,7 +971,7 @@ keyword such as type of business.""
|
||||
[Test]
|
||||
public void SerializeStack()
|
||||
{
|
||||
Stack s = new Stack();
|
||||
Stack<object> s = new Stack<object>();
|
||||
s.Push(1);
|
||||
s.Push(2);
|
||||
s.Push(3);
|
||||
|
||||
@@ -29,7 +29,6 @@ using System.Text;
|
||||
using NUnit.Framework;
|
||||
using Newtonsoft.Json;
|
||||
using System.IO;
|
||||
using System.Runtime.Serialization.Json;
|
||||
|
||||
namespace Newtonsoft.Json.Tests
|
||||
{
|
||||
|
||||
@@ -29,7 +29,6 @@ using System.Linq;
|
||||
using System.Text;
|
||||
using NUnit.Framework;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using System.Xml.Linq;
|
||||
|
||||
namespace Newtonsoft.Json.Tests.Linq
|
||||
{
|
||||
@@ -64,9 +63,6 @@ namespace Newtonsoft.Json.Tests.Linq
|
||||
|
||||
Assert.AreEqual(v, o["Test1"]);
|
||||
|
||||
XText t = new XText("XText");
|
||||
Assert.AreEqual(null, t.Parent);
|
||||
|
||||
Assert.AreEqual(null, o.Parent);
|
||||
JProperty o1 = new JProperty("O1", o);
|
||||
Assert.AreEqual(o, o1.Value);
|
||||
|
||||
@@ -447,7 +447,7 @@ keyword such as type of business.""
|
||||
//}
|
||||
|
||||
var postTitles =
|
||||
from p in rss["channel"]["item"].Children()
|
||||
from p in rss["channel"]["item"]
|
||||
select p.Value<string>("title");
|
||||
|
||||
foreach (var item in postTitles)
|
||||
|
||||
@@ -34,11 +34,11 @@ namespace Newtonsoft.Json.Tests
|
||||
[TestFixture]
|
||||
public abstract class TestFixtureBase
|
||||
{
|
||||
public string GetOffset(DateTime value)
|
||||
{
|
||||
TimeSpan utcOffset = TimeZone.CurrentTimeZone.GetUtcOffset(value.ToLocalTime());
|
||||
//public string GetOffset(DateTime value)
|
||||
//{
|
||||
// TimeSpan utcOffset = TimeZone.CurrentTimeZone.GetUtcOffset(value.ToLocalTime());
|
||||
|
||||
return utcOffset.Hours.ToString("+00;-00") + utcOffset.Minutes.ToString("00;00");
|
||||
}
|
||||
// return utcOffset.Hours.ToString("+00;-00") + utcOffset.Minutes.ToString("00;00");
|
||||
//}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,6 +22,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Doc", "Doc", "{8F9E13D9-418
|
||||
Doc\styles.css = Doc\styles.css
|
||||
EndProjectSection
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Newtonsoft.Json.Silverlight", "Newtonsoft.Json.Silverlight\Newtonsoft.Json.Silverlight.csproj", "{DC3C6F3D-2CA1-4278-9B79-63770FB3EA2D}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Newtonsoft.Json.Tests.Silverlight", "Newtonsoft.Json.Tests.Silverlight\Newtonsoft.Json.Tests.Silverlight.csproj", "{0D8C3C2E-62C6-4C93-9377-6F74DD6BFD93}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
@@ -36,6 +40,14 @@ Global
|
||||
{3E6E2335-B079-4B5B-A65A-9D586914BCBB}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{3E6E2335-B079-4B5B-A65A-9D586914BCBB}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{3E6E2335-B079-4B5B-A65A-9D586914BCBB}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{DC3C6F3D-2CA1-4278-9B79-63770FB3EA2D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{DC3C6F3D-2CA1-4278-9B79-63770FB3EA2D}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{DC3C6F3D-2CA1-4278-9B79-63770FB3EA2D}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{DC3C6F3D-2CA1-4278-9B79-63770FB3EA2D}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{0D8C3C2E-62C6-4C93-9377-6F74DD6BFD93}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{0D8C3C2E-62C6-4C93-9377-6F74DD6BFD93}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{0D8C3C2E-62C6-4C93-9377-6F74DD6BFD93}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{0D8C3C2E-62C6-4C93-9377-6F74DD6BFD93}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
|
||||
@@ -23,6 +23,7 @@
|
||||
// OTHER DEALINGS IN THE SOFTWARE.
|
||||
#endregion
|
||||
|
||||
#if !SILVERLIGHT
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
@@ -69,3 +70,4 @@ namespace Newtonsoft.Json.Converters
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -23,6 +23,7 @@
|
||||
// OTHER DEALINGS IN THE SOFTWARE.
|
||||
#endregion
|
||||
|
||||
#if !SILVERLIGHT
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
@@ -552,3 +553,4 @@ namespace Newtonsoft.Json.Converters
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -29,10 +29,7 @@ using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Web;
|
||||
using System.Collections.Generic;
|
||||
using System.Drawing;
|
||||
using System.Web.UI.WebControls;
|
||||
using System.ComponentModel;
|
||||
|
||||
namespace Newtonsoft.Json
|
||||
|
||||
@@ -26,14 +26,11 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Drawing;
|
||||
using System.Web.UI.WebControls;
|
||||
using System.Collections;
|
||||
using System.IO;
|
||||
using System.Globalization;
|
||||
using System.Runtime.Serialization;
|
||||
using System.Reflection;
|
||||
using System.Data.SqlTypes;
|
||||
using Newtonsoft.Json.Utilities;
|
||||
using System.Xml;
|
||||
using Newtonsoft.Json.Converters;
|
||||
@@ -182,7 +179,7 @@ namespace Newtonsoft.Json
|
||||
/// <returns>A Json string representation of the <see cref="Enum"/>.</returns>
|
||||
public static string ToString(Enum value)
|
||||
{
|
||||
return Enum.Format(value.GetType(), value, "D");
|
||||
return value.ToString("D");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -515,6 +512,7 @@ namespace Newtonsoft.Json
|
||||
return deserializedValue;
|
||||
}
|
||||
|
||||
#if !SILVERLIGHT
|
||||
/// <summary>
|
||||
/// Serializes the XML node to a JSON string.
|
||||
/// </summary>
|
||||
@@ -538,5 +536,6 @@ namespace Newtonsoft.Json
|
||||
|
||||
return (XmlDocument)DeserializeObject(value, typeof(XmlDocument), converter);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,29 @@
|
||||
#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
|
||||
|
||||
#if !SILVERLIGHT
|
||||
using System;
|
||||
using System.Web;
|
||||
using System.Web.Caching;
|
||||
@@ -60,4 +86,5 @@ namespace Newtonsoft.Json
|
||||
get { return false; }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -353,7 +353,7 @@ namespace Newtonsoft.Json
|
||||
case JsonToken.Date:
|
||||
return false;
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException("token", token, "Unexpected JsonToken value.");
|
||||
throw MiscellaneousUtils.CreateArgumentOutOfRangeException("token", token, "Unexpected JsonToken value.");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -290,27 +290,7 @@ namespace Newtonsoft.Json
|
||||
// attempt to convert value's type to target's type
|
||||
if (valueType != targetType)
|
||||
{
|
||||
TypeConverter targetConverter = TypeDescriptor.GetConverter(targetType);
|
||||
|
||||
if (!targetConverter.CanConvertFrom(valueType))
|
||||
{
|
||||
if (targetConverter.CanConvertFrom(typeof(string)))
|
||||
{
|
||||
string valueString = TypeDescriptor.GetConverter(value).ConvertToInvariantString(value);
|
||||
|
||||
return targetConverter.ConvertFromInvariantString(valueString);
|
||||
}
|
||||
|
||||
if (targetType == typeof(DateTimeOffset) && value is DateTime)
|
||||
return new DateTimeOffset((DateTime)value);
|
||||
|
||||
if (!targetType.IsAssignableFrom(valueType))
|
||||
throw new InvalidOperationException("Cannot convert object of type '{0}' to type '{1}'".FormatWith(CultureInfo.InvariantCulture, value.GetType(), targetType));
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
return targetConverter.ConvertFrom(null, CultureInfo.InvariantCulture, value);
|
||||
return ConvertUtils.ConvertOrCast(value, CultureInfo.InvariantCulture, targetType);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -763,6 +743,7 @@ namespace Newtonsoft.Json
|
||||
{
|
||||
Type objectType = value.GetType();
|
||||
|
||||
#if !SILVERLIGHT
|
||||
TypeConverter converter = TypeDescriptor.GetConverter(objectType);
|
||||
|
||||
// use the objectType's TypeConverter if it has one and can convert to a string
|
||||
@@ -774,6 +755,13 @@ namespace Newtonsoft.Json
|
||||
return;
|
||||
}
|
||||
}
|
||||
#else
|
||||
if (value is Guid)
|
||||
{
|
||||
writer.WriteValue(value.ToString());
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
|
||||
writer.SerializeStack.Add(value);
|
||||
|
||||
|
||||
@@ -198,7 +198,7 @@ namespace Newtonsoft.Json
|
||||
value = value.Substring(0, index);
|
||||
}
|
||||
|
||||
long javaScriptTicks = long.Parse(value, CultureInfo.InvariantCulture);
|
||||
long javaScriptTicks = long.Parse(value, NumberStyles.Integer, CultureInfo.InvariantCulture);
|
||||
DateTime utcDateTime = JavaScriptConvert.ConvertJavaScriptTicksToDateTime(javaScriptTicks);
|
||||
DateTime dateTime;
|
||||
|
||||
|
||||
@@ -364,7 +364,7 @@ namespace Newtonsoft.Json
|
||||
WriteValue((DateTime)reader.Value);
|
||||
break;
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException("TokenType", reader.TokenType, "Unexpected token type.");
|
||||
throw MiscellaneousUtils.CreateArgumentOutOfRangeException("TokenType", reader.TokenType, "Unexpected token type.");
|
||||
}
|
||||
}
|
||||
while (reader.Read() && (currentDepth - 1 < reader.Depth || (currentDepth == reader.Depth && !IsEndToken(reader.TokenType))));
|
||||
|
||||
@@ -36,7 +36,7 @@ namespace Newtonsoft.Json.Linq
|
||||
/// <summary>
|
||||
/// Represents a JSON array.
|
||||
/// </summary>
|
||||
public class JArray : JContainer
|
||||
public class JArray : JContainer, IList<JToken>
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the node type for this <see cref="JToken"/>.
|
||||
@@ -92,15 +92,6 @@ namespace Newtonsoft.Json.Linq
|
||||
return new JArray(this);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a count of this token's child tokens.
|
||||
/// </summary>
|
||||
/// <returns>A count of this token's child tokens.</returns>
|
||||
public int Count()
|
||||
{
|
||||
return Children().Count();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Loads an <see cref="JArray"/> from a <see cref="JsonReader"/>.
|
||||
/// </summary>
|
||||
@@ -194,8 +185,108 @@ namespace Newtonsoft.Json.Linq
|
||||
if (!(key is int))
|
||||
throw new ArgumentException("Accessed JArray values with invalid key value: {0}. Array position index expected.".FormatWith(CultureInfo.InvariantCulture, MiscellaneousUtils.ToString(key)));
|
||||
|
||||
return GetIndex(this, (int)key);
|
||||
return this[(int)key];
|
||||
}
|
||||
}
|
||||
|
||||
public JToken this[int index]
|
||||
{
|
||||
get
|
||||
{
|
||||
return GetIndex(this, index);
|
||||
}
|
||||
set
|
||||
{
|
||||
JToken token = this[(object)index];
|
||||
token.Replace(value);
|
||||
}
|
||||
}
|
||||
|
||||
#region IList<JToken> Members
|
||||
|
||||
public int IndexOf(JToken item)
|
||||
{
|
||||
int index = 0;
|
||||
foreach (JToken token in Children())
|
||||
{
|
||||
if (token == item)
|
||||
return index;
|
||||
|
||||
index++;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
public void Insert(int index, JToken item)
|
||||
{
|
||||
JToken token = GetIndex(this, index);
|
||||
AddInternal(false, token.Previous, item);
|
||||
}
|
||||
|
||||
public void RemoveAt(int index)
|
||||
{
|
||||
int currentIndex = 0;
|
||||
foreach (JToken token in Children())
|
||||
{
|
||||
if (index == currentIndex)
|
||||
{
|
||||
token.Remove();
|
||||
return;
|
||||
}
|
||||
|
||||
currentIndex++;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region ICollection<JToken> Members
|
||||
|
||||
void ICollection<JToken>.Add(JToken item)
|
||||
{
|
||||
Add((object)item);
|
||||
}
|
||||
|
||||
void ICollection<JToken>.Clear()
|
||||
{
|
||||
RemoveAll();
|
||||
}
|
||||
|
||||
public bool Contains(JToken item)
|
||||
{
|
||||
return Children().Contains(item);
|
||||
}
|
||||
|
||||
void ICollection<JToken>.CopyTo(JToken[] array, int arrayIndex)
|
||||
{
|
||||
int index = 0;
|
||||
foreach (JToken token in Children())
|
||||
{
|
||||
array[arrayIndex + index] = token;
|
||||
index++;
|
||||
}
|
||||
}
|
||||
|
||||
public int Count
|
||||
{
|
||||
get { return Children().Count(); }
|
||||
}
|
||||
|
||||
bool ICollection<JToken>.IsReadOnly
|
||||
{
|
||||
get { return false; }
|
||||
}
|
||||
|
||||
bool ICollection<JToken>.Remove(JToken item)
|
||||
{
|
||||
if (!((ICollection<JToken>)this).Contains(item))
|
||||
return false;
|
||||
|
||||
item.Remove();
|
||||
return true;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -214,7 +214,7 @@ namespace Newtonsoft.Json.Linq
|
||||
|
||||
internal bool IsMultiContent(object content)
|
||||
{
|
||||
return (content is IEnumerable && !(content is string));
|
||||
return (content is IEnumerable && !(content is string) && !(content is JToken));
|
||||
}
|
||||
|
||||
internal void AddInternal(bool isLast, JToken previous, object content)
|
||||
|
||||
@@ -36,7 +36,7 @@ namespace Newtonsoft.Json.Linq
|
||||
/// <summary>
|
||||
/// Represents a JSON object.
|
||||
/// </summary>
|
||||
public class JObject : JContainer
|
||||
public class JObject : JContainer, IDictionary<string, JToken>
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="JObject"/> class.
|
||||
@@ -144,10 +144,28 @@ namespace Newtonsoft.Json.Linq
|
||||
if (propertyName == null)
|
||||
throw new ArgumentException("Accessed JObject values with invalid key value: {0}. Object property name expected.".FormatWith(CultureInfo.InvariantCulture, MiscellaneousUtils.ToString(key)));
|
||||
|
||||
return this[propertyName];
|
||||
}
|
||||
}
|
||||
|
||||
public JToken this[string propertyName]
|
||||
{
|
||||
get
|
||||
{
|
||||
ValidationUtils.ArgumentNotNull(propertyName, "propertyName");
|
||||
|
||||
JProperty property = Property(propertyName);
|
||||
|
||||
return (property != null) ? property.Value : null;
|
||||
}
|
||||
set
|
||||
{
|
||||
JProperty property = Property(propertyName);
|
||||
if (property != null)
|
||||
property.Value = value;
|
||||
else
|
||||
Add(new JProperty(propertyName, value));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -231,5 +249,114 @@ namespace Newtonsoft.Json.Linq
|
||||
|
||||
writer.WriteEndObject();
|
||||
}
|
||||
|
||||
#region IDictionary<string,JToken> Members
|
||||
public void Add(string propertyName, JToken value)
|
||||
{
|
||||
Add(new JProperty(propertyName, value));
|
||||
}
|
||||
|
||||
bool IDictionary<string, JToken>.ContainsKey(string key)
|
||||
{
|
||||
return (Property(key) != null);
|
||||
}
|
||||
|
||||
ICollection<string> IDictionary<string, JToken>.Keys
|
||||
{
|
||||
get { throw new NotImplementedException(); }
|
||||
}
|
||||
|
||||
public bool Remove(string propertyName)
|
||||
{
|
||||
JProperty property = Property(propertyName);
|
||||
if (property == null)
|
||||
return false;
|
||||
|
||||
property.Remove();
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool TryGetValue(string propertyName, out JToken value)
|
||||
{
|
||||
JProperty property = Property(propertyName);
|
||||
if (property == null)
|
||||
{
|
||||
value = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
value = property.Value;
|
||||
return true;
|
||||
}
|
||||
|
||||
ICollection<JToken> IDictionary<string, JToken>.Values
|
||||
{
|
||||
get { throw new NotImplementedException(); }
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region ICollection<KeyValuePair<string,JToken>> Members
|
||||
|
||||
void ICollection<KeyValuePair<string,JToken>>.Add(KeyValuePair<string, JToken> item)
|
||||
{
|
||||
Add(new JProperty(item.Key, item.Value));
|
||||
}
|
||||
|
||||
void ICollection<KeyValuePair<string, JToken>>.Clear()
|
||||
{
|
||||
RemoveAll();
|
||||
}
|
||||
|
||||
bool ICollection<KeyValuePair<string,JToken>>.Contains(KeyValuePair<string, JToken> item)
|
||||
{
|
||||
JProperty property = Property(item.Key);
|
||||
if (property == null)
|
||||
return false;
|
||||
|
||||
return (property.Value == item.Value);
|
||||
}
|
||||
|
||||
void ICollection<KeyValuePair<string,JToken>>.CopyTo(KeyValuePair<string, JToken>[] array, int arrayIndex)
|
||||
{
|
||||
int index = 0;
|
||||
foreach (JProperty property in Properties())
|
||||
{
|
||||
array[arrayIndex + index] = new KeyValuePair<string, JToken>(property.Name, property.Value);
|
||||
}
|
||||
}
|
||||
|
||||
public int Count
|
||||
{
|
||||
get { return Children().Count(); }
|
||||
}
|
||||
|
||||
bool ICollection<KeyValuePair<string,JToken>>.IsReadOnly
|
||||
{
|
||||
get { return false; }
|
||||
}
|
||||
|
||||
bool ICollection<KeyValuePair<string,JToken>>.Remove(KeyValuePair<string, JToken> item)
|
||||
{
|
||||
if (!((ICollection<KeyValuePair<string,JToken>>)this).Contains(item))
|
||||
return false;
|
||||
|
||||
((IDictionary<string, JToken>)this).Remove(item.Key);
|
||||
return true;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region IEnumerable<KeyValuePair<string,JToken>> Members
|
||||
|
||||
IEnumerator<KeyValuePair<string, JToken>> IEnumerable<KeyValuePair<string,JToken>>.GetEnumerator()
|
||||
{
|
||||
foreach (JProperty property in Properties())
|
||||
{
|
||||
yield return new KeyValuePair<string, JToken>(property.Name, property.Value);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -31,13 +31,14 @@ using System.IO;
|
||||
using Newtonsoft.Json.Utilities;
|
||||
using System.Diagnostics;
|
||||
using System.Globalization;
|
||||
using System.Collections;
|
||||
|
||||
namespace Newtonsoft.Json.Linq
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents an abstract JSON token.
|
||||
/// </summary>
|
||||
public abstract class JToken
|
||||
public abstract class JToken : IEnumerable<JToken>
|
||||
{
|
||||
private JContainer _parent;
|
||||
internal JToken _next;
|
||||
@@ -694,5 +695,15 @@ namespace Newtonsoft.Json.Linq
|
||||
return Convert.ToUInt64(v.Value, CultureInfo.InvariantCulture);
|
||||
}
|
||||
#endregion
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator()
|
||||
{
|
||||
return ((IEnumerable<JToken>)this).GetEnumerator();
|
||||
}
|
||||
|
||||
IEnumerator<JToken> IEnumerable<JToken>.GetEnumerator()
|
||||
{
|
||||
return Children().GetEnumerator();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -159,7 +159,7 @@ namespace Newtonsoft.Json.Linq
|
||||
case JsonTokenType.Date:
|
||||
return objA.Equals(objB);
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException("valueType", _valueType, "Unexpected value type: {0}".FormatWith(CultureInfo.InvariantCulture, _valueType));
|
||||
throw MiscellaneousUtils.CreateArgumentOutOfRangeException("valueType", _valueType, "Unexpected value type: {0}".FormatWith(CultureInfo.InvariantCulture, _valueType));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -286,7 +286,7 @@ namespace Newtonsoft.Json.Linq
|
||||
writer.WriteUndefined();
|
||||
break;
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException("TokenType", _valueType, "Unexpected token type.");
|
||||
throw MiscellaneousUtils.CreateArgumentOutOfRangeException("TokenType", _valueType, "Unexpected token type.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -88,7 +88,7 @@ namespace Newtonsoft.Json.Linq
|
||||
case JsonTokenType.Property:
|
||||
return null;
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException("Type", c.Type, "Unexpected JContainer type.");
|
||||
throw MiscellaneousUtils.CreateArgumentOutOfRangeException("Type", c.Type, "Unexpected JContainer type.");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -165,7 +165,7 @@ namespace Newtonsoft.Json.Linq
|
||||
SetToken(JsonToken.Date, ((JValue)token).Value);
|
||||
break;
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException("Type", token.Type, "Unexpected JsonTokenType.");
|
||||
throw MiscellaneousUtils.CreateArgumentOutOfRangeException("Type", token.Type, "Unexpected JsonTokenType.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -112,6 +112,7 @@
|
||||
<Compile Include="MissingMemberHandling.cs" />
|
||||
<Compile Include="NullValueHandling.cs" />
|
||||
<Compile Include="ReferenceLoopHandling.cs" />
|
||||
<Compile Include="Utilities\ConvertUtils.cs" />
|
||||
<Compile Include="Utilities\DateTimeUtils.cs" />
|
||||
<Compile Include="Utilities\DictionaryWrapper.cs" />
|
||||
<Compile Include="Utilities\JavaScriptUtils.cs" />
|
||||
|
||||
@@ -436,7 +436,7 @@ namespace Newtonsoft.Json.Utilities
|
||||
{
|
||||
// have to use an arraylist when creating array
|
||||
// there is no way to know the size until it is finised
|
||||
list = new ArrayList();
|
||||
list = new List<object>();
|
||||
isReadOnlyOrFixedSize = true;
|
||||
}
|
||||
else if (ReflectionUtils.IsSubClass(listType, typeof(ReadOnlyCollection<>), out collectionType))
|
||||
@@ -494,12 +494,22 @@ namespace Newtonsoft.Json.Utilities
|
||||
if (isReadOnlyOrFixedSize)
|
||||
{
|
||||
if (listType.IsArray)
|
||||
list = ((ArrayList)list).ToArray(ReflectionUtils.GetListItemType(listType));
|
||||
list = ToArray(((List<object>)list).ToArray(), ReflectionUtils.GetListItemType(listType));
|
||||
else if (ReflectionUtils.IsSubClass(listType, typeof(ReadOnlyCollection<>)))
|
||||
list = (IList)Activator.CreateInstance(listType, list);
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
public static Array ToArray(Array initial, Type type)
|
||||
{
|
||||
if (type == null)
|
||||
throw new ArgumentNullException("type");
|
||||
|
||||
Array destinationArray = Array.CreateInstance(type, initial.Length);
|
||||
Array.Copy(initial, 0, destinationArray, 0, initial.Length);
|
||||
return destinationArray;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,368 @@
|
||||
#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;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Globalization;
|
||||
using System.ComponentModel;
|
||||
#if !SILVERLIGHT
|
||||
using System.Data.SqlTypes;
|
||||
#endif
|
||||
|
||||
namespace Newtonsoft.Json.Utilities
|
||||
{
|
||||
internal static class ConvertUtils
|
||||
{
|
||||
#region Convert
|
||||
/// <summary>
|
||||
/// Converts the value to the specified type.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type to convert the value to.</typeparam>
|
||||
/// <param name="initialValue">The value to convert.</param>
|
||||
/// <returns>The converted type.</returns>
|
||||
public static T Convert<T>(object initialValue)
|
||||
{
|
||||
return Convert<T>(initialValue, CultureInfo.CurrentCulture);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts the value to the specified type.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type to convert the value to.</typeparam>
|
||||
/// <param name="initialValue">The value to convert.</param>
|
||||
/// <param name="culture">The culture to use when converting.</param>
|
||||
/// <returns>The converted type.</returns>
|
||||
public static T Convert<T>(object initialValue, CultureInfo culture)
|
||||
{
|
||||
return (T)Convert(initialValue, culture, typeof(T));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts the value to the specified type.
|
||||
/// </summary>
|
||||
/// <param name="initialValue">The value to convert.</param>
|
||||
/// <param name="culture">The culture to use when converting.</param>
|
||||
/// <param name="targetType">The type to convert the value to.</param>
|
||||
/// <returns>The converted type.</returns>
|
||||
public static object Convert(object initialValue, CultureInfo culture, Type targetType)
|
||||
{
|
||||
if (initialValue == null)
|
||||
throw new ArgumentNullException("initialValue");
|
||||
|
||||
Type initialType = initialValue.GetType();
|
||||
|
||||
if (targetType == initialType)
|
||||
return initialValue;
|
||||
|
||||
if (targetType.IsInterface || targetType.IsGenericTypeDefinition || targetType.IsAbstract)
|
||||
throw new ArgumentException(string.Format("Target type {0} is not a value type or a non-abstract class.", targetType), "targetType");
|
||||
|
||||
|
||||
// use Convert.ChangeType if both types are IConvertible
|
||||
if (initialValue is IConvertible && typeof(IConvertible).IsAssignableFrom(targetType))
|
||||
{
|
||||
if (targetType.IsEnum)
|
||||
{
|
||||
if (initialValue is string)
|
||||
return Enum.Parse(targetType, initialValue.ToString(), true);
|
||||
else if (IsInteger(initialValue))
|
||||
return Enum.ToObject(targetType, initialValue);
|
||||
}
|
||||
|
||||
return System.Convert.ChangeType(initialValue, targetType, culture);
|
||||
}
|
||||
|
||||
if (initialValue is DateTime && targetType == typeof(DateTimeOffset))
|
||||
return new DateTimeOffset((DateTime)initialValue);
|
||||
|
||||
if (initialValue is string && targetType == typeof(Guid))
|
||||
return new Guid((string)initialValue);
|
||||
|
||||
// see if source or target types have a TypeConverter that converts between the two
|
||||
TypeConverter toConverter = GetConverter(initialType);
|
||||
|
||||
if (toConverter != null && toConverter.CanConvertTo(targetType))
|
||||
{
|
||||
#if !SILVERLIGHT
|
||||
return toConverter.ConvertTo(null, culture, initialValue, targetType);
|
||||
#else
|
||||
return toConverter.ConvertTo(initialValue, targetType);
|
||||
#endif
|
||||
}
|
||||
|
||||
TypeConverter fromConverter = GetConverter(targetType);
|
||||
|
||||
if (fromConverter != null && fromConverter.CanConvertFrom(initialType))
|
||||
{
|
||||
#if !SILVERLIGHT
|
||||
return fromConverter.ConvertFrom(null, culture, initialValue);
|
||||
#else
|
||||
return fromConverter.ConvertFrom(initialValue);
|
||||
#endif
|
||||
}
|
||||
|
||||
// handle DBNull and INullable
|
||||
if (initialValue == DBNull.Value)
|
||||
{
|
||||
if (ReflectionUtils.IsNullable(targetType))
|
||||
return EnsureTypeAssignable(null, initialType, targetType);
|
||||
else
|
||||
throw new Exception(string.Format("Can not convert null {0} into non-nullable {1}.", initialType, targetType));
|
||||
}
|
||||
#if !SILVERLIGHT
|
||||
else if (initialValue is INullable)
|
||||
{
|
||||
return EnsureTypeAssignable(ToValue((INullable)initialValue), initialType, targetType);
|
||||
}
|
||||
#endif
|
||||
|
||||
throw new Exception(string.Format("Can not convert from {0} to {1}.", initialType, targetType));
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region TryConvert
|
||||
/// <summary>
|
||||
/// Converts the value to the specified type.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type to convert the value to.</typeparam>
|
||||
/// <param name="initialValue">The value to convert.</param>
|
||||
/// <param name="convertedValue">The converted value if the conversion was successful or the default value of <c>T</c> if it failed.</param>
|
||||
/// <returns>
|
||||
/// <c>true</c> if <c>initialValue</c> was converted successfully; otherwise, <c>false</c>.
|
||||
/// </returns>
|
||||
public static bool TryConvert<T>(object initialValue, out T convertedValue)
|
||||
{
|
||||
return TryConvert(initialValue, CultureInfo.CurrentCulture, out convertedValue);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts the value to the specified type.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type to convert the value to.</typeparam>
|
||||
/// <param name="initialValue">The value to convert.</param>
|
||||
/// <param name="culture">The culture to use when converting.</param>
|
||||
/// <param name="convertedValue">The converted value if the conversion was successful or the default value of <c>T</c> if it failed.</param>
|
||||
/// <returns>
|
||||
/// <c>true</c> if <c>initialValue</c> was converted successfully; otherwise, <c>false</c>.
|
||||
/// </returns>
|
||||
public static bool TryConvert<T>(object initialValue, CultureInfo culture, out T convertedValue)
|
||||
{
|
||||
return MiscellaneousUtils.TryAction<T>(delegate
|
||||
{
|
||||
object tempConvertedValue;
|
||||
TryConvert(initialValue, CultureInfo.CurrentCulture, typeof(T), out tempConvertedValue);
|
||||
|
||||
return (T)tempConvertedValue;
|
||||
}, out convertedValue);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts the value to the specified type.
|
||||
/// </summary>
|
||||
/// <param name="initialValue">The value to convert.</param>
|
||||
/// <param name="culture">The culture to use when converting.</param>
|
||||
/// <param name="targetType">The type to convert the value to.</param>
|
||||
/// <param name="convertedValue">The converted value if the conversion was successful or the default value of <c>T</c> if it failed.</param>
|
||||
/// <returns>
|
||||
/// <c>true</c> if <c>initialValue</c> was converted successfully; otherwise, <c>false</c>.
|
||||
/// </returns>
|
||||
public static bool TryConvert(object initialValue, CultureInfo culture, Type targetType, out object convertedValue)
|
||||
{
|
||||
return MiscellaneousUtils.TryAction<object>(delegate { return Convert(initialValue, culture, targetType); }, out convertedValue);
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region ConvertOrCast
|
||||
/// <summary>
|
||||
/// Converts the value to the specified type. If the value is unable to be converted, the
|
||||
/// value is checked whether it assignable to the specified type.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type to convert or cast the value to.</typeparam>
|
||||
/// <param name="initialValue">The value to convert.</param>
|
||||
/// <returns>The converted type. If conversion was unsuccessful, the initial value is returned if assignable to the target type</returns>
|
||||
public static T ConvertOrCast<T>(object initialValue)
|
||||
{
|
||||
return ConvertOrCast<T>(initialValue, CultureInfo.CurrentCulture);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts the value to the specified type. If the value is unable to be converted, the
|
||||
/// value is checked whether it assignable to the specified type.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type to convert or cast the value to.</typeparam>
|
||||
/// <param name="initialValue">The value to convert.</param>
|
||||
/// <param name="culture">The culture to use when converting.</param>
|
||||
/// <returns>The converted type. If conversion was unsuccessful, the initial value is returned if assignable to the target type</returns>
|
||||
public static T ConvertOrCast<T>(object initialValue, CultureInfo culture)
|
||||
{
|
||||
return (T)ConvertOrCast(initialValue, culture, typeof(T));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts the value to the specified type. If the value is unable to be converted, the
|
||||
/// value is checked whether it assignable to the specified type.
|
||||
/// </summary>
|
||||
/// <param name="initialValue">The value to convert.</param>
|
||||
/// <param name="culture">The culture to use when converting.</param>
|
||||
/// <param name="targetType">The type to convert or cast the value to.</param>
|
||||
/// <returns>
|
||||
/// The converted type. If conversion was unsuccessful, the initial value
|
||||
/// is returned if assignable to the target type.
|
||||
/// </returns>
|
||||
public static object ConvertOrCast(object initialValue, CultureInfo culture, Type targetType)
|
||||
{
|
||||
object convertedValue;
|
||||
if (TryConvert(initialValue, culture, targetType, out convertedValue))
|
||||
return convertedValue;
|
||||
|
||||
return EnsureTypeAssignable(initialValue, ReflectionUtils.GetObjectType(initialValue), targetType);
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region TryConvertOrCast
|
||||
/// <summary>
|
||||
/// Converts the value to the specified type. If the value is unable to be converted, the
|
||||
/// value is checked whether it assignable to the specified type.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type to convert the value to.</typeparam>
|
||||
/// <param name="initialValue">The value to convert.</param>
|
||||
/// <param name="convertedValue">The converted value if the conversion was successful or the default value of <c>T</c> if it failed.</param>
|
||||
/// <returns>
|
||||
/// <c>true</c> if <c>initialValue</c> was converted successfully or is assignable; otherwise, <c>false</c>.
|
||||
/// </returns>
|
||||
public static bool TryConvertOrCast<T>(object initialValue, out T convertedValue)
|
||||
{
|
||||
return TryConvertOrCast<T>(initialValue, CultureInfo.CurrentCulture, out convertedValue);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts the value to the specified type. If the value is unable to be converted, the
|
||||
/// value is checked whether it assignable to the specified type.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type to convert the value to.</typeparam>
|
||||
/// <param name="initialValue">The value to convert.</param>
|
||||
/// <param name="culture">The culture to use when converting.</param>
|
||||
/// <param name="convertedValue">The converted value if the conversion was successful or the default value of <c>T</c> if it failed.</param>
|
||||
/// <returns>
|
||||
/// <c>true</c> if <c>initialValue</c> was converted successfully or is assignable; otherwise, <c>false</c>.
|
||||
/// </returns>
|
||||
public static bool TryConvertOrCast<T>(object initialValue, CultureInfo culture, out T convertedValue)
|
||||
{
|
||||
return MiscellaneousUtils.TryAction<T>(delegate
|
||||
{
|
||||
object tempConvertedValue;
|
||||
TryConvertOrCast(initialValue, CultureInfo.CurrentCulture, typeof(T), out tempConvertedValue);
|
||||
|
||||
return (T)tempConvertedValue;
|
||||
}, out convertedValue);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts the value to the specified type. If the value is unable to be converted, the
|
||||
/// value is checked whether it assignable to the specified type.
|
||||
/// </summary>
|
||||
/// <param name="initialValue">The value to convert.</param>
|
||||
/// <param name="culture">The culture to use when converting.</param>
|
||||
/// <param name="targetType">The type to convert the value to.</typeparam>
|
||||
/// <param name="convertedValue">The converted value if the conversion was successful or the default value of <c>T</c> if it failed.</param>
|
||||
/// <returns>
|
||||
/// <c>true</c> if <c>initialValue</c> was converted successfully or is assignable; otherwise, <c>false</c>.
|
||||
/// </returns>
|
||||
public static bool TryConvertOrCast(object initialValue, CultureInfo culture, Type targetType, out object convertedValue)
|
||||
{
|
||||
return MiscellaneousUtils.TryAction<object>(delegate { return ConvertOrCast(initialValue, culture, targetType); }, out convertedValue);
|
||||
}
|
||||
#endregion
|
||||
|
||||
private static object EnsureTypeAssignable(object value, Type initialType, Type targetType)
|
||||
{
|
||||
Type valueType = (value != null) ? value.GetType() : null;
|
||||
|
||||
if (value != null && targetType.IsAssignableFrom(valueType))
|
||||
return value;
|
||||
else if (value == null && ReflectionUtils.IsNullable(targetType))
|
||||
return null;
|
||||
else
|
||||
throw new Exception(string.Format("Could not cast or convert from {0} to {1}.",
|
||||
(initialType != null) ? initialType.ToString() : "{null}", targetType));
|
||||
}
|
||||
|
||||
#if !SILVERLIGHT
|
||||
public static object ToValue(INullable nullableValue)
|
||||
{
|
||||
if (nullableValue == null)
|
||||
return null;
|
||||
else if (nullableValue is SqlInt32)
|
||||
return ToValue((SqlInt32)nullableValue);
|
||||
else if (nullableValue is SqlInt64)
|
||||
return ToValue((SqlInt64)nullableValue);
|
||||
else if (nullableValue is SqlBoolean)
|
||||
return ToValue((SqlBoolean)nullableValue);
|
||||
else if (nullableValue is SqlString)
|
||||
return ToValue((SqlString)nullableValue);
|
||||
else if (nullableValue is SqlDateTime)
|
||||
return ToValue((SqlDateTime)nullableValue);
|
||||
|
||||
throw new Exception(string.Format("Unsupported INullable type: {0}", nullableValue.GetType()));
|
||||
}
|
||||
#endif
|
||||
|
||||
private static TypeConverter GetConverter(Type t)
|
||||
{
|
||||
ValidationUtils.ArgumentNotNull(t, "t");
|
||||
|
||||
#if !SILVERLIGHT
|
||||
return TypeDescriptor.GetConverter(t);
|
||||
#else
|
||||
object[] customAttributes = t.GetCustomAttributes(typeof(TypeConverterAttribute), true);
|
||||
if (customAttributes.Length != 1)
|
||||
return null;
|
||||
|
||||
TypeConverterAttribute typeConverterAttribute = (TypeConverterAttribute)customAttributes[0];
|
||||
return (Activator.CreateInstance(Type.GetType(typeConverterAttribute.ConverterTypeName)) as TypeConverter);
|
||||
#endif
|
||||
}
|
||||
|
||||
public static bool IsInteger(object value)
|
||||
{
|
||||
switch (System.Convert.GetTypeCode(value))
|
||||
{
|
||||
case TypeCode.SByte:
|
||||
case TypeCode.Byte:
|
||||
case TypeCode.Int16:
|
||||
case TypeCode.UInt16:
|
||||
case TypeCode.Int32:
|
||||
case TypeCode.UInt32:
|
||||
case TypeCode.Int64:
|
||||
case TypeCode.UInt64:
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -27,7 +27,7 @@ namespace Newtonsoft.Json.Utilities
|
||||
case DateTimeKind.Utc:
|
||||
return XmlDateTimeSerializationMode.Utc;
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException("kind", kind, "Unexpected DateTimeKind value.");
|
||||
throw MiscellaneousUtils.CreateArgumentOutOfRangeException("kind", kind, "Unexpected DateTimeKind value.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,34 +29,12 @@ using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Web;
|
||||
using System.Collections.Generic;
|
||||
using System.Drawing;
|
||||
using System.Web.UI;
|
||||
using System.Web.UI.WebControls;
|
||||
|
||||
namespace Newtonsoft.Json.Utilities
|
||||
{
|
||||
internal static class JavaScriptUtils
|
||||
{
|
||||
public static string GetCallbackEventFunction(Page page, Control control)
|
||||
{
|
||||
string script = page.ClientScript.GetCallbackEventReference(control, "eventArgument", "eventCallback", "context", "errorCallback", true);
|
||||
|
||||
script = "function(eventArgument,eventCallback,context,errorCallback){" + script + "}";
|
||||
|
||||
return script;
|
||||
}
|
||||
|
||||
public static string GetCallbackEventFunction(Page page, Control control, string argument)
|
||||
{
|
||||
string script = page.ClientScript.GetCallbackEventReference(control, "'" + argument + "'", "eventCallback", "context", "errorCallback", true);
|
||||
|
||||
script = "function(eventCallback,context,errorCallback){" + script + "}";
|
||||
|
||||
return script;
|
||||
}
|
||||
|
||||
public static void WriteEscapedJavaScriptChar(TextWriter writer, char c, char delimiter)
|
||||
{
|
||||
switch (c)
|
||||
|
||||
@@ -12,6 +12,13 @@ namespace Newtonsoft.Json.Utilities
|
||||
|
||||
internal static class MiscellaneousUtils
|
||||
{
|
||||
public static ArgumentOutOfRangeException CreateArgumentOutOfRangeException(string paramName, object actualValue, string message)
|
||||
{
|
||||
string newMessage = message + Environment.NewLine + @"Actual value was {0}.".FormatWith(CultureInfo.InvariantCulture, actualValue);
|
||||
|
||||
return new ArgumentOutOfRangeException(paramName, newMessage);
|
||||
}
|
||||
|
||||
public static bool TryAction<T>(Creator<T> creator, out T output)
|
||||
{
|
||||
ValidationUtils.ArgumentNotNull(creator, "creator");
|
||||
|
||||
@@ -388,7 +388,7 @@ namespace Newtonsoft.Json.Utilities
|
||||
}
|
||||
catch (TargetParameterCountException e)
|
||||
{
|
||||
throw new ArgumentException("MemberInfo '{0}' has index parameters".FormatWith(CultureInfo.InvariantCulture, member.Name), "member", e);
|
||||
throw new ArgumentException("MemberInfo '{0}' has index parameters".FormatWith(CultureInfo.InvariantCulture, member.Name), e);
|
||||
}
|
||||
default:
|
||||
throw new ArgumentException("MemberInfo '{0}' is not of type FieldInfo or PropertyInfo".FormatWith(CultureInfo.InvariantCulture, CultureInfo.InvariantCulture, member.Name), "member");
|
||||
|
||||
@@ -25,7 +25,6 @@
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data.SqlTypes;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
@@ -128,21 +127,6 @@ namespace Newtonsoft.Json.Utilities
|
||||
return target + value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determines whether the SqlString is null or empty.
|
||||
/// </summary>
|
||||
/// <param name="s">The string.</param>
|
||||
/// <returns>
|
||||
/// <c>true</c> if the SqlString is null or empty; otherwise, <c>false</c>.
|
||||
/// </returns>
|
||||
public static bool IsNullOrEmpty(SqlString s)
|
||||
{
|
||||
if (s.IsNull)
|
||||
return true;
|
||||
else
|
||||
return string.IsNullOrEmpty(s.Value);
|
||||
}
|
||||
|
||||
public static bool IsNullOrEmptyOrWhiteSpace(string s)
|
||||
{
|
||||
if (string.IsNullOrEmpty(s))
|
||||
@@ -290,48 +274,6 @@ namespace Newtonsoft.Json.Utilities
|
||||
return sb.ToString();
|
||||
}
|
||||
|
||||
public static string RemoveHtml(string s)
|
||||
{
|
||||
return RemoveHtmlInternal(s, null);
|
||||
}
|
||||
|
||||
public static string RemoveHtml(string s, IList<string> removeTags)
|
||||
{
|
||||
if (removeTags == null)
|
||||
throw new ArgumentNullException("removeTags");
|
||||
|
||||
return RemoveHtmlInternal(s, removeTags);
|
||||
}
|
||||
|
||||
private static string RemoveHtmlInternal(string s, IList<string> removeTags)
|
||||
{
|
||||
List<string> removeTagsUpper = null;
|
||||
|
||||
if (removeTags != null)
|
||||
{
|
||||
removeTagsUpper = new List<string>(removeTags.Count);
|
||||
|
||||
foreach (string tag in removeTags)
|
||||
{
|
||||
removeTagsUpper.Add(tag.ToUpperInvariant());
|
||||
}
|
||||
}
|
||||
|
||||
Regex anyTag = new Regex(@"<[/]{0,1}\s*(?<tag>\w*)\s*(?<attr>.*?=['""].*?[""'])*?\s*[/]{0,1}>", RegexOptions.Compiled);
|
||||
|
||||
return anyTag.Replace(s, delegate(Match match)
|
||||
{
|
||||
string tag = match.Groups["tag"].Value.ToUpperInvariant();
|
||||
|
||||
if (removeTagsUpper == null)
|
||||
return string.Empty;
|
||||
else if (removeTagsUpper.Contains(tag))
|
||||
return string.Empty;
|
||||
else
|
||||
return match.Value;
|
||||
});
|
||||
}
|
||||
|
||||
public static string Truncate(string s, int maximumLength)
|
||||
{
|
||||
return Truncate(s, maximumLength, "...");
|
||||
|
||||
@@ -101,37 +101,37 @@ namespace Newtonsoft.Json.Utilities
|
||||
public static void ArgumentNotNegative(int value, string parameterName)
|
||||
{
|
||||
if (value <= 0)
|
||||
throw new ArgumentOutOfRangeException(parameterName, value, "Argument cannot be negative.");
|
||||
throw MiscellaneousUtils.CreateArgumentOutOfRangeException(parameterName, value, "Argument cannot be negative.");
|
||||
}
|
||||
|
||||
public static void ArgumentNotNegative(int value, string parameterName, string message)
|
||||
{
|
||||
if (value <= 0)
|
||||
throw new ArgumentOutOfRangeException(parameterName, value, message);
|
||||
throw MiscellaneousUtils.CreateArgumentOutOfRangeException(parameterName, value, message);
|
||||
}
|
||||
|
||||
public static void ArgumentNotZero(int value, string parameterName)
|
||||
{
|
||||
if (value == 0)
|
||||
throw new ArgumentOutOfRangeException(parameterName, value, "Argument cannot be zero.");
|
||||
throw MiscellaneousUtils.CreateArgumentOutOfRangeException(parameterName, value, "Argument cannot be zero.");
|
||||
}
|
||||
|
||||
public static void ArgumentNotZero(int value, string parameterName, string message)
|
||||
{
|
||||
if (value == 0)
|
||||
throw new ArgumentOutOfRangeException(parameterName, value, message);
|
||||
throw MiscellaneousUtils.CreateArgumentOutOfRangeException(parameterName, value, message);
|
||||
}
|
||||
|
||||
public static void ArgumentIsPositive<T>(T value, string parameterName) where T : struct, IComparable<T>
|
||||
{
|
||||
if (value.CompareTo(default(T)) != 1)
|
||||
throw new ArgumentOutOfRangeException(parameterName, value, "Positive number required.");
|
||||
throw MiscellaneousUtils.CreateArgumentOutOfRangeException(parameterName, value, "Positive number required.");
|
||||
}
|
||||
|
||||
public static void ArgumentIsPositive(int value, string parameterName, string message)
|
||||
{
|
||||
if (value > 0)
|
||||
throw new ArgumentOutOfRangeException(parameterName, value, message);
|
||||
throw MiscellaneousUtils.CreateArgumentOutOfRangeException(parameterName, value, message);
|
||||
}
|
||||
|
||||
public static void ObjectNotDisposed(bool disposed, Type objectType)
|
||||
|
||||
Reference in New Issue
Block a user