-Added StringEnumConverter

-Added BinaryConverter
This commit is contained in:
JamesNK
2009-03-29 01:36:06 +00:00
parent b9bedd1233
commit 3630a43f11
13 changed files with 530 additions and 8 deletions
@@ -0,0 +1,136 @@
using System;
using System.Collections.Generic;
#if !SILVERLIGHT && !PocketPC
using System.Data.Linq;
#endif
#if !SILVERLIGHT
using System.Data.SqlTypes;
#endif
using System.Linq;
using System.Text;
using Newtonsoft.Json.Converters;
using NUnit.Framework;
using Newtonsoft.Json.Tests.TestObjects;
namespace Newtonsoft.Json.Tests.Converters
{
public class BinaryConverterTests : TestFixtureBase
{
private static readonly byte[] TestData = Encoding.UTF8.GetBytes("This is some test data!!!");
public class ByteArrayClass
{
public byte[] ByteArray { get; set; }
public byte[] NullByteArray { get; set; }
}
#if !SILVERLIGHT && !PocketPC
[Test]
public void DeserializeBinaryClass()
{
string json = @"{
""Binary"": ""VGhpcyBpcyBzb21lIHRlc3QgZGF0YSEhIQ=="",
""NullBinary"": null
}";
BinaryClass binaryClass = JsonConvert.DeserializeObject<BinaryClass>(json, new BinaryConverter());
Assert.AreEqual(new Binary(TestData), binaryClass.Binary);
Assert.AreEqual(null, binaryClass.NullBinary);
}
public class BinaryClass
{
public Binary Binary { get; set; }
public Binary NullBinary { get; set; }
}
[Test]
public void SerializeBinaryClass()
{
BinaryClass binaryClass = new BinaryClass();
binaryClass.Binary = new Binary(TestData);
binaryClass.NullBinary = null;
string json = JsonConvert.SerializeObject(binaryClass, Formatting.Indented, new BinaryConverter());
Assert.AreEqual(@"{
""Binary"": ""VGhpcyBpcyBzb21lIHRlc3QgZGF0YSEhIQ=="",
""NullBinary"": null
}", json);
}
#endif
[Test]
public void SerializeByteArrayClass()
{
ByteArrayClass byteArrayClass = new ByteArrayClass();
byteArrayClass.ByteArray = TestData;
byteArrayClass.NullByteArray = null;
string json = JsonConvert.SerializeObject(byteArrayClass, Formatting.Indented, new BinaryConverter());
Assert.AreEqual(@"{
""ByteArray"": ""VGhpcyBpcyBzb21lIHRlc3QgZGF0YSEhIQ=="",
""NullByteArray"": null
}", json);
}
#if !SILVERLIGHT
public class SqlBinaryClass
{
public SqlBinary SqlBinary { get; set; }
public SqlBinary? NullableSqlBinary1 { get; set; }
public SqlBinary? NullableSqlBinary2 { get; set; }
}
[Test]
public void SerializeSqlBinaryClass()
{
SqlBinaryClass sqlBinaryClass = new SqlBinaryClass();
sqlBinaryClass.SqlBinary = new SqlBinary(TestData);
sqlBinaryClass.NullableSqlBinary1 = new SqlBinary(TestData);
sqlBinaryClass.NullableSqlBinary2 = null;
string json = JsonConvert.SerializeObject(sqlBinaryClass, Formatting.Indented, new BinaryConverter());
Assert.AreEqual(@"{
""SqlBinary"": ""VGhpcyBpcyBzb21lIHRlc3QgZGF0YSEhIQ=="",
""NullableSqlBinary1"": ""VGhpcyBpcyBzb21lIHRlc3QgZGF0YSEhIQ=="",
""NullableSqlBinary2"": null
}", json);
}
[Test]
public void DeserializeSqlBinaryClass()
{
string json = @"{
""SqlBinary"": ""VGhpcyBpcyBzb21lIHRlc3QgZGF0YSEhIQ=="",
""NullableSqlBinary1"": ""VGhpcyBpcyBzb21lIHRlc3QgZGF0YSEhIQ=="",
""NullableSqlBinary2"": null
}";
SqlBinaryClass sqlBinaryClass = JsonConvert.DeserializeObject<SqlBinaryClass>(json, new BinaryConverter());
Assert.AreEqual(new SqlBinary(TestData), sqlBinaryClass.SqlBinary);
Assert.AreEqual(new SqlBinary(TestData), sqlBinaryClass.NullableSqlBinary1);
Assert.AreEqual(null, sqlBinaryClass.NullableSqlBinary2);
}
#endif
[Test]
public void DeserializeByteArrayClass()
{
string json = @"{
""ByteArray"": ""VGhpcyBpcyBzb21lIHRlc3QgZGF0YSEhIQ=="",
""NullByteArray"": null
}";
ByteArrayClass byteArrayClass = JsonConvert.DeserializeObject<ByteArrayClass>(json, new BinaryConverter());
Assert.AreEqual(TestData, byteArrayClass.ByteArray);
Assert.AreEqual(null, byteArrayClass.NullByteArray);
}
}
}
@@ -0,0 +1,161 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json.Converters;
using NUnit.Framework;
using Newtonsoft.Json.Tests.TestObjects;
namespace Newtonsoft.Json.Tests.Converters
{
public class StringEnumConverterTests : TestFixtureBase
{
public class EnumClass
{
public StoreColor StoreColor { get; set; }
public StoreColor? NullableStoreColor1 { get; set; }
public StoreColor? NullableStoreColor2 { get; set; }
}
public enum NegativeEnum
{
Negative = -1,
Zero = 0,
Positive = 1
}
public class NegativeEnumClass
{
public NegativeEnum Value1 { get; set; }
public NegativeEnum Value2 { get; set; }
}
[Test]
public void SerializeEnumClass()
{
EnumClass enumClass = new EnumClass();
enumClass.StoreColor = StoreColor.Red;
enumClass.NullableStoreColor1 = StoreColor.White;
enumClass.NullableStoreColor2 = null;
string json = JsonConvert.SerializeObject(enumClass, Formatting.Indented, new StringEnumConverter());
Assert.AreEqual(@"{
""StoreColor"": ""Red"",
""NullableStoreColor1"": ""White"",
""NullableStoreColor2"": null
}", json);
}
[Test]
public void SerializeEnumClassUndefined()
{
EnumClass enumClass = new EnumClass();
enumClass.StoreColor = (StoreColor)1000;
enumClass.NullableStoreColor1 = (StoreColor)1000;
enumClass.NullableStoreColor2 = null;
string json = JsonConvert.SerializeObject(enumClass, Formatting.Indented, new StringEnumConverter());
Assert.AreEqual(@"{
""StoreColor"": 1000,
""NullableStoreColor1"": 1000,
""NullableStoreColor2"": null
}", json);
}
[Test]
public void SerializeFlagEnum()
{
EnumClass enumClass = new EnumClass();
enumClass.StoreColor = StoreColor.Red | StoreColor.White;
enumClass.NullableStoreColor1 = StoreColor.White & StoreColor.Yellow;
enumClass.NullableStoreColor2 = StoreColor.Red | StoreColor.White | StoreColor.Black;
string json = JsonConvert.SerializeObject(enumClass, Formatting.Indented, new StringEnumConverter());
Assert.AreEqual(@"{
""StoreColor"": ""Red, White"",
""NullableStoreColor1"": 0,
""NullableStoreColor2"": ""Black, Red, White""
}", json);
}
[Test]
public void SerializeNegativeEnum()
{
NegativeEnumClass negativeEnumClass = new NegativeEnumClass();
negativeEnumClass.Value1 = NegativeEnum.Negative;
negativeEnumClass.Value2 = (NegativeEnum) int.MinValue;
string json = JsonConvert.SerializeObject(negativeEnumClass, Formatting.Indented, new StringEnumConverter());
Assert.AreEqual(@"{
""Value1"": ""Negative"",
""Value2"": -2147483648
}", json);
}
[Test]
public void DeserializeNegativeEnum()
{
string json = @"{
""Value1"": ""Negative"",
""Value2"": -2147483648
}";
NegativeEnumClass negativeEnumClass = JsonConvert.DeserializeObject<NegativeEnumClass>(json, new StringEnumConverter());
Assert.AreEqual(NegativeEnum.Negative, negativeEnumClass.Value1);
Assert.AreEqual((NegativeEnum)int.MinValue, negativeEnumClass.Value2);
}
[Test]
public void DeserializeFlagEnum()
{
string json = @"{
""StoreColor"": ""Red, White"",
""NullableStoreColor1"": 0,
""NullableStoreColor2"": ""Black, Red, White""
}";
EnumClass enumClass = JsonConvert.DeserializeObject<EnumClass>(json, new StringEnumConverter());
Assert.AreEqual(StoreColor.Red | StoreColor.White, enumClass.StoreColor);
Assert.AreEqual((StoreColor)0, enumClass.NullableStoreColor1);
Assert.AreEqual(StoreColor.Red | StoreColor.White | StoreColor.Black, enumClass.NullableStoreColor2);
}
[Test]
public void DeserializeEnumClass()
{
string json = @"{
""StoreColor"": ""Red"",
""NullableStoreColor1"": ""White"",
""NullableStoreColor2"": null
}";
EnumClass enumClass = JsonConvert.DeserializeObject<EnumClass>(json, new StringEnumConverter());
Assert.AreEqual(StoreColor.Red, enumClass.StoreColor);
Assert.AreEqual(StoreColor.White, enumClass.NullableStoreColor1);
Assert.AreEqual(null, enumClass.NullableStoreColor2);
}
[Test]
public void DeserializeEnumClassUndefined()
{
string json = @"{
""StoreColor"": 1000,
""NullableStoreColor1"": 1000,
""NullableStoreColor2"": null
}";
EnumClass enumClass = JsonConvert.DeserializeObject<EnumClass>(json, new StringEnumConverter());
Assert.AreEqual((StoreColor)1000, enumClass.StoreColor);
Assert.AreEqual((StoreColor)1000, enumClass.NullableStoreColor1);
Assert.AreEqual(null, enumClass.NullableStoreColor2);
}
}
}
@@ -604,7 +604,7 @@ keyword such as type of business.""
//JsonConvert.ConvertDateTimeToJavaScriptTicks(s1.Establised.DateTime)
Assert.AreEqual(@"{""Color"":2,""Establised"":""\/Date(1264122061000+0000)\/"",""Width"":1.1,""Employees"":999,""RoomsPerFloor"":[1,2,3,4,5,6,7,8,9],""Open"":false,""Symbol"":""@"",""Mottos"":[""Hello World"",""öäüÖÄÜ\\'{new Date(12345);}[222]_µ@²³~"",null,"" ""],""Cost"":100980.1,""Escape"":""\r\n\t\f\b?{\\r\\n\""'"",""product"":[{""Name"":""Rocket"",""ExpiryDate"":""\/Date(949532490000)\/"",""Price"":0},{""Name"":""Alien"",""ExpiryDate"":""\/Date(946684800000)\/"",""Price"":0}]}", sw.GetStringBuilder().ToString());
Assert.AreEqual(@"{""Color"":4,""Establised"":""\/Date(1264122061000+0000)\/"",""Width"":1.1,""Employees"":999,""RoomsPerFloor"":[1,2,3,4,5,6,7,8,9],""Open"":false,""Symbol"":""@"",""Mottos"":[""Hello World"",""öäüÖÄÜ\\'{new Date(12345);}[222]_µ@²³~"",null,"" ""],""Cost"":100980.1,""Escape"":""\r\n\t\f\b?{\\r\\n\""'"",""product"":[{""Name"":""Rocket"",""ExpiryDate"":""\/Date(949532490000)\/"",""Price"":0},{""Name"":""Alien"",""ExpiryDate"":""\/Date(946684800000)\/"",""Price"":0}]}", sw.GetStringBuilder().ToString());
Store s2 = (Store)jsonSerializer.Deserialize(new JsonTextReader(new StringReader("{}")), typeof(Store));
Assert.AreEqual("\r\n\t\f\b?{\\r\\n\"\'", s2.Escape);
@@ -58,6 +58,8 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Converters\BinaryConverterTests.cs" />
<Compile Include="Converters\StringEnumConverterTests.cs" />
<Compile Include="Serialization\CamelCaseMappingResolverTests.cs" />
<Compile Include="JsonValidatingReaderTests.cs" />
<Compile Include="Schema\ExtensionsTests.cs" />
@@ -63,6 +63,8 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="Converters\BinaryConverterTests.cs" />
<Compile Include="Converters\StringEnumConverterTests.cs" />
<Compile Include="Serialization\CamelCaseMappingResolverTests.cs" />
<Compile Include="JsonValidatingReaderTests.cs" />
<Compile Include="Schema\ExtensionsTests.cs" />
@@ -57,6 +57,9 @@
<Reference Include="System.Data.Entity">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
<Reference Include="System.Data.Linq">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
<Reference Include="System.Runtime.Serialization">
<RequiredTargetFramework>3.0</RequiredTargetFramework>
</Reference>
@@ -77,6 +80,8 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="Converters\BinaryConverterTests.cs" />
<Compile Include="Converters\StringEnumConverterTests.cs" />
<Compile Include="JsonValidatingReaderTests.cs" />
<Compile Include="Schema\ExtensionsTests.cs" />
<Compile Include="Schema\JsonSchemaModelBuilderTests.cs" />
@@ -23,13 +23,16 @@
// OTHER DEALINGS IN THE SOFTWARE.
#endregion
using System;
namespace Newtonsoft.Json.Tests.TestObjects
{
[Flags]
public enum StoreColor
{
Black,
Red,
Yellow,
White
Black = 1,
Red = 2,
Yellow = 4,
White = 8
}
}