-Removed HtmlColorConverter to remove System.Drawing.dll dependency

-Fixed culture invariance
-Fixed serialization attributes to work in .NET 2.0
This commit is contained in:
JamesNK
2010-07-01 10:27:18 +00:00
parent 4a1c8d3a80
commit c9ec228942
20 changed files with 155 additions and 164 deletions
@@ -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<Color>(json, new HtmlColorConverter());
Assert.AreEqual(c.A, 255);
Assert.AreEqual(c.R, 1);
Assert.AreEqual(c.G, 2);
Assert.AreEqual(c.B, 3);
}
}
}
#endif
@@ -65,7 +65,6 @@
<Compile Include="Converters\CustomCreationConverterTests.cs" />
<Compile Include="Converters\DataSetConverterTests.cs" />
<Compile Include="Converters\DataTableConverterTests.cs" />
<Compile Include="Converters\HtmlColorConverterTests.cs" />
<Compile Include="Converters\ObjectIdConverterTests.cs" />
<Compile Include="Converters\RegexConverterTests.cs" />
<Compile Include="Converters\StringEnumConverterTests.cs" />
@@ -104,6 +103,8 @@
<Compile Include="TestObjects\ConstructorReadonlyFields.cs" />
<Compile Include="TestObjects\Container.cs" />
<Compile Include="TestObjects\Content.cs" />
<Compile Include="TestObjects\ContentBaseClass.cs" />
<Compile Include="TestObjects\ContentSubClass.cs" />
<Compile Include="TestObjects\ConverterPrecedenceClass.cs" />
<Compile Include="TestObjects\ConverterPrecedenceClassConverter.cs" />
<Compile Include="TestObjects\CircularReferenceWithIdClass.cs" />
@@ -118,6 +119,7 @@
<Compile Include="TestObjects\GenericListAndDictionaryInterfaceProperties.cs" />
<Compile Include="TestObjects\GetOnlyPropertyClass.cs" />
<Compile Include="TestObjects\GoogleMapGeocoderStructure.cs" />
<Compile Include="TestObjects\HolderClass.cs" />
<Compile Include="TestObjects\IncompatibleJsonAttributeClass.cs" />
<Compile Include="TestObjects\InterfacePropertyTestClass.cs" />
<Compile Include="TestObjects\Invoice.cs" />
@@ -69,7 +69,6 @@
<Compile Include="Converters\CustomCreationConverterTests.cs" />
<Compile Include="Converters\DataSetConverterTests.cs" />
<Compile Include="Converters\DataTableConverterTests.cs" />
<Compile Include="Converters\HtmlColorConverterTests.cs" />
<Compile Include="Converters\ObjectIdConverterTests.cs" />
<Compile Include="Converters\RegexConverterTests.cs" />
<Compile Include="Converters\StringEnumConverterTests.cs" />
@@ -89,11 +88,14 @@
<Compile Include="TestObjects\ConstructorReadonlyFields.cs" />
<Compile Include="TestObjects\Container.cs" />
<Compile Include="TestObjects\Content.cs" />
<Compile Include="TestObjects\ContentBaseClass.cs" />
<Compile Include="TestObjects\ContentSubClass.cs" />
<Compile Include="TestObjects\DictionaryInterfaceClass.cs" />
<Compile Include="Serialization\EntitiesSerializationTests.cs" />
<Compile Include="TestObjects\Event.cs" />
<Compile Include="TestObjects\Foo.cs" />
<Compile Include="TestObjects\GoogleMapGeocoderStructure.cs" />
<Compile Include="TestObjects\HolderClass.cs" />
<Compile Include="TestObjects\InterfacePropertyTestClass.cs" />
<Compile Include="TestObjects\JsonPropertyClass.cs" />
<Compile Include="Serialization\JsonSerializerTest.cs" />
@@ -74,7 +74,6 @@
<Compile Include="Converters\CustomCreationConverterTests.cs" />
<Compile Include="Converters\DataSetConverterTests.cs" />
<Compile Include="Converters\DataTableConverterTests.cs" />
<Compile Include="Converters\HtmlColorConverterTests.cs" />
<Compile Include="Converters\ObjectIdConverterTests.cs" />
<Compile Include="Converters\RegexConverterTests.cs" />
<Compile Include="Converters\StringEnumConverterTests.cs" />
@@ -115,6 +114,8 @@
<Compile Include="TestObjects\ConstructorReadonlyFields.cs" />
<Compile Include="TestObjects\Container.cs" />
<Compile Include="TestObjects\Content.cs" />
<Compile Include="TestObjects\ContentBaseClass.cs" />
<Compile Include="TestObjects\ContentSubClass.cs" />
<Compile Include="TestObjects\ConverterPrecedenceClass.cs" />
<Compile Include="TestObjects\ConverterPrecedenceClassConverter.cs" />
<Compile Include="TestObjects\CircularReferenceWithIdClass.cs" />
@@ -129,6 +130,7 @@
<Compile Include="TestObjects\GenericListAndDictionaryInterfaceProperties.cs" />
<Compile Include="TestObjects\GetOnlyPropertyClass.cs" />
<Compile Include="TestObjects\GoogleMapGeocoderStructure.cs" />
<Compile Include="TestObjects\HolderClass.cs" />
<Compile Include="TestObjects\IncompatibleJsonAttributeClass.cs" />
<Compile Include="TestObjects\InterfacePropertyTestClass.cs" />
<Compile Include="TestObjects\Invoice.cs" />
@@ -91,7 +91,6 @@
<Compile Include="Converters\DataTableConverterTests.cs" />
<Compile Include="Converters\DataSetConverterTests.cs" />
<Compile Include="Converters\CustomCreationConverterTests.cs" />
<Compile Include="Converters\HtmlColorConverterTests.cs" />
<Compile Include="Converters\ObjectIdConverterTests.cs" />
<Compile Include="Converters\StringEnumConverterTests.cs" />
<Compile Include="FileSystemEntityModel.Designer.cs">
@@ -128,7 +127,10 @@
<Compile Include="TestObjects\Computer.cs" />
<Compile Include="TestObjects\ConstructorReadonlyFields.cs" />
<Compile Include="TestObjects\Container.cs" />
<Compile Include="TestObjects\ContentBaseClass.cs" />
<Compile Include="TestObjects\ContentSubClass.cs" />
<Compile Include="TestObjects\Foo.cs" />
<Compile Include="TestObjects\HolderClass.cs" />
<Compile Include="TestObjects\ListOfIds.cs" />
<Compile Include="TestObjects\Movie.cs" />
<Compile Include="TestObjects\PersonError.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<StringDictionaryTestClass>(json);
}
#endif
}
}
@@ -23,7 +23,7 @@
// OTHER DEALINGS IN THE SOFTWARE.
#endregion
#if !PocketPC && !NET20
#if !PocketPC
using System;
using System.IO;
using System.Linq;
@@ -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<int, List<ContentBaseClass>>();
// testObject.AnotherTestMember.Add(1, new List<ContentBaseClass>());
// 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<HolderClass>(jsonReader);
// }
//}
}
}
@@ -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
{
}
}
@@ -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; }
}
}
@@ -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<int, List<ContentBaseClass>> AnotherTestMember { get; set; }
public ContentBaseClass AThirdTestMember { get; set; }
}
}
@@ -23,7 +23,7 @@
// OTHER DEALINGS IN THE SOFTWARE.
#endregion
#if !PocketPC && !NET20
#if !PocketPC
using System;
using System.Linq;
using System.Runtime.Serialization;