From c9ec2289421792fc28a6a37e700e8da633a3fe92 Mon Sep 17 00:00:00 2001 From: JamesNK Date: Thu, 1 Jul 2010 10:27:18 +0000 Subject: [PATCH] -Removed HtmlColorConverter to remove System.Drawing.dll dependency -Fixed culture invariance -Fixed serialization attributes to work in .NET 2.0 --- .../Converters/HtmlColorConverterTests.cs | 64 -------------- .../Newtonsoft.Json.Tests.Compact.csproj | 4 +- .../Newtonsoft.Json.Tests.Net20.csproj | 4 +- .../Newtonsoft.Json.Tests.Silverlight.csproj | 4 +- .../Newtonsoft.Json.Tests.csproj | 4 +- .../Serialization/JsonSerializerTest.cs | 27 ++++++ .../SerializationEventAttributeTests.cs | 2 +- .../Serialization/TypeNameHandlingTests.cs | 57 +++++++++++++ .../TestObjects/ContentBaseClass.cs | 11 +++ .../TestObjects/ContentSubClass.cs | 18 ++++ .../TestObjects/HolderClass.cs | 21 +++++ .../SerializationEventTestObject.cs | 2 +- .../Converters/HtmlColorConverter.cs | 85 ------------------- .../Converters/RegexConverter.cs | 3 +- .../Newtonsoft.Json.Compact.csproj | 1 - .../Newtonsoft.Json.Net20.csproj | 2 - .../Newtonsoft.Json.Silverlight.csproj | 1 - Src/Newtonsoft.Json/Newtonsoft.Json.csproj | 2 - .../Serialization/DefaultContractResolver.cs | 2 +- .../JsonSerializerInternalWriter.cs | 5 +- 20 files changed, 155 insertions(+), 164 deletions(-) delete mode 100644 Src/Newtonsoft.Json.Tests/Converters/HtmlColorConverterTests.cs create mode 100644 Src/Newtonsoft.Json.Tests/TestObjects/ContentBaseClass.cs create mode 100644 Src/Newtonsoft.Json.Tests/TestObjects/ContentSubClass.cs create mode 100644 Src/Newtonsoft.Json.Tests/TestObjects/HolderClass.cs delete mode 100644 Src/Newtonsoft.Json/Converters/HtmlColorConverter.cs diff --git a/Src/Newtonsoft.Json.Tests/Converters/HtmlColorConverterTests.cs b/Src/Newtonsoft.Json.Tests/Converters/HtmlColorConverterTests.cs deleted file mode 100644 index 8bafa43b..00000000 --- a/Src/Newtonsoft.Json.Tests/Converters/HtmlColorConverterTests.cs +++ /dev/null @@ -1,64 +0,0 @@ -#region License -// Copyright (c) 2007 James Newton-King -// -// Permission is hereby granted, free of charge, to any person -// obtaining a copy of this software and associated documentation -// files (the "Software"), to deal in the Software without -// restriction, including without limitation the rights to use, -// copy, modify, merge, publish, distribute, sublicense, and/or sell -// copies of the Software, and to permit persons to whom the -// Software is furnished to do so, subject to the following -// conditions: -// -// The above copyright notice and this permission notice shall be -// included in all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES -// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT -// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, -// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR -// OTHER DEALINGS IN THE SOFTWARE. -#endregion - -#if !SILVERLIGHT && !PocketPC -using System; -using System.Collections.Generic; -using System.Drawing; -using System.Linq; -using System.Text; -using Newtonsoft.Json.Converters; -using NUnit.Framework; - -namespace Newtonsoft.Json.Tests.Converters -{ - public class HtmlColorConverterTests : TestFixtureBase - { - [Test] - public void WriteJsonTest() - { - string json; - - json = JsonConvert.SerializeObject(Color.DarkGray, new HtmlColorConverter()); - Assert.AreEqual(@"""DarkGray""", json); - - json = JsonConvert.SerializeObject(Color.FromArgb(255, 1, 2, 3), new HtmlColorConverter()); - Assert.AreEqual(@"""#010203""", json); - } - - [Test] - public void ReadJsonTest() - { - string json = @"""#010203"""; - - Color c = JsonConvert.DeserializeObject(json, new HtmlColorConverter()); - Assert.AreEqual(c.A, 255); - Assert.AreEqual(c.R, 1); - Assert.AreEqual(c.G, 2); - Assert.AreEqual(c.B, 3); - } - } -} -#endif \ No newline at end of file diff --git a/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.Compact.csproj b/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.Compact.csproj index 5b44a1b9..1d4cc07b 100644 --- a/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.Compact.csproj +++ b/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.Compact.csproj @@ -65,7 +65,6 @@ - @@ -104,6 +103,8 @@ + + @@ -118,6 +119,7 @@ + diff --git a/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.Net20.csproj b/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.Net20.csproj index c08454b2..7e43745e 100644 --- a/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.Net20.csproj +++ b/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.Net20.csproj @@ -69,7 +69,6 @@ - @@ -89,11 +88,14 @@ + + + diff --git a/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.Silverlight.csproj b/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.Silverlight.csproj index f337bdc2..4de04665 100644 --- a/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.Silverlight.csproj +++ b/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.Silverlight.csproj @@ -74,7 +74,6 @@ - @@ -115,6 +114,8 @@ + + @@ -129,6 +130,7 @@ + diff --git a/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.csproj b/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.csproj index 54352dbd..17695f7a 100644 --- a/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.csproj +++ b/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.csproj @@ -91,7 +91,6 @@ - @@ -128,7 +127,10 @@ + + + diff --git a/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs b/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs index 3ffe737d..91d60670 100644 --- a/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs +++ b/Src/Newtonsoft.Json.Tests/Serialization/JsonSerializerTest.cs @@ -51,6 +51,7 @@ using System.Reflection; #if !NET20 && !SILVERLIGHT using System.Xml.Linq; using System.Text.RegularExpressions; +using System.Collections.Specialized; #endif namespace Newtonsoft.Json.Tests.Serialization @@ -3463,5 +3464,31 @@ keyword such as type of business."" Assert.AreEqual(0, z[0].Prop1.Length); Assert.AreEqual(0, z[1].Prop1.Length); } + +#if !NET20 && !PocketPC && !SILVERLIGHT + public class StringDictionaryTestClass + { + public StringDictionary StringDictionaryProperty { get; set; } + } + + [Test] + [ExpectedException(typeof(Exception), ExpectedMessage = "Cannot create and populate list type System.Collections.Specialized.StringDictionary.")] + public void StringDictionaryTest() + { + StringDictionaryTestClass s1 = new StringDictionaryTestClass() + { + StringDictionaryProperty = new StringDictionary() + { + {"1", "One"}, + {"2", "II"}, + {"3", "3"} + } + }; + + string json = JsonConvert.SerializeObject(s1, Formatting.Indented); + + JsonConvert.DeserializeObject(json); + } +#endif } } \ No newline at end of file diff --git a/Src/Newtonsoft.Json.Tests/Serialization/SerializationEventAttributeTests.cs b/Src/Newtonsoft.Json.Tests/Serialization/SerializationEventAttributeTests.cs index 7c15d94d..1041002c 100644 --- a/Src/Newtonsoft.Json.Tests/Serialization/SerializationEventAttributeTests.cs +++ b/Src/Newtonsoft.Json.Tests/Serialization/SerializationEventAttributeTests.cs @@ -23,7 +23,7 @@ // OTHER DEALINGS IN THE SOFTWARE. #endregion -#if !PocketPC && !NET20 +#if !PocketPC using System; using System.IO; using System.Linq; diff --git a/Src/Newtonsoft.Json.Tests/Serialization/TypeNameHandlingTests.cs b/Src/Newtonsoft.Json.Tests/Serialization/TypeNameHandlingTests.cs index 3ffc80bb..382e0d2e 100644 --- a/Src/Newtonsoft.Json.Tests/Serialization/TypeNameHandlingTests.cs +++ b/Src/Newtonsoft.Json.Tests/Serialization/TypeNameHandlingTests.cs @@ -34,6 +34,7 @@ using NUnit.Framework; using Newtonsoft.Json.Utilities; using System.Net; using System.Runtime.Serialization; +using System.IO; namespace Newtonsoft.Json.Tests.Serialization { @@ -459,5 +460,61 @@ namespace Newtonsoft.Json.Tests.Serialization } } #endif + + //[Test] + //[Ignore("TODO")] + //public void CollectionWithAbstractItems() + //{ + // string Text = ""; + + // HolderClass testObject = new HolderClass(); + // testObject.TestMember = new ContentSubClass("First One"); + // testObject.AnotherTestMember = new Dictionary>(); + // testObject.AnotherTestMember.Add(1, new List()); + // testObject.AnotherTestMember[1].Add(new ContentSubClass("Second One")); + // testObject.AThirdTestMember = new ContentSubClass("Third One"); + + // HolderClass anotherTestObject = null; + + // Newtonsoft.Json.JsonSerializer serializingTester = new Newtonsoft.Json.JsonSerializer(); + // serializingTester.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore; + + // StringWriter sw = new StringWriter(); + // using (Newtonsoft.Json.JsonTextWriter jsonWriter = new Newtonsoft.Json.JsonTextWriter(sw)) + // { + // jsonWriter.Formatting = Newtonsoft.Json.Formatting.Indented; + + // if (Text.Equals("Properties All, Serializer All")) + // serializingTester.TypeNameHandling = Newtonsoft.Json.TypeNameHandling.All; + // else if (Text.Equals("Properties All, Serializer Objects")) + // serializingTester.TypeNameHandling = Newtonsoft.Json.TypeNameHandling.Objects; + // else if (Text.Equals("Properties All, Serializer Arrays")) + // serializingTester.TypeNameHandling = Newtonsoft.Json.TypeNameHandling.Arrays; + // //otherwise leave default, which looks like it's "None" + + // serializingTester.Serialize(jsonWriter, testObject); + // } + + // string json = sw.ToString(); + + // Console.WriteLine(json); + + // StringReader sr = new StringReader(json); + + // Newtonsoft.Json.JsonSerializer deserializingTester = new Newtonsoft.Json.JsonSerializer(); + + // using (Newtonsoft.Json.JsonTextReader jsonReader = new Newtonsoft.Json.JsonTextReader(sr)) + // { + // if (Text.Equals("Properties All, Serializer All")) + // deserializingTester.TypeNameHandling = Newtonsoft.Json.TypeNameHandling.All; + // else if (Text.Equals("Properties All, Serializer Objects")) + // deserializingTester.TypeNameHandling = Newtonsoft.Json.TypeNameHandling.Objects; + // else if (Text.Equals("Properties All, Serializer Arrays")) + // deserializingTester.TypeNameHandling = Newtonsoft.Json.TypeNameHandling.Arrays; + // //otherwise leave default, which looks like it's "None" + + // anotherTestObject = deserializingTester.Deserialize(jsonReader); + // } + //} } } \ No newline at end of file diff --git a/Src/Newtonsoft.Json.Tests/TestObjects/ContentBaseClass.cs b/Src/Newtonsoft.Json.Tests/TestObjects/ContentBaseClass.cs new file mode 100644 index 00000000..6e505eb6 --- /dev/null +++ b/Src/Newtonsoft.Json.Tests/TestObjects/ContentBaseClass.cs @@ -0,0 +1,11 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace Newtonsoft.Json.Tests.TestObjects +{ + public abstract class ContentBaseClass + { + } +} diff --git a/Src/Newtonsoft.Json.Tests/TestObjects/ContentSubClass.cs b/Src/Newtonsoft.Json.Tests/TestObjects/ContentSubClass.cs new file mode 100644 index 00000000..b88e2c87 --- /dev/null +++ b/Src/Newtonsoft.Json.Tests/TestObjects/ContentSubClass.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace Newtonsoft.Json.Tests.TestObjects +{ + public class ContentSubClass : ContentBaseClass + { + public ContentSubClass() { } + public ContentSubClass(string EasyIn) + { + SomeString = EasyIn; + } + + public string SomeString { get; set; } + } +} diff --git a/Src/Newtonsoft.Json.Tests/TestObjects/HolderClass.cs b/Src/Newtonsoft.Json.Tests/TestObjects/HolderClass.cs new file mode 100644 index 00000000..27b26f4b --- /dev/null +++ b/Src/Newtonsoft.Json.Tests/TestObjects/HolderClass.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace Newtonsoft.Json.Tests.TestObjects +{ + public class HolderClass + { + public HolderClass() { } + + [Newtonsoft.Json.JsonProperty(TypeNameHandling = Newtonsoft.Json.TypeNameHandling.All)] + public ContentBaseClass TestMember { get; set; } + + [Newtonsoft.Json.JsonProperty(TypeNameHandling = Newtonsoft.Json.TypeNameHandling.All)] + public Dictionary> AnotherTestMember { get; set; } + + public ContentBaseClass AThirdTestMember { get; set; } + + } +} diff --git a/Src/Newtonsoft.Json.Tests/TestObjects/SerializationEventTestObject.cs b/Src/Newtonsoft.Json.Tests/TestObjects/SerializationEventTestObject.cs index 0e8ef23e..9289b149 100644 --- a/Src/Newtonsoft.Json.Tests/TestObjects/SerializationEventTestObject.cs +++ b/Src/Newtonsoft.Json.Tests/TestObjects/SerializationEventTestObject.cs @@ -23,7 +23,7 @@ // OTHER DEALINGS IN THE SOFTWARE. #endregion -#if !PocketPC && !NET20 +#if !PocketPC using System; using System.Linq; using System.Runtime.Serialization; diff --git a/Src/Newtonsoft.Json/Converters/HtmlColorConverter.cs b/Src/Newtonsoft.Json/Converters/HtmlColorConverter.cs deleted file mode 100644 index 137c24ab..00000000 --- a/Src/Newtonsoft.Json/Converters/HtmlColorConverter.cs +++ /dev/null @@ -1,85 +0,0 @@ -#region License -// Copyright (c) 2007 James Newton-King -// -// Permission is hereby granted, free of charge, to any person -// obtaining a copy of this software and associated documentation -// files (the "Software"), to deal in the Software without -// restriction, including without limitation the rights to use, -// copy, modify, merge, publish, distribute, sublicense, and/or sell -// copies of the Software, and to permit persons to whom the -// Software is furnished to do so, subject to the following -// conditions: -// -// The above copyright notice and this permission notice shall be -// included in all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES -// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT -// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, -// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR -// OTHER DEALINGS IN THE SOFTWARE. -#endregion - -#if !SILVERLIGHT && !PocketPC -using System; -using System.Drawing; - -namespace Newtonsoft.Json.Converters -{ - /// - /// Converts a object to and from JSON. - /// - public class HtmlColorConverter : JsonConverter - { - /// - /// Writes the JSON representation of the object. - /// - /// The to write to. - /// The calling serializer. - /// The value. - public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) - { - writer.WriteValue(ColorTranslator.ToHtml((Color)value)); - } - - /// - /// Determines whether this instance can convert the specified value type. - /// - /// Type of the value. - /// - /// true if this instance can convert the specified value type; otherwise, false. - /// - public override bool CanConvert(Type valueType) - { - return (valueType == typeof(Color)); - } - - /// - /// Reads the JSON representation of the object. - /// - /// The to read from. - /// Type of the object. - /// The existing value of object being read. - /// The calling serializer. - /// The object value. - public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) - { - throw new NotImplementedException(); - } - - /// - /// Gets a value indicating whether this can read JSON. - /// - /// - /// true if this can read JSON; otherwise, false. - /// - public override bool CanRead - { - get { return false; } - } - } -} -#endif \ No newline at end of file diff --git a/Src/Newtonsoft.Json/Converters/RegexConverter.cs b/Src/Newtonsoft.Json/Converters/RegexConverter.cs index cd6ce993..72c5c8a9 100644 --- a/Src/Newtonsoft.Json/Converters/RegexConverter.cs +++ b/Src/Newtonsoft.Json/Converters/RegexConverter.cs @@ -4,6 +4,7 @@ using System.Linq; using System.Text; using System.Text.RegularExpressions; using Newtonsoft.Json.Bson; +using System.Globalization; namespace Newtonsoft.Json.Converters { @@ -129,7 +130,7 @@ namespace Newtonsoft.Json.Converters reader.Read(); reader.Read(); - int options = Convert.ToInt32(reader.Value); + int options = Convert.ToInt32(reader.Value, CultureInfo.InvariantCulture); reader.Read(); diff --git a/Src/Newtonsoft.Json/Newtonsoft.Json.Compact.csproj b/Src/Newtonsoft.Json/Newtonsoft.Json.Compact.csproj index 68b28712..86a285f9 100644 --- a/Src/Newtonsoft.Json/Newtonsoft.Json.Compact.csproj +++ b/Src/Newtonsoft.Json/Newtonsoft.Json.Compact.csproj @@ -100,7 +100,6 @@ - diff --git a/Src/Newtonsoft.Json/Newtonsoft.Json.Net20.csproj b/Src/Newtonsoft.Json/Newtonsoft.Json.Net20.csproj index cc2dfb51..465f7803 100644 --- a/Src/Newtonsoft.Json/Newtonsoft.Json.Net20.csproj +++ b/Src/Newtonsoft.Json/Newtonsoft.Json.Net20.csproj @@ -72,7 +72,6 @@ - @@ -119,7 +118,6 @@ - diff --git a/Src/Newtonsoft.Json/Newtonsoft.Json.Silverlight.csproj b/Src/Newtonsoft.Json/Newtonsoft.Json.Silverlight.csproj index b13d894b..d8624642 100644 --- a/Src/Newtonsoft.Json/Newtonsoft.Json.Silverlight.csproj +++ b/Src/Newtonsoft.Json/Newtonsoft.Json.Silverlight.csproj @@ -76,7 +76,6 @@ - diff --git a/Src/Newtonsoft.Json/Newtonsoft.Json.csproj b/Src/Newtonsoft.Json/Newtonsoft.Json.csproj index 49cb6bd8..9a31b5e7 100644 --- a/Src/Newtonsoft.Json/Newtonsoft.Json.csproj +++ b/Src/Newtonsoft.Json/Newtonsoft.Json.csproj @@ -69,7 +69,6 @@ 3.5 - 3.0 @@ -128,7 +127,6 @@ - diff --git a/Src/Newtonsoft.Json/Serialization/DefaultContractResolver.cs b/Src/Newtonsoft.Json/Serialization/DefaultContractResolver.cs index d44940a6..d596101e 100644 --- a/Src/Newtonsoft.Json/Serialization/DefaultContractResolver.cs +++ b/Src/Newtonsoft.Json/Serialization/DefaultContractResolver.cs @@ -330,7 +330,7 @@ namespace Newtonsoft.Json.Serialization Type prevAttributeType = null; ParameterInfo[] parameters = method.GetParameters(); -#if !PocketPC && !NET20 +#if !PocketPC if (IsValidCallback(method, parameters, typeof(OnSerializingAttribute), contract.OnSerializing, ref prevAttributeType)) { contract.OnSerializing = method; diff --git a/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalWriter.cs b/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalWriter.cs index b15b7957..ddb67db3 100644 --- a/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalWriter.cs +++ b/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalWriter.cs @@ -481,7 +481,10 @@ namespace Newtonsoft.Json.Serialization int initialDepth = writer.Top; - foreach (DictionaryEntry entry in values) + // Mono Unity 3.0 fix + IDictionary d = values; + + foreach (DictionaryEntry entry in d) { string propertyName = GetPropertyName(entry);