From ded0ebef3bb1649ad3713f83fdf067065bc1f794 Mon Sep 17 00:00:00 2001 From: Erik Ovegard Date: Fri, 28 Oct 2016 14:47:21 +0300 Subject: [PATCH 1/5] Modified serilization to support units saved as IComparable --- .../UnitsNetJsonConverterTests.cs | 83 +++++++++++++++++++ .../UnitsNetJsonConverter.cs | 56 +++++++++++-- 2 files changed, 134 insertions(+), 5 deletions(-) diff --git a/UnitsNet.Serialization.JsonNet.Tests/UnitsNetJsonConverterTests.cs b/UnitsNet.Serialization.JsonNet.Tests/UnitsNetJsonConverterTests.cs index d19c461a75..f8394a47bb 100644 --- a/UnitsNet.Serialization.JsonNet.Tests/UnitsNetJsonConverterTests.cs +++ b/UnitsNet.Serialization.JsonNet.Tests/UnitsNetJsonConverterTests.cs @@ -226,6 +226,75 @@ public void UnitEnumChangedAfterSerialization_ExpectUnitCorrectlyDeserialized() // still deserializable, and the correct value of 1000 g is obtained. Assert.That(deserializedMass.Grams, Is.EqualTo(1000)); } + + [Test] + public void UnitInIComparable_ExpectUnitCorrectlyDeserialized() + { + TestObjWithIComparable testObjWithIComparable = new TestObjWithIComparable() + { + Value = Power.FromWatts(10) + }; + var jsonSerializerSettings = new JsonSerializerSettings + { + Formatting = Formatting.Indented, + TypeNameHandling = TypeNameHandling.Objects + }; + jsonSerializerSettings.Converters.Add(new UnitsNetJsonConverter()); + + string json = JsonConvert.SerializeObject(testObjWithIComparable,jsonSerializerSettings); + + jsonSerializerSettings.TypeNameHandling = TypeNameHandling.Auto; + var deserializedTestObject = JsonConvert.DeserializeObject(json,jsonSerializerSettings); + + Assert.That(deserializedTestObject.Value.GetType(), Is.EqualTo(typeof(Power))); + Assert.That((Power)deserializedTestObject.Value, Is.EqualTo(Power.FromWatts(10))); + } + + [Test] + public void DoubleInIComparable_ExpectUnitCorrectlyDeserialized() + { + TestObjWithIComparable testObjWithIComparable = new TestObjWithIComparable() + { + Value = 10.0 + }; + var jsonSerializerSettings = new JsonSerializerSettings + { + Formatting = Formatting.Indented, + TypeNameHandling = TypeNameHandling.Objects + }; + jsonSerializerSettings.Converters.Add(new UnitsNetJsonConverter()); + + string json = JsonConvert.SerializeObject(testObjWithIComparable, jsonSerializerSettings); + + jsonSerializerSettings.TypeNameHandling = TypeNameHandling.Auto; + var deserializedTestObject = JsonConvert.DeserializeObject(json, jsonSerializerSettings); + + Assert.That(deserializedTestObject.Value.GetType(), Is.EqualTo(typeof(double))); + Assert.That((double)deserializedTestObject.Value, Is.EqualTo(10.0)); + } + + [Test] + public void ClassInIComparable_ExpectUnitCorrectlyDeserialized() + { + TestObjWithIComparable testObjWithIComparable = new TestObjWithIComparable() + { + Value = new ComparableClass() { Value = 10 } + }; + var jsonSerializerSettings = new JsonSerializerSettings + { + Formatting = Formatting.Indented, + TypeNameHandling = TypeNameHandling.Objects + }; + jsonSerializerSettings.Converters.Add(new UnitsNetJsonConverter()); + + string json = JsonConvert.SerializeObject(testObjWithIComparable, jsonSerializerSettings); + + jsonSerializerSettings.TypeNameHandling = TypeNameHandling.Auto; + var deserializedTestObject = JsonConvert.DeserializeObject(json, jsonSerializerSettings); + + Assert.That(deserializedTestObject.Value.GetType(), Is.EqualTo(typeof(ComparableClass))); + Assert.That(((ComparableClass)(deserializedTestObject.Value)).Value, Is.EqualTo(10.0)); + } } internal class TestObj @@ -233,5 +302,19 @@ internal class TestObj public Frequency? NullableFrequency { get; set; } public Frequency NonNullableFrequency { get; set; } } + + internal class ComparableClass : IComparable + { + public int Value { get; set; } + public int CompareTo(object obj) + { + return Value.CompareTo(obj); + } + } + + internal class TestObjWithIComparable + { + public IComparable Value { get; set; } + } } } \ No newline at end of file diff --git a/UnitsNet.Serialization.JsonNet/UnitsNetJsonConverter.cs b/UnitsNet.Serialization.JsonNet/UnitsNetJsonConverter.cs index 6e3eef62dd..5840e14729 100644 --- a/UnitsNet.Serialization.JsonNet/UnitsNetJsonConverter.cs +++ b/UnitsNet.Serialization.JsonNet/UnitsNetJsonConverter.cs @@ -25,6 +25,7 @@ using System.Reflection; using JetBrains.Annotations; using Newtonsoft.Json; +using Newtonsoft.Json.Linq; namespace UnitsNet.Serialization.JsonNet { @@ -55,10 +56,17 @@ public class UnitsNetJsonConverter : JsonConverter public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { - var vu = serializer.Deserialize(reader); - // A null System.Nullable value was deserialized so just return null. + if (reader.ValueType != null) + { + return reader.Value; + } + object obj = TryDeserializeIComparable(reader, serializer); + var vu = obj as ValueUnit; + // A null System.Nullable value or a comparable type was deserialized so return this if (vu == null) - return null; + { + return obj; + } // "MassUnit.Kilogram" => "MassUnit" and "Kilogram" string unitEnumTypeName = vu.Unit.Split('.')[0]; @@ -102,10 +110,31 @@ where m.Name.Equals("From", StringComparison.InvariantCulture) && // TODO: there is a possible loss of precision if base value requires higher precision than double can represent. // Example: Serializing Information.FromExabytes(100) then deserializing to Information // will likely return a very different result. Not sure how we can handle this? - return fromMethod.Invoke(null, BindingFlags.Static, null, new[] {vu.Value, unit}, + return fromMethod.Invoke(null, BindingFlags.Static, null, new[] { vu.Value, unit }, CultureInfo.InvariantCulture); } + private static object TryDeserializeIComparable(JsonReader reader, JsonSerializer serializer) + { + JToken token = JToken.Load(reader); + if (!token.HasValues || token["Unit"] == null || token["Value"] == null) + { + JsonSerializer localSerializer = new JsonSerializer() + { + TypeNameHandling = serializer.TypeNameHandling, + }; + return token.ToObject(localSerializer); + } + else + { + return new ValueUnit() + { + Unit = token["Unit"].ToString(), + Value = token["Value"].ToObject() + }; + } + } + /// /// Writes the JSON representation of the object. /// @@ -117,6 +146,18 @@ public override void WriteJson(JsonWriter writer, object value, JsonSerializer s { Type unitType = value.GetType(); + // ValueUnit should be written as usual (but read in a custom way) + if(unitType == typeof(ValueUnit)) + { + JsonSerializer localSerializer = new JsonSerializer() + { + TypeNameHandling = serializer.TypeNameHandling, + }; + JToken t = JToken.FromObject(value, localSerializer); + + t.WriteTo(writer); + return; + } FieldInfo[] fields = unitType.GetFields(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly); if (fields.Length == 0) @@ -184,7 +225,12 @@ public override bool CanConvert(Type objectType) return CanConvertNullable(objectType); } - return objectType.Namespace != null && objectType.Namespace.Equals("UnitsNet"); + return objectType.Namespace != null && + (objectType.Namespace.Equals("UnitsNet") || + objectType == typeof(ValueUnit) || + // All unit types implement IComparable + objectType==typeof(IComparable) || + objectType.FullName.StartsWith("System." + nameof(IComparable))); } /// From 36532286b98e97a45631784e09de9d3c00c10a75 Mon Sep 17 00:00:00 2001 From: Erik Ovegard Date: Mon, 31 Oct 2016 16:30:49 +0100 Subject: [PATCH 2/5] Made changes based on code review by @anjdreas --- .../UnitsNetJsonConverterTests.cs | 130 +++++++++++++++--- .../UnitsNetJsonConverter.cs | 17 ++- 2 files changed, 116 insertions(+), 31 deletions(-) diff --git a/UnitsNet.Serialization.JsonNet.Tests/UnitsNetJsonConverterTests.cs b/UnitsNet.Serialization.JsonNet.Tests/UnitsNetJsonConverterTests.cs index f8394a47bb..738b76c371 100644 --- a/UnitsNet.Serialization.JsonNet.Tests/UnitsNetJsonConverterTests.cs +++ b/UnitsNet.Serialization.JsonNet.Tests/UnitsNetJsonConverterTests.cs @@ -22,6 +22,7 @@ using System; using Newtonsoft.Json; using NUnit.Framework; +using System.Collections.Generic; namespace UnitsNet.Serialization.JsonNet.Tests { @@ -234,16 +235,10 @@ public void UnitInIComparable_ExpectUnitCorrectlyDeserialized() { Value = Power.FromWatts(10) }; - var jsonSerializerSettings = new JsonSerializerSettings - { - Formatting = Formatting.Indented, - TypeNameHandling = TypeNameHandling.Objects - }; - jsonSerializerSettings.Converters.Add(new UnitsNetJsonConverter()); + JsonSerializerSettings jsonSerializerSettings = CreateJsonSerializerSettigns(); string json = JsonConvert.SerializeObject(testObjWithIComparable,jsonSerializerSettings); - jsonSerializerSettings.TypeNameHandling = TypeNameHandling.Auto; var deserializedTestObject = JsonConvert.DeserializeObject(json,jsonSerializerSettings); Assert.That(deserializedTestObject.Value.GetType(), Is.EqualTo(typeof(Power))); @@ -257,16 +252,10 @@ public void DoubleInIComparable_ExpectUnitCorrectlyDeserialized() { Value = 10.0 }; - var jsonSerializerSettings = new JsonSerializerSettings - { - Formatting = Formatting.Indented, - TypeNameHandling = TypeNameHandling.Objects - }; - jsonSerializerSettings.Converters.Add(new UnitsNetJsonConverter()); + JsonSerializerSettings jsonSerializerSettings = CreateJsonSerializerSettigns(); string json = JsonConvert.SerializeObject(testObjWithIComparable, jsonSerializerSettings); - jsonSerializerSettings.TypeNameHandling = TypeNameHandling.Auto; var deserializedTestObject = JsonConvert.DeserializeObject(json, jsonSerializerSettings); Assert.That(deserializedTestObject.Value.GetType(), Is.EqualTo(typeof(double))); @@ -280,21 +269,88 @@ public void ClassInIComparable_ExpectUnitCorrectlyDeserialized() { Value = new ComparableClass() { Value = 10 } }; - var jsonSerializerSettings = new JsonSerializerSettings - { - Formatting = Formatting.Indented, - TypeNameHandling = TypeNameHandling.Objects - }; - jsonSerializerSettings.Converters.Add(new UnitsNetJsonConverter()); + JsonSerializerSettings jsonSerializerSettings = CreateJsonSerializerSettigns(); string json = JsonConvert.SerializeObject(testObjWithIComparable, jsonSerializerSettings); - - jsonSerializerSettings.TypeNameHandling = TypeNameHandling.Auto; var deserializedTestObject = JsonConvert.DeserializeObject(json, jsonSerializerSettings); Assert.That(deserializedTestObject.Value.GetType(), Is.EqualTo(typeof(ComparableClass))); Assert.That(((ComparableClass)(deserializedTestObject.Value)).Value, Is.EqualTo(10.0)); } + + [Test] + public void OtherObjectWithUnitAndValue_ExpectCorrectResturnValues() + { + TestObjWithValueAndUnit testObjWithValueAndUnit = new TestObjWithValueAndUnit() + { + Value = 5, + Unit = "Test", + }; + JsonSerializerSettings jsonSerializerSettings = CreateJsonSerializerSettigns(); + + string json = JsonConvert.SerializeObject(testObjWithValueAndUnit, jsonSerializerSettings); + TestObjWithValueAndUnit deserializedTestObject = JsonConvert.DeserializeObject(json, jsonSerializerSettings); + + Assert.That(deserializedTestObject.Value.GetType(), Is.EqualTo(typeof(double))); + Assert.That(deserializedTestObject.Value, Is.EqualTo(5.0)); + Assert.That(deserializedTestObject.Unit, Is.EqualTo("Test")); + } + + [Test, TestCaseSource(nameof(TestObjectsForThreeObjectsInIComparableWithDifferentValues_ExpectAllCorrectlyDeserialized))] + public void ThreeObjectsInIComparableWithDifferentValues_ExpectAllCorrectlyDeserialized( + IComparable comparable1, + IComparable comparable2, + IComparable comparable3) + { + TestObjWithThreeIComparable testObjWithIComparable = new TestObjWithThreeIComparable() + { + Value1 = comparable1, + Value2 = comparable2, + Value3 = comparable3, + }; + JsonSerializerSettings jsonSerializerSettings = CreateJsonSerializerSettigns(); + + string json = JsonConvert.SerializeObject(testObjWithIComparable, jsonSerializerSettings); + var deserializedTestObject = JsonConvert.DeserializeObject(json, jsonSerializerSettings); + + Assert.That(deserializedTestObject.Value1.GetType(), Is.EqualTo(comparable1.GetType())); + Assert.That(((deserializedTestObject.Value1)), Is.EqualTo(comparable1)); + Assert.That(deserializedTestObject.Value2.GetType(), Is.EqualTo(comparable2.GetType())); + Assert.That(((deserializedTestObject.Value2)), Is.EqualTo(comparable2)); + Assert.That(deserializedTestObject.Value3.GetType(), Is.EqualTo(comparable3.GetType())); + Assert.That(((deserializedTestObject.Value3)), Is.EqualTo(comparable3)); + } + + public static object[] TestObjectsForThreeObjectsInIComparableWithDifferentValues_ExpectAllCorrectlyDeserialized + { + get + { + List result = new List(); + var objects = new object[] { 10.0, Power.FromWatts(19), new ComparableClass() { Value = 10 } }; + for (int i = 0; i < objects.Length; i++) + { + for (int j = 0; j < objects.Length; j++) + { + for (int k = 0; k < objects.Length; k++) + { + result.Add(new object[] { objects[i], objects[j], objects[k]}); + } + } + } + return result.ToArray(); + } + } + + private static JsonSerializerSettings CreateJsonSerializerSettigns() + { + var jsonSerializerSettings = new JsonSerializerSettings() + { + Formatting = Formatting.Indented, + TypeNameHandling = TypeNameHandling.Auto + }; + jsonSerializerSettings.Converters.Add(new UnitsNetJsonConverter()); + return jsonSerializerSettings; + } } internal class TestObj @@ -303,6 +359,12 @@ internal class TestObj public Frequency NonNullableFrequency { get; set; } } + internal class TestObjWithValueAndUnit + { + public double Value { get; set; } + public string Unit { get; set; } + } + internal class ComparableClass : IComparable { public int Value { get; set; } @@ -310,11 +372,35 @@ public int CompareTo(object obj) { return Value.CompareTo(obj); } + + // Needed for virfying that the deserialized object is the same, should not affect the serilization code + public override bool Equals(object obj) + { + if (obj == null || GetType() != obj.GetType()) + { + return false; + } + return Value.Equals(((ComparableClass)obj).Value); + } + + public override int GetHashCode() + { + return Value.GetHashCode(); + } } internal class TestObjWithIComparable { public IComparable Value { get; set; } } + + internal class TestObjWithThreeIComparable + { + public IComparable Value1 { get; set; } + + public IComparable Value2 { get; set; } + + public IComparable Value3 { get; set; } + } } } \ No newline at end of file diff --git a/UnitsNet.Serialization.JsonNet/UnitsNetJsonConverter.cs b/UnitsNet.Serialization.JsonNet/UnitsNetJsonConverter.cs index 5840e14729..da2c4351ee 100644 --- a/UnitsNet.Serialization.JsonNet/UnitsNetJsonConverter.cs +++ b/UnitsNet.Serialization.JsonNet/UnitsNetJsonConverter.cs @@ -117,7 +117,7 @@ where m.Name.Equals("From", StringComparison.InvariantCulture) && private static object TryDeserializeIComparable(JsonReader reader, JsonSerializer serializer) { JToken token = JToken.Load(reader); - if (!token.HasValues || token["Unit"] == null || token["Value"] == null) + if (!token.HasValues || token[nameof(ValueUnit.Unit)] == null || token[nameof(ValueUnit.Value)] == null) { JsonSerializer localSerializer = new JsonSerializer() { @@ -129,8 +129,8 @@ private static object TryDeserializeIComparable(JsonReader reader, JsonSerialize { return new ValueUnit() { - Unit = token["Unit"].ToString(), - Value = token["Value"].ToObject() + Unit = token[nameof(ValueUnit.Unit)].ToString(), + Value = token[nameof(ValueUnit.Value)].ToObject() }; } } @@ -225,12 +225,11 @@ public override bool CanConvert(Type objectType) return CanConvertNullable(objectType); } - return objectType.Namespace != null && - (objectType.Namespace.Equals("UnitsNet") || - objectType == typeof(ValueUnit) || + return objectType.Namespace != null && + (objectType.Namespace.Equals(nameof(UnitsNet)) || + objectType == typeof(ValueUnit) || // All unit types implement IComparable - objectType==typeof(IComparable) || - objectType.FullName.StartsWith("System." + nameof(IComparable))); + objectType == typeof(IComparable)); } /// @@ -252,7 +251,7 @@ protected virtual bool CanConvertNullable(Type objectType) { // Need to look at the FullName in order to determine if the nullable type contains a UnitsNet type. // For example: FullName = 'System.Nullable`1[[UnitsNet.Frequency, UnitsNet, Version=3.19.0.0, Culture=neutral, PublicKeyToken=null]]' - return objectType.FullName != null && objectType.FullName.Contains("UnitsNet."); + return objectType.FullName != null && objectType.FullName.Contains(nameof(UnitsNet) + "."); } #endregion From c56f4a4279e5a10ab12fa15bcdc234a7e594bb97 Mon Sep 17 00:00:00 2001 From: Erik Ovegard Date: Mon, 31 Oct 2016 16:36:25 +0100 Subject: [PATCH 3/5] Added missing IComparable --- .../UnitsNetJsonConverterTests.cs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/UnitsNet.Serialization.JsonNet.Tests/UnitsNetJsonConverterTests.cs b/UnitsNet.Serialization.JsonNet.Tests/UnitsNetJsonConverterTests.cs index 738b76c371..ed1c1b70f4 100644 --- a/UnitsNet.Serialization.JsonNet.Tests/UnitsNetJsonConverterTests.cs +++ b/UnitsNet.Serialization.JsonNet.Tests/UnitsNetJsonConverterTests.cs @@ -359,10 +359,15 @@ internal class TestObj public Frequency NonNullableFrequency { get; set; } } - internal class TestObjWithValueAndUnit + internal class TestObjWithValueAndUnit : IComparable { public double Value { get; set; } public string Unit { get; set; } + + public int CompareTo(object obj) + { + return Value.CompareTo(obj); + } } internal class ComparableClass : IComparable From 5a165d76ff4f8b46269b159bdd8c45653b71dbab Mon Sep 17 00:00:00 2001 From: Erik Ovegard Date: Tue, 1 Nov 2016 10:38:05 +0100 Subject: [PATCH 4/5] Changes based on code review by @anjdreas --- .../UnitsNetJsonConverterTests.cs | 32 +++++++++---------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/UnitsNet.Serialization.JsonNet.Tests/UnitsNetJsonConverterTests.cs b/UnitsNet.Serialization.JsonNet.Tests/UnitsNetJsonConverterTests.cs index ed1c1b70f4..4749e0cabc 100644 --- a/UnitsNet.Serialization.JsonNet.Tests/UnitsNetJsonConverterTests.cs +++ b/UnitsNet.Serialization.JsonNet.Tests/UnitsNetJsonConverterTests.cs @@ -235,9 +235,9 @@ public void UnitInIComparable_ExpectUnitCorrectlyDeserialized() { Value = Power.FromWatts(10) }; - JsonSerializerSettings jsonSerializerSettings = CreateJsonSerializerSettigns(); + JsonSerializerSettings jsonSerializerSettings = CreateJsonSerializerSettings(); - string json = JsonConvert.SerializeObject(testObjWithIComparable,jsonSerializerSettings); + string json = JsonConvert.SerializeObject(testObjWithIComparable, jsonSerializerSettings); var deserializedTestObject = JsonConvert.DeserializeObject(json,jsonSerializerSettings); @@ -252,7 +252,7 @@ public void DoubleInIComparable_ExpectUnitCorrectlyDeserialized() { Value = 10.0 }; - JsonSerializerSettings jsonSerializerSettings = CreateJsonSerializerSettigns(); + JsonSerializerSettings jsonSerializerSettings = CreateJsonSerializerSettings(); string json = JsonConvert.SerializeObject(testObjWithIComparable, jsonSerializerSettings); @@ -269,7 +269,7 @@ public void ClassInIComparable_ExpectUnitCorrectlyDeserialized() { Value = new ComparableClass() { Value = 10 } }; - JsonSerializerSettings jsonSerializerSettings = CreateJsonSerializerSettigns(); + JsonSerializerSettings jsonSerializerSettings = CreateJsonSerializerSettings(); string json = JsonConvert.SerializeObject(testObjWithIComparable, jsonSerializerSettings); var deserializedTestObject = JsonConvert.DeserializeObject(json, jsonSerializerSettings); @@ -286,7 +286,7 @@ public void OtherObjectWithUnitAndValue_ExpectCorrectResturnValues() Value = 5, Unit = "Test", }; - JsonSerializerSettings jsonSerializerSettings = CreateJsonSerializerSettigns(); + JsonSerializerSettings jsonSerializerSettings = CreateJsonSerializerSettings(); string json = JsonConvert.SerializeObject(testObjWithValueAndUnit, jsonSerializerSettings); TestObjWithValueAndUnit deserializedTestObject = JsonConvert.DeserializeObject(json, jsonSerializerSettings); @@ -308,20 +308,20 @@ public void ThreeObjectsInIComparableWithDifferentValues_ExpectAllCorrectlyDeser Value2 = comparable2, Value3 = comparable3, }; - JsonSerializerSettings jsonSerializerSettings = CreateJsonSerializerSettigns(); + JsonSerializerSettings jsonSerializerSettings = CreateJsonSerializerSettings(); string json = JsonConvert.SerializeObject(testObjWithIComparable, jsonSerializerSettings); var deserializedTestObject = JsonConvert.DeserializeObject(json, jsonSerializerSettings); Assert.That(deserializedTestObject.Value1.GetType(), Is.EqualTo(comparable1.GetType())); - Assert.That(((deserializedTestObject.Value1)), Is.EqualTo(comparable1)); + Assert.That((deserializedTestObject.Value1), Is.EqualTo(comparable1)); Assert.That(deserializedTestObject.Value2.GetType(), Is.EqualTo(comparable2.GetType())); - Assert.That(((deserializedTestObject.Value2)), Is.EqualTo(comparable2)); + Assert.That((deserializedTestObject.Value2), Is.EqualTo(comparable2)); Assert.That(deserializedTestObject.Value3.GetType(), Is.EqualTo(comparable3.GetType())); - Assert.That(((deserializedTestObject.Value3)), Is.EqualTo(comparable3)); + Assert.That((deserializedTestObject.Value3), Is.EqualTo(comparable3)); } - public static object[] TestObjectsForThreeObjectsInIComparableWithDifferentValues_ExpectAllCorrectlyDeserialized + private static object[] TestObjectsForThreeObjectsInIComparableWithDifferentValues_ExpectAllCorrectlyDeserialized { get { @@ -341,7 +341,7 @@ public static object[] TestObjectsForThreeObjectsInIComparableWithDifferentValue } } - private static JsonSerializerSettings CreateJsonSerializerSettigns() + private static JsonSerializerSettings CreateJsonSerializerSettings() { var jsonSerializerSettings = new JsonSerializerSettings() { @@ -353,13 +353,13 @@ private static JsonSerializerSettings CreateJsonSerializerSettigns() } } - internal class TestObj + private class TestObj { public Frequency? NullableFrequency { get; set; } public Frequency NonNullableFrequency { get; set; } } - internal class TestObjWithValueAndUnit : IComparable + private class TestObjWithValueAndUnit : IComparable { public double Value { get; set; } public string Unit { get; set; } @@ -370,7 +370,7 @@ public int CompareTo(object obj) } } - internal class ComparableClass : IComparable + private class ComparableClass : IComparable { public int Value { get; set; } public int CompareTo(object obj) @@ -394,12 +394,12 @@ public override int GetHashCode() } } - internal class TestObjWithIComparable + private class TestObjWithIComparable { public IComparable Value { get; set; } } - internal class TestObjWithThreeIComparable + private class TestObjWithThreeIComparable { public IComparable Value1 { get; set; } From 5bff42e7a34895dec309416afc2d1b7935c5962b Mon Sep 17 00:00:00 2001 From: Erik Ovegard Date: Wed, 2 Nov 2016 09:56:37 +0100 Subject: [PATCH 5/5] Removed TestCaseSource --- .../UnitsNetJsonConverterTests.cs | 45 +++++-------------- 1 file changed, 11 insertions(+), 34 deletions(-) diff --git a/UnitsNet.Serialization.JsonNet.Tests/UnitsNetJsonConverterTests.cs b/UnitsNet.Serialization.JsonNet.Tests/UnitsNetJsonConverterTests.cs index 4749e0cabc..5fa345c71e 100644 --- a/UnitsNet.Serialization.JsonNet.Tests/UnitsNetJsonConverterTests.cs +++ b/UnitsNet.Serialization.JsonNet.Tests/UnitsNetJsonConverterTests.cs @@ -296,49 +296,26 @@ public void OtherObjectWithUnitAndValue_ExpectCorrectResturnValues() Assert.That(deserializedTestObject.Unit, Is.EqualTo("Test")); } - [Test, TestCaseSource(nameof(TestObjectsForThreeObjectsInIComparableWithDifferentValues_ExpectAllCorrectlyDeserialized))] - public void ThreeObjectsInIComparableWithDifferentValues_ExpectAllCorrectlyDeserialized( - IComparable comparable1, - IComparable comparable2, - IComparable comparable3) + [Test] + public void ThreeObjectsInIComparableWithDifferentValues_ExpectAllCorrectlyDeserialized() { TestObjWithThreeIComparable testObjWithIComparable = new TestObjWithThreeIComparable() { - Value1 = comparable1, - Value2 = comparable2, - Value3 = comparable3, + Value1 = 10.0, + Value2 = Power.FromWatts(19), + Value3 = new ComparableClass() { Value = 10 }, }; JsonSerializerSettings jsonSerializerSettings = CreateJsonSerializerSettings(); string json = JsonConvert.SerializeObject(testObjWithIComparable, jsonSerializerSettings); var deserializedTestObject = JsonConvert.DeserializeObject(json, jsonSerializerSettings); - Assert.That(deserializedTestObject.Value1.GetType(), Is.EqualTo(comparable1.GetType())); - Assert.That((deserializedTestObject.Value1), Is.EqualTo(comparable1)); - Assert.That(deserializedTestObject.Value2.GetType(), Is.EqualTo(comparable2.GetType())); - Assert.That((deserializedTestObject.Value2), Is.EqualTo(comparable2)); - Assert.That(deserializedTestObject.Value3.GetType(), Is.EqualTo(comparable3.GetType())); - Assert.That((deserializedTestObject.Value3), Is.EqualTo(comparable3)); - } - - private static object[] TestObjectsForThreeObjectsInIComparableWithDifferentValues_ExpectAllCorrectlyDeserialized - { - get - { - List result = new List(); - var objects = new object[] { 10.0, Power.FromWatts(19), new ComparableClass() { Value = 10 } }; - for (int i = 0; i < objects.Length; i++) - { - for (int j = 0; j < objects.Length; j++) - { - for (int k = 0; k < objects.Length; k++) - { - result.Add(new object[] { objects[i], objects[j], objects[k]}); - } - } - } - return result.ToArray(); - } + Assert.That(deserializedTestObject.Value1.GetType(), Is.EqualTo(typeof(double))); + Assert.That((deserializedTestObject.Value1), Is.EqualTo(10.0)); + Assert.That(deserializedTestObject.Value2.GetType(), Is.EqualTo(typeof(Power))); + Assert.That((deserializedTestObject.Value2), Is.EqualTo(Power.FromWatts(19))); + Assert.That(deserializedTestObject.Value3.GetType(), Is.EqualTo(typeof(ComparableClass))); + Assert.That((deserializedTestObject.Value3), Is.EqualTo(testObjWithIComparable.Value3)); } private static JsonSerializerSettings CreateJsonSerializerSettings()