public void SerializeEnum() { // Verify that Enum values serialize correctly. var converter = new JsonDataConverter(); byte[] contents; string json; // Verify that an enum type with custom [EnumMemver] values // serializes as expected. var enumValue = new EnumEntity() { Gender = Gender.Male }; contents = converter.ToData(enumValue); json = Encoding.UTF8.GetString(contents); Assert.Equal("{\"Gender\":\"male\"}", json); // Verify that an enum type without custom [EnumMemver] values // serializes as expected. var enumNotCustomValue = new EnumNotCustomEntity() { Gender = GenderNotCustom.Male }; contents = converter.ToData(enumNotCustomValue); json = Encoding.UTF8.GetString(contents); Assert.Equal("{\"Gender\":\"Male\"}", json); }
public void DataWithGenericType() { // Verify that we can serialize and deserialize various (non-roundtrip) data items // using the generic FromData() method. var converter = new JsonDataConverter(); byte[] contents; // Serialize NULL. contents = converter.ToData(null); Assert.Null(converter.FromData <object>(contents)); // Serialize a string. contents = converter.ToData("foo"); Assert.Equal("foo", converter.FromData <string>(contents)); // Serialize a byte array. var items = new int[] { 0, 1, 2, 3, 4 }; contents = converter.ToData(items); Assert.Equal(items, converter.FromData <int[]>(contents)); // Serialize an array of non-roundtrip objects. var items2 = new TestData[] { new TestData() { Hello = "World!" }, new TestData() { Hello = "Goodbye!" }, null }; contents = converter.ToData(items2); items2 = converter.FromData <TestData[]>(contents); Assert.Equal(3, items2.Length); Assert.Equal("World!", items2[0].Hello); Assert.Equal("Goodbye!", items2[1].Hello); Assert.Null(items2[2]); }
public void RoundTripDataWithGeneric() { // Verify that we can serialize and deserialize various roundtrip data items // using the generic converter. var converter = new JsonDataConverter(); byte[] contents; // Serialize NULL. contents = converter.ToData(null); Assert.Null(converter.FromData <Person>(contents)); // Serialize a roundtrip item without extra data. var bob = new Person() { Name = "Bob", Age = 27, Data = new byte[] { 0, 1, 2, 3, 4 }, Gender = Gender.Male }; contents = converter.ToData(bob); Assert.Equal(bob, converter.FromData <Person>(contents)); // Serialize a roundtrip item WITH extra data. bob.__O.Add("foo", "bar"); contents = converter.ToData(bob); var deserializedBob = converter.FromData <Person>(contents); Assert.Equal(bob, deserializedBob); Assert.Equal("bar", (string)deserializedBob.__O["foo"]); }
public void SerializeDateTime() { // Verify that Enum values with the [Flags] attribute serialize correctly. var converter = new JsonDataConverter(); byte[] contents; string json; var enumValue = new EnumEntity() { Gender = Gender.Male }; contents = converter.ToData(enumValue); json = Encoding.UTF8.GetString(contents); Assert.Equal("{\"Gender\":\"male\"}", json); }
public void SerializeFlagsEnum() { var converter = new JsonDataConverter(); byte[] contents; string json; EnumFlagsEntity value; // Verify uncombined flags. value = new EnumFlagsEntity() { Gender = GenderFlags.Unspecified }; contents = converter.ToData(value); json = Encoding.UTF8.GetString(contents); Assert.Equal("{\"Gender\":\"unspecified\"}", json); //---------- value = new EnumFlagsEntity() { Gender = GenderFlags.Male }; contents = converter.ToData(value); json = Encoding.UTF8.GetString(contents); Assert.Equal("{\"Gender\":\"male\"}", json); //---------- value = new EnumFlagsEntity() { Gender = GenderFlags.Female }; contents = converter.ToData(value); json = Encoding.UTF8.GetString(contents); Assert.Equal("{\"Gender\":\"female\"}", json); //---------- value = new EnumFlagsEntity() { Gender = GenderFlags.Other }; contents = converter.ToData(value); json = Encoding.UTF8.GetString(contents); Assert.Equal("{\"Gender\":\"other\"}", json); // Verifiy flag combinations value = new EnumFlagsEntity() { Gender = GenderFlags.Male | GenderFlags.Female }; contents = converter.ToData(value); json = Encoding.UTF8.GetString(contents); Assert.Equal("{\"Gender\":\"male, female\"}", json); }