-
Notifications
You must be signed in to change notification settings - Fork 383
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Modified serilization to support units saved as IComparable #200
Changes from 3 commits
ded0ebe
3653228
c56f4a4
5a165d7
5bff42e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,7 @@ | |
using System; | ||
using Newtonsoft.Json; | ||
using NUnit.Framework; | ||
using System.Collections.Generic; | ||
|
||
namespace UnitsNet.Serialization.JsonNet.Tests | ||
{ | ||
|
@@ -226,12 +227,185 @@ 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) | ||
}; | ||
JsonSerializerSettings jsonSerializerSettings = CreateJsonSerializerSettigns(); | ||
|
||
string json = JsonConvert.SerializeObject(testObjWithIComparable,jsonSerializerSettings); | ||
|
||
var deserializedTestObject = JsonConvert.DeserializeObject<TestObjWithIComparable>(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 | ||
}; | ||
JsonSerializerSettings jsonSerializerSettings = CreateJsonSerializerSettigns(); | ||
|
||
string json = JsonConvert.SerializeObject(testObjWithIComparable, jsonSerializerSettings); | ||
|
||
var deserializedTestObject = JsonConvert.DeserializeObject<TestObjWithIComparable>(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 } | ||
}; | ||
JsonSerializerSettings jsonSerializerSettings = CreateJsonSerializerSettigns(); | ||
|
||
string json = JsonConvert.SerializeObject(testObjWithIComparable, jsonSerializerSettings); | ||
var deserializedTestObject = JsonConvert.DeserializeObject<TestObjWithIComparable>(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<TestObjWithValueAndUnit>(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))] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This part was slightly confusing to me, as I haven't used TestCaseSource before. If there is only one case, I think I would prefer a normal method that give me that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will actually be 27 tests (testing all combinations of objects). Might be a little too much. I started with 3 very similar tests and did this to reduce code duplication and then it was easy to add the loop with all combinations. I have to admit that I don't fully understand the serialization so I rather have some extra tests just to be sure. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, ok. 27 tests seem a bit overboard, yes. Tests are nice, but I want each test to bring value and from the way I understand the implementation design and the test, I don't see how testing multiple variations of Their docs helped me understand what goes on: http://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_TypeNameHandling.htm Basically they add a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oops, wrong link: http://www.newtonsoft.com/json/help/html/serializetypenamehandling.htm |
||
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<TestObjWithThreeIComparable>(json, jsonSerializerSettings); | ||
|
||
Assert.That(deserializedTestObject.Value1.GetType(), Is.EqualTo(comparable1.GetType())); | ||
Assert.That(((deserializedTestObject.Value1)), Is.EqualTo(comparable1)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Redundant parantheses. |
||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can be private. |
||
{ | ||
get | ||
{ | ||
List<object> result = new List<object>(); | ||
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() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Typo |
||
{ | ||
var jsonSerializerSettings = new JsonSerializerSettings() | ||
{ | ||
Formatting = Formatting.Indented, | ||
TypeNameHandling = TypeNameHandling.Auto | ||
}; | ||
jsonSerializerSettings.Converters.Add(new UnitsNetJsonConverter()); | ||
return jsonSerializerSettings; | ||
} | ||
} | ||
|
||
internal class TestObj | ||
{ | ||
public Frequency? NullableFrequency { get; set; } | ||
public Frequency NonNullableFrequency { get; set; } | ||
} | ||
|
||
internal class TestObjWithValueAndUnit : IComparable | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All these types can be private, can't they? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's true. Followed the existing class, will change all of them. |
||
{ | ||
public double Value { get; set; } | ||
public string Unit { get; set; } | ||
|
||
public int CompareTo(object obj) | ||
{ | ||
return Value.CompareTo(obj); | ||
} | ||
} | ||
|
||
internal class ComparableClass : IComparable | ||
{ | ||
public int Value { get; set; } | ||
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; } | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add space between params.