-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;
@@ -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
{
/// <summary>
/// Converts a <see cref="Color"/> object to and from JSON.
/// </summary>
public class HtmlColorConverter : JsonConverter
{
/// <summary>
/// Writes the JSON representation of the object.
/// </summary>
/// <param name="writer">The <see cref="JsonWriter"/> to write to.</param>
/// <param name="serializer">The calling serializer.</param>
/// <param name="value">The value.</param>
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
writer.WriteValue(ColorTranslator.ToHtml((Color)value));
}
/// <summary>
/// Determines whether this instance can convert the specified value type.
/// </summary>
/// <param name="valueType">Type of the value.</param>
/// <returns>
/// <c>true</c> if this instance can convert the specified value type; otherwise, <c>false</c>.
/// </returns>
public override bool CanConvert(Type valueType)
{
return (valueType == typeof(Color));
}
/// <summary>
/// Reads the JSON representation of the object.
/// </summary>
/// <param name="reader">The <see cref="JsonReader"/> to read from.</param>
/// <param name="objectType">Type of the object.</param>
/// <param name="existingValue">The existing value of object being read.</param>
/// <param name="serializer">The calling serializer.</param>
/// <returns>The object value.</returns>
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
throw new NotImplementedException();
}
/// <summary>
/// Gets a value indicating whether this <see cref="JsonConverter"/> can read JSON.
/// </summary>
/// <value>
/// <c>true</c> if this <see cref="JsonConverter"/> can read JSON; otherwise, <c>false</c>.
/// </value>
public override bool CanRead
{
get { return false; }
}
}
}
#endif
@@ -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();
@@ -100,7 +100,6 @@
<Compile Include="Converters\JavaScriptDateTimeConverter.cs" />
<Compile Include="Converters\JsonDateTimeSerializationMode.cs" />
<Compile Include="Converters\XmlNodeConverter.cs" />
<Compile Include="Converters\HtmlColorConverter.cs" />
<Compile Include="JsonTextReader.cs" />
<Compile Include="JsonPropertyAttribute.cs" />
<Compile Include="JsonIgnoreAttribute.cs" />
@@ -72,7 +72,6 @@
</Reference>
<Reference Include="System" />
<Reference Include="System.Data" />
<Reference Include="System.Drawing" />
<Reference Include="System.Web" />
<Reference Include="System.Xml" />
</ItemGroup>
@@ -119,7 +118,6 @@
<Compile Include="Converters\JavaScriptDateTimeConverter.cs" />
<Compile Include="Converters\JsonDateTimeSerializationMode.cs" />
<Compile Include="Converters\XmlNodeConverter.cs" />
<Compile Include="Converters\HtmlColorConverter.cs" />
<Compile Include="JsonTextReader.cs" />
<Compile Include="JsonPropertyAttribute.cs" />
<Compile Include="JsonIgnoreAttribute.cs" />
@@ -76,7 +76,6 @@
<Compile Include="Converters\RegexConverter.cs" />
<Compile Include="Converters\StringEnumConverter.cs" />
<Compile Include="Converters\XmlNodeConverter.cs" />
<Compile Include="Converters\HtmlColorConverter.cs" />
<Compile Include="FormatterAssemblyStyle.cs" />
<Compile Include="DefaultValueHandling.cs" />
<Compile Include="IJsonLineInfo.cs" />
@@ -69,7 +69,6 @@
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
<Reference Include="System.Data" />
<Reference Include="System.Drawing" />
<Reference Include="System.Runtime.Serialization">
<RequiredTargetFramework>3.0</RequiredTargetFramework>
</Reference>
@@ -128,7 +127,6 @@
<Compile Include="Converters\JavaScriptDateTimeConverter.cs" />
<Compile Include="Converters\JsonDateTimeSerializationMode.cs" />
<Compile Include="Converters\XmlNodeConverter.cs" />
<Compile Include="Converters\HtmlColorConverter.cs" />
<Compile Include="JsonTextReader.cs" />
<Compile Include="JsonPropertyAttribute.cs" />
<Compile Include="JsonIgnoreAttribute.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;
@@ -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);