From 0ae1c636bdf7eb372bc59abf5f620119882e691d Mon Sep 17 00:00:00 2001 From: JamesNK Date: Sun, 8 Jun 2008 10:41:20 +0000 Subject: [PATCH] Work in progress: -Silverlight build projects -JObject now implements IDictionary, JArray now implements IList --- .../Newtonsoft.Json.Silverlight.csproj | 225 +++++++++++ .../Converters/XmlNodeConverterTest.cs | 4 +- .../JavaScriptConvertTest.cs | 2 +- .../JsonSerializerTest.cs | 10 +- .../JsonTextWriterTest.cs | 1 - Src/Newtonsoft.Json.Tests/Linq/JTokenTests.cs | 4 - .../Linq/LinqToJsonTest.cs | 2 +- Src/Newtonsoft.Json.Tests/TestFixtureBase.cs | 10 +- Src/Newtonsoft.Json.sln | 12 + .../Converters/HtmlColorConverter.cs | 2 + .../Converters/XmlNodeConverter.cs | 2 + Src/Newtonsoft.Json/Identifier.cs | 3 - Src/Newtonsoft.Json/JavaScriptConvert.cs | 7 +- Src/Newtonsoft.Json/JsonHandler.cs | 29 +- Src/Newtonsoft.Json/JsonReader.cs | 2 +- Src/Newtonsoft.Json/JsonSerializer.cs | 30 +- Src/Newtonsoft.Json/JsonTextReader.cs | 2 +- Src/Newtonsoft.Json/JsonWriter.cs | 2 +- Src/Newtonsoft.Json/Linq/JArray.cs | 113 +++++- Src/Newtonsoft.Json/Linq/JContainer.cs | 2 +- Src/Newtonsoft.Json/Linq/JObject.cs | 129 +++++- Src/Newtonsoft.Json/Linq/JToken.cs | 13 +- Src/Newtonsoft.Json/Linq/JValue.cs | 4 +- Src/Newtonsoft.Json/Linq/JsonTokenReader.cs | 4 +- Src/Newtonsoft.Json/Newtonsoft.Json.csproj | 1 + .../Utilities/CollectionUtils.cs | 14 +- Src/Newtonsoft.Json/Utilities/ConvertUtils.cs | 368 ++++++++++++++++++ .../Utilities/DateTimeUtils.cs | 2 +- .../Utilities/JavaScriptUtils.cs | 22 -- .../Utilities/MiscellaneousUtils.cs | 7 + .../Utilities/ReflectionUtils.cs | 2 +- Src/Newtonsoft.Json/Utilities/StringUtils.cs | 58 --- .../Utilities/ValidationUtils.cs | 12 +- 33 files changed, 943 insertions(+), 157 deletions(-) create mode 100644 Src/Newtonsoft.Json.Silverlight/Newtonsoft.Json.Silverlight.csproj create mode 100644 Src/Newtonsoft.Json/Utilities/ConvertUtils.cs diff --git a/Src/Newtonsoft.Json.Silverlight/Newtonsoft.Json.Silverlight.csproj b/Src/Newtonsoft.Json.Silverlight/Newtonsoft.Json.Silverlight.csproj new file mode 100644 index 00000000..0e2e7b4e --- /dev/null +++ b/Src/Newtonsoft.Json.Silverlight/Newtonsoft.Json.Silverlight.csproj @@ -0,0 +1,225 @@ + + + Debug + AnyCPU + 9.0.21022 + 2.0 + {DC3C6F3D-2CA1-4278-9B79-63770FB3EA2D} + {A1591282-1198-4647-A2B1-27E5FF5F6F3B};{fae04ec0-301f-11d3-bf4b-00c04f79efbc} + Library + Properties + Newtonsoft.Json.Silverlight + Newtonsoft.Json.Silverlight + v3.5 + false + true + false + + + true + full + false + Bin\Debug + DEBUG;TRACE;SILVERLIGHT + true + true + prompt + 4 + + + pdbonly + true + Bin\Release + TRACE;SILVERLIGHT + true + true + prompt + 4 + + + + + + + + + + + + + Converters\HtmlColorConverter.cs + + + Converters\IsoDateTimeConverter.cs + + + Converters\JavaScriptDateTimeConverter.cs + + + Converters\JsonDateTimeSerializationMode.cs + + + Converters\XmlNodeConverter.cs + + + Identifier.cs + + + JavaScriptConvert.cs + + + JsonConverter.cs + + + JsonConverterCollection.cs + + + JsonHandler.cs + + + JsonIgnoreAttribute.cs + + + JsonObjectAttribute.cs + + + JsonPropertyAttribute.cs + + + JsonReader.cs + + + JsonReaderException.cs + + + JsonSerializationException.cs + + + JsonSerializer.cs + + + JsonTextReader.cs + + + JsonTextWriter.cs + + + JsonToken.cs + + + JsonWriter.cs + + + JsonWriterException.cs + + + Linq\Extensions.cs + + + Linq\JArray.cs + + + Linq\JConstructor.cs + + + Linq\JContainer.cs + + + Linq\JEnumerable.cs + + + Linq\JObject.cs + + + Linq\JProperty.cs + + + Linq\JsonTokenReader.cs + + + Linq\JsonTokenType.cs + + + Linq\JsonTokenWriter.cs + + + Linq\JToken.cs + + + Linq\JValue.cs + + + MemberMapping.cs + + + MemberMappingCollection.cs + + + MemberSerialization.cs + + + MissingMemberHandling.cs + + + NullValueHandling.cs + + + ObjectCreationHandling.cs + + + ReferenceLoopHandling.cs + + + StringBuffer.cs + + + Utilities\CollectionUtils.cs + + + Utilities\ConvertUtils.cs + + + Utilities\DateTimeUtils.cs + + + Utilities\DictionaryWrapper.cs + + + Utilities\JavaScriptUtils.cs + + + Utilities\ListWrapper.cs + + + Utilities\MathUtils.cs + + + Utilities\MiscellaneousUtils.cs + + + Utilities\ReflectionUtils.cs + + + Utilities\StringUtils.cs + + + Utilities\ValidationUtils.cs + + + + + + + + + + + + + \ No newline at end of file diff --git a/Src/Newtonsoft.Json.Tests/Converters/XmlNodeConverterTest.cs b/Src/Newtonsoft.Json.Tests/Converters/XmlNodeConverterTest.cs index d9a0bf96..d2501987 100644 --- a/Src/Newtonsoft.Json.Tests/Converters/XmlNodeConverterTest.cs +++ b/Src/Newtonsoft.Json.Tests/Converters/XmlNodeConverterTest.cs @@ -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); } } -} \ No newline at end of file +} +#endif \ No newline at end of file diff --git a/Src/Newtonsoft.Json.Tests/JavaScriptConvertTest.cs b/Src/Newtonsoft.Json.Tests/JavaScriptConvertTest.cs index bee8354b..a6098906 100644 --- a/Src/Newtonsoft.Json.Tests/JavaScriptConvertTest.cs +++ b/Src/Newtonsoft.Json.Tests/JavaScriptConvertTest.cs @@ -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] diff --git a/Src/Newtonsoft.Json.Tests/JsonSerializerTest.cs b/Src/Newtonsoft.Json.Tests/JsonSerializerTest.cs index 38d4c025..304d685a 100644 --- a/Src/Newtonsoft.Json.Tests/JsonSerializerTest.cs +++ b/Src/Newtonsoft.Json.Tests/JsonSerializerTest.cs @@ -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 s = new Stack(); s.Push(1); s.Push(2); s.Push(3); diff --git a/Src/Newtonsoft.Json.Tests/JsonTextWriterTest.cs b/Src/Newtonsoft.Json.Tests/JsonTextWriterTest.cs index 948599d6..0cf8f056 100644 --- a/Src/Newtonsoft.Json.Tests/JsonTextWriterTest.cs +++ b/Src/Newtonsoft.Json.Tests/JsonTextWriterTest.cs @@ -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 { diff --git a/Src/Newtonsoft.Json.Tests/Linq/JTokenTests.cs b/Src/Newtonsoft.Json.Tests/Linq/JTokenTests.cs index 19642d5f..03d1454f 100644 --- a/Src/Newtonsoft.Json.Tests/Linq/JTokenTests.cs +++ b/Src/Newtonsoft.Json.Tests/Linq/JTokenTests.cs @@ -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); diff --git a/Src/Newtonsoft.Json.Tests/Linq/LinqToJsonTest.cs b/Src/Newtonsoft.Json.Tests/Linq/LinqToJsonTest.cs index 0a226fce..d74dbe23 100644 --- a/Src/Newtonsoft.Json.Tests/Linq/LinqToJsonTest.cs +++ b/Src/Newtonsoft.Json.Tests/Linq/LinqToJsonTest.cs @@ -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("title"); foreach (var item in postTitles) diff --git a/Src/Newtonsoft.Json.Tests/TestFixtureBase.cs b/Src/Newtonsoft.Json.Tests/TestFixtureBase.cs index e04fba57..0ba9f057 100644 --- a/Src/Newtonsoft.Json.Tests/TestFixtureBase.cs +++ b/Src/Newtonsoft.Json.Tests/TestFixtureBase.cs @@ -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"); + //} } } diff --git a/Src/Newtonsoft.Json.sln b/Src/Newtonsoft.Json.sln index 49e3a00f..600a2c9b 100644 --- a/Src/Newtonsoft.Json.sln +++ b/Src/Newtonsoft.Json.sln @@ -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 diff --git a/Src/Newtonsoft.Json/Converters/HtmlColorConverter.cs b/Src/Newtonsoft.Json/Converters/HtmlColorConverter.cs index 3027b12d..807e2cf8 100644 --- a/Src/Newtonsoft.Json/Converters/HtmlColorConverter.cs +++ b/Src/Newtonsoft.Json/Converters/HtmlColorConverter.cs @@ -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 \ No newline at end of file diff --git a/Src/Newtonsoft.Json/Converters/XmlNodeConverter.cs b/Src/Newtonsoft.Json/Converters/XmlNodeConverter.cs index 88c05316..34bd47f0 100644 --- a/Src/Newtonsoft.Json/Converters/XmlNodeConverter.cs +++ b/Src/Newtonsoft.Json/Converters/XmlNodeConverter.cs @@ -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 \ No newline at end of file diff --git a/Src/Newtonsoft.Json/Identifier.cs b/Src/Newtonsoft.Json/Identifier.cs index bccbe438..91e5d6ee 100644 --- a/Src/Newtonsoft.Json/Identifier.cs +++ b/Src/Newtonsoft.Json/Identifier.cs @@ -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 diff --git a/Src/Newtonsoft.Json/JavaScriptConvert.cs b/Src/Newtonsoft.Json/JavaScriptConvert.cs index a20c4dc5..ec7d34fc 100644 --- a/Src/Newtonsoft.Json/JavaScriptConvert.cs +++ b/Src/Newtonsoft.Json/JavaScriptConvert.cs @@ -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 /// A Json string representation of the . public static string ToString(Enum value) { - return Enum.Format(value.GetType(), value, "D"); + return value.ToString("D"); } /// @@ -515,6 +512,7 @@ namespace Newtonsoft.Json return deserializedValue; } +#if !SILVERLIGHT /// /// Serializes the XML node to a JSON string. /// @@ -538,5 +536,6 @@ namespace Newtonsoft.Json return (XmlDocument)DeserializeObject(value, typeof(XmlDocument), converter); } +#endif } } \ No newline at end of file diff --git a/Src/Newtonsoft.Json/JsonHandler.cs b/Src/Newtonsoft.Json/JsonHandler.cs index ca2a93f7..557932f8 100644 --- a/Src/Newtonsoft.Json/JsonHandler.cs +++ b/Src/Newtonsoft.Json/JsonHandler.cs @@ -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; } } } -} \ No newline at end of file +} +#endif \ No newline at end of file diff --git a/Src/Newtonsoft.Json/JsonReader.cs b/Src/Newtonsoft.Json/JsonReader.cs index f2814548..7e926a6b 100644 --- a/Src/Newtonsoft.Json/JsonReader.cs +++ b/Src/Newtonsoft.Json/JsonReader.cs @@ -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."); } } diff --git a/Src/Newtonsoft.Json/JsonSerializer.cs b/Src/Newtonsoft.Json/JsonSerializer.cs index 745593df..3238a4d5 100644 --- a/Src/Newtonsoft.Json/JsonSerializer.cs +++ b/Src/Newtonsoft.Json/JsonSerializer.cs @@ -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); diff --git a/Src/Newtonsoft.Json/JsonTextReader.cs b/Src/Newtonsoft.Json/JsonTextReader.cs index 02330d6a..0d2aac88 100644 --- a/Src/Newtonsoft.Json/JsonTextReader.cs +++ b/Src/Newtonsoft.Json/JsonTextReader.cs @@ -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; diff --git a/Src/Newtonsoft.Json/JsonWriter.cs b/Src/Newtonsoft.Json/JsonWriter.cs index 53ca7526..856c5957 100644 --- a/Src/Newtonsoft.Json/JsonWriter.cs +++ b/Src/Newtonsoft.Json/JsonWriter.cs @@ -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)))); diff --git a/Src/Newtonsoft.Json/Linq/JArray.cs b/Src/Newtonsoft.Json/Linq/JArray.cs index b49d97b3..1441a6c3 100644 --- a/Src/Newtonsoft.Json/Linq/JArray.cs +++ b/Src/Newtonsoft.Json/Linq/JArray.cs @@ -36,7 +36,7 @@ namespace Newtonsoft.Json.Linq /// /// Represents a JSON array. /// - public class JArray : JContainer + public class JArray : JContainer, IList { /// /// Gets the node type for this . @@ -92,15 +92,6 @@ namespace Newtonsoft.Json.Linq return new JArray(this); } - /// - /// Returns a count of this token's child tokens. - /// - /// A count of this token's child tokens. - public int Count() - { - return Children().Count(); - } - /// /// Loads an from a . /// @@ -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 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 Members + + void ICollection.Add(JToken item) + { + Add((object)item); + } + + void ICollection.Clear() + { + RemoveAll(); + } + + public bool Contains(JToken item) + { + return Children().Contains(item); + } + + void ICollection.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.IsReadOnly + { + get { return false; } + } + + bool ICollection.Remove(JToken item) + { + if (!((ICollection)this).Contains(item)) + return false; + + item.Remove(); + return true; + } + + #endregion } } \ No newline at end of file diff --git a/Src/Newtonsoft.Json/Linq/JContainer.cs b/Src/Newtonsoft.Json/Linq/JContainer.cs index 16eb532e..1ba028ab 100644 --- a/Src/Newtonsoft.Json/Linq/JContainer.cs +++ b/Src/Newtonsoft.Json/Linq/JContainer.cs @@ -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) diff --git a/Src/Newtonsoft.Json/Linq/JObject.cs b/Src/Newtonsoft.Json/Linq/JObject.cs index f8540a0b..0aa031eb 100644 --- a/Src/Newtonsoft.Json/Linq/JObject.cs +++ b/Src/Newtonsoft.Json/Linq/JObject.cs @@ -36,7 +36,7 @@ namespace Newtonsoft.Json.Linq /// /// Represents a JSON object. /// - public class JObject : JContainer + public class JObject : JContainer, IDictionary { /// /// Initializes a new instance of the 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)); + } } /// @@ -231,5 +249,114 @@ namespace Newtonsoft.Json.Linq writer.WriteEndObject(); } + + #region IDictionary Members + public void Add(string propertyName, JToken value) + { + Add(new JProperty(propertyName, value)); + } + + bool IDictionary.ContainsKey(string key) + { + return (Property(key) != null); + } + + ICollection IDictionary.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 IDictionary.Values + { + get { throw new NotImplementedException(); } + } + + #endregion + + #region ICollection> Members + + void ICollection>.Add(KeyValuePair item) + { + Add(new JProperty(item.Key, item.Value)); + } + + void ICollection>.Clear() + { + RemoveAll(); + } + + bool ICollection>.Contains(KeyValuePair item) + { + JProperty property = Property(item.Key); + if (property == null) + return false; + + return (property.Value == item.Value); + } + + void ICollection>.CopyTo(KeyValuePair[] array, int arrayIndex) + { + int index = 0; + foreach (JProperty property in Properties()) + { + array[arrayIndex + index] = new KeyValuePair(property.Name, property.Value); + } + } + + public int Count + { + get { return Children().Count(); } + } + + bool ICollection>.IsReadOnly + { + get { return false; } + } + + bool ICollection>.Remove(KeyValuePair item) + { + if (!((ICollection>)this).Contains(item)) + return false; + + ((IDictionary)this).Remove(item.Key); + return true; + } + + #endregion + + #region IEnumerable> Members + + IEnumerator> IEnumerable>.GetEnumerator() + { + foreach (JProperty property in Properties()) + { + yield return new KeyValuePair(property.Name, property.Value); + } + } + + #endregion } } \ No newline at end of file diff --git a/Src/Newtonsoft.Json/Linq/JToken.cs b/Src/Newtonsoft.Json/Linq/JToken.cs index b75a345d..d1d612ad 100644 --- a/Src/Newtonsoft.Json/Linq/JToken.cs +++ b/Src/Newtonsoft.Json/Linq/JToken.cs @@ -31,13 +31,14 @@ using System.IO; using Newtonsoft.Json.Utilities; using System.Diagnostics; using System.Globalization; +using System.Collections; namespace Newtonsoft.Json.Linq { /// /// Represents an abstract JSON token. /// - public abstract class JToken + public abstract class JToken : IEnumerable { 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)this).GetEnumerator(); + } + + IEnumerator IEnumerable.GetEnumerator() + { + return Children().GetEnumerator(); + } } } \ No newline at end of file diff --git a/Src/Newtonsoft.Json/Linq/JValue.cs b/Src/Newtonsoft.Json/Linq/JValue.cs index 930c0b1d..a72d7b2f 100644 --- a/Src/Newtonsoft.Json/Linq/JValue.cs +++ b/Src/Newtonsoft.Json/Linq/JValue.cs @@ -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."); } } } diff --git a/Src/Newtonsoft.Json/Linq/JsonTokenReader.cs b/Src/Newtonsoft.Json/Linq/JsonTokenReader.cs index 921b7f97..bc518d9a 100644 --- a/Src/Newtonsoft.Json/Linq/JsonTokenReader.cs +++ b/Src/Newtonsoft.Json/Linq/JsonTokenReader.cs @@ -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."); } } } diff --git a/Src/Newtonsoft.Json/Newtonsoft.Json.csproj b/Src/Newtonsoft.Json/Newtonsoft.Json.csproj index b8008a4e..3204f74e 100644 --- a/Src/Newtonsoft.Json/Newtonsoft.Json.csproj +++ b/Src/Newtonsoft.Json/Newtonsoft.Json.csproj @@ -112,6 +112,7 @@ + diff --git a/Src/Newtonsoft.Json/Utilities/CollectionUtils.cs b/Src/Newtonsoft.Json/Utilities/CollectionUtils.cs index 94f123e7..56736cca 100644 --- a/Src/Newtonsoft.Json/Utilities/CollectionUtils.cs +++ b/Src/Newtonsoft.Json/Utilities/CollectionUtils.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(); 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)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; + } } } \ No newline at end of file diff --git a/Src/Newtonsoft.Json/Utilities/ConvertUtils.cs b/Src/Newtonsoft.Json/Utilities/ConvertUtils.cs new file mode 100644 index 00000000..ecb03a4d --- /dev/null +++ b/Src/Newtonsoft.Json/Utilities/ConvertUtils.cs @@ -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 + /// + /// Converts the value to the specified type. + /// + /// The type to convert the value to. + /// The value to convert. + /// The converted type. + public static T Convert(object initialValue) + { + return Convert(initialValue, CultureInfo.CurrentCulture); + } + + /// + /// Converts the value to the specified type. + /// + /// The type to convert the value to. + /// The value to convert. + /// The culture to use when converting. + /// The converted type. + public static T Convert(object initialValue, CultureInfo culture) + { + return (T)Convert(initialValue, culture, typeof(T)); + } + + /// + /// Converts the value to the specified type. + /// + /// The value to convert. + /// The culture to use when converting. + /// The type to convert the value to. + /// The converted type. + 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 + /// + /// Converts the value to the specified type. + /// + /// The type to convert the value to. + /// The value to convert. + /// The converted value if the conversion was successful or the default value of T if it failed. + /// + /// true if initialValue was converted successfully; otherwise, false. + /// + public static bool TryConvert(object initialValue, out T convertedValue) + { + return TryConvert(initialValue, CultureInfo.CurrentCulture, out convertedValue); + } + + /// + /// Converts the value to the specified type. + /// + /// The type to convert the value to. + /// The value to convert. + /// The culture to use when converting. + /// The converted value if the conversion was successful or the default value of T if it failed. + /// + /// true if initialValue was converted successfully; otherwise, false. + /// + public static bool TryConvert(object initialValue, CultureInfo culture, out T convertedValue) + { + return MiscellaneousUtils.TryAction(delegate + { + object tempConvertedValue; + TryConvert(initialValue, CultureInfo.CurrentCulture, typeof(T), out tempConvertedValue); + + return (T)tempConvertedValue; + }, out convertedValue); + } + + /// + /// Converts the value to the specified type. + /// + /// The value to convert. + /// The culture to use when converting. + /// The type to convert the value to. + /// The converted value if the conversion was successful or the default value of T if it failed. + /// + /// true if initialValue was converted successfully; otherwise, false. + /// + public static bool TryConvert(object initialValue, CultureInfo culture, Type targetType, out object convertedValue) + { + return MiscellaneousUtils.TryAction(delegate { return Convert(initialValue, culture, targetType); }, out convertedValue); + } + #endregion + + #region ConvertOrCast + /// + /// 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. + /// + /// The type to convert or cast the value to. + /// The value to convert. + /// The converted type. If conversion was unsuccessful, the initial value is returned if assignable to the target type + public static T ConvertOrCast(object initialValue) + { + return ConvertOrCast(initialValue, CultureInfo.CurrentCulture); + } + + /// + /// 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. + /// + /// The type to convert or cast the value to. + /// The value to convert. + /// The culture to use when converting. + /// The converted type. If conversion was unsuccessful, the initial value is returned if assignable to the target type + public static T ConvertOrCast(object initialValue, CultureInfo culture) + { + return (T)ConvertOrCast(initialValue, culture, typeof(T)); + } + + /// + /// 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. + /// + /// The value to convert. + /// The culture to use when converting. + /// The type to convert or cast the value to. + /// + /// The converted type. If conversion was unsuccessful, the initial value + /// is returned if assignable to the target type. + /// + 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 + /// + /// 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. + /// + /// The type to convert the value to. + /// The value to convert. + /// The converted value if the conversion was successful or the default value of T if it failed. + /// + /// true if initialValue was converted successfully or is assignable; otherwise, false. + /// + public static bool TryConvertOrCast(object initialValue, out T convertedValue) + { + return TryConvertOrCast(initialValue, CultureInfo.CurrentCulture, out convertedValue); + } + + /// + /// 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. + /// + /// The type to convert the value to. + /// The value to convert. + /// The culture to use when converting. + /// The converted value if the conversion was successful or the default value of T if it failed. + /// + /// true if initialValue was converted successfully or is assignable; otherwise, false. + /// + public static bool TryConvertOrCast(object initialValue, CultureInfo culture, out T convertedValue) + { + return MiscellaneousUtils.TryAction(delegate + { + object tempConvertedValue; + TryConvertOrCast(initialValue, CultureInfo.CurrentCulture, typeof(T), out tempConvertedValue); + + return (T)tempConvertedValue; + }, out convertedValue); + } + + /// + /// 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. + /// + /// The value to convert. + /// The culture to use when converting. + /// The type to convert the value to. + /// The converted value if the conversion was successful or the default value of T if it failed. + /// + /// true if initialValue was converted successfully or is assignable; otherwise, false. + /// + public static bool TryConvertOrCast(object initialValue, CultureInfo culture, Type targetType, out object convertedValue) + { + return MiscellaneousUtils.TryAction(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; + } + } + } +} diff --git a/Src/Newtonsoft.Json/Utilities/DateTimeUtils.cs b/Src/Newtonsoft.Json/Utilities/DateTimeUtils.cs index f968c927..33c11d50 100644 --- a/Src/Newtonsoft.Json/Utilities/DateTimeUtils.cs +++ b/Src/Newtonsoft.Json/Utilities/DateTimeUtils.cs @@ -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."); } } } diff --git a/Src/Newtonsoft.Json/Utilities/JavaScriptUtils.cs b/Src/Newtonsoft.Json/Utilities/JavaScriptUtils.cs index fde0bb92..22a1e58a 100644 --- a/Src/Newtonsoft.Json/Utilities/JavaScriptUtils.cs +++ b/Src/Newtonsoft.Json/Utilities/JavaScriptUtils.cs @@ -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) diff --git a/Src/Newtonsoft.Json/Utilities/MiscellaneousUtils.cs b/Src/Newtonsoft.Json/Utilities/MiscellaneousUtils.cs index c365b75a..df33be03 100644 --- a/Src/Newtonsoft.Json/Utilities/MiscellaneousUtils.cs +++ b/Src/Newtonsoft.Json/Utilities/MiscellaneousUtils.cs @@ -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(Creator creator, out T output) { ValidationUtils.ArgumentNotNull(creator, "creator"); diff --git a/Src/Newtonsoft.Json/Utilities/ReflectionUtils.cs b/Src/Newtonsoft.Json/Utilities/ReflectionUtils.cs index 79799ad3..27eae652 100644 --- a/Src/Newtonsoft.Json/Utilities/ReflectionUtils.cs +++ b/Src/Newtonsoft.Json/Utilities/ReflectionUtils.cs @@ -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"); diff --git a/Src/Newtonsoft.Json/Utilities/StringUtils.cs b/Src/Newtonsoft.Json/Utilities/StringUtils.cs index 67073557..afbc6beb 100644 --- a/Src/Newtonsoft.Json/Utilities/StringUtils.cs +++ b/Src/Newtonsoft.Json/Utilities/StringUtils.cs @@ -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; } - /// - /// Determines whether the SqlString is null or empty. - /// - /// The string. - /// - /// true if the SqlString is null or empty; otherwise, false. - /// - 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 removeTags) - { - if (removeTags == null) - throw new ArgumentNullException("removeTags"); - - return RemoveHtmlInternal(s, removeTags); - } - - private static string RemoveHtmlInternal(string s, IList removeTags) - { - List removeTagsUpper = null; - - if (removeTags != null) - { - removeTagsUpper = new List(removeTags.Count); - - foreach (string tag in removeTags) - { - removeTagsUpper.Add(tag.ToUpperInvariant()); - } - } - - Regex anyTag = new Regex(@"<[/]{0,1}\s*(?\w*)\s*(?.*?=['""].*?[""'])*?\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, "..."); diff --git a/Src/Newtonsoft.Json/Utilities/ValidationUtils.cs b/Src/Newtonsoft.Json/Utilities/ValidationUtils.cs index 9796fb63..50654480 100644 --- a/Src/Newtonsoft.Json/Utilities/ValidationUtils.cs +++ b/Src/Newtonsoft.Json/Utilities/ValidationUtils.cs @@ -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 value, string parameterName) where T : struct, IComparable { 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)