Skip to content

Commit 437fdd5

Browse files
authored
Replace Settings with JsonSerializerOptions (#333)
***NO_CI***
1 parent f1439fc commit 437fdd5

16 files changed

+378
-210
lines changed

nanoFramework.Json.Test/Configuration/SettingsTests.cs

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,67 @@
44
//
55

66
using nanoFramework.Json.Configuration;
7+
using nanoFramework.Json.Resolvers;
8+
using nanoFramework.Json.Test.Mocks;
79
using nanoFramework.TestFramework;
810

911
namespace nanoFramework.Json.Test.Configuration
1012
{
1113
[TestClass]
1214
public class SettingsTests
1315
{
16+
private static bool _caseSensitive;
17+
private static IMemberResolver _resolver;
18+
private static bool _throwExceptionWhenPropertyNotFound;
19+
20+
[Cleanup]
21+
public void Cleanup()
22+
{
23+
// Restore default settings
24+
JsonSerializerOptions.Default.PropertyNameCaseInsensitive = _caseSensitive;
25+
JsonSerializerOptions.Default.Resolver = _resolver;
26+
JsonSerializerOptions.Default.ThrowExceptionWhenPropertyNotFound = _throwExceptionWhenPropertyNotFound;
27+
}
28+
29+
[Setup]
30+
public void Setup()
31+
{
32+
// Capture default settings
33+
_caseSensitive = JsonSerializerOptions.Default.PropertyNameCaseInsensitive;
34+
_resolver = JsonSerializerOptions.Default.Resolver;
35+
_throwExceptionWhenPropertyNotFound = JsonSerializerOptions.Default.ThrowExceptionWhenPropertyNotFound;
36+
}
37+
1438
[TestMethod]
15-
public void CaseSensitive_Should_BeTrueByDefault()
39+
public void CaseSensitive_Should_Delegate_To_JsonSettings()
1640
{
17-
Assert.AreEqual(Settings.CaseSensitive, true);
41+
#pragma warning disable CS0618
42+
Settings.CaseSensitive = !Settings.CaseSensitive;
43+
44+
Assert.AreEqual(Settings.CaseSensitive, !JsonSerializerOptions.Default.PropertyNameCaseInsensitive);
45+
#pragma warning restore CS0618
1846
}
1947

2048
[TestMethod]
21-
public void ThrowExceptionWhenPropertyNotFound_Should_BeFalseByDefault()
49+
public void Resolver_Should_Delegate_To_JsonSettings()
2250
{
23-
Assert.AreEqual(Settings.ThrowExceptionWhenPropertyNotFound, false);
51+
var resolver = new MockMemberResolver();
52+
53+
#pragma warning disable CS0618
54+
Settings.Resolver = resolver;
55+
56+
Assert.AreEqual(resolver, JsonSerializerOptions.Default.Resolver);
57+
#pragma warning restore CS0618
58+
}
59+
60+
[TestMethod]
61+
public void ThrowExceptionWhenPropertyNotFound_Should_Delegate_To_JsonSettings()
62+
{
63+
#pragma warning disable CS0618
64+
Settings.ThrowExceptionWhenPropertyNotFound = !Settings.ThrowExceptionWhenPropertyNotFound;
65+
66+
Assert.AreEqual(Settings.ThrowExceptionWhenPropertyNotFound, JsonSerializerOptions.Default.ThrowExceptionWhenPropertyNotFound);
67+
#pragma warning restore CS0618
2468
}
2569
}
2670
}

nanoFramework.Json.Test/JsonUnitTests.cs renamed to nanoFramework.Json.Test/JsonSerializerOptionsTests.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
namespace nanoFramework.Json.Test
1313
{
1414
[TestClass]
15-
public class JsonUnitTests
15+
public class JsonSerializerOptionsTests
1616
{
1717
[Setup]
1818
public void Initialize()
@@ -32,7 +32,7 @@ public void Can_serialize_and_deserialize_object_with_self_referencing_static_fi
3232
OutputHelper.WriteLine("Can_serialize_and_deserialize_object_with_self_referencing_static_field() - Starting test...");
3333

3434
var serialized = JsonConvert.SerializeObject(JsonTestStaticProperty.StaticProperty);
35-
var deserialized = (JsonTestStaticProperty) JsonConvert.DeserializeObject(serialized, typeof(JsonTestStaticProperty));
35+
var deserialized = (JsonTestStaticProperty)JsonConvert.DeserializeObject(serialized, typeof(JsonTestStaticProperty));
3636

3737
Assert.AreEqual(
3838
JsonTestStaticProperty.StaticProperty.InstanceProperty,
@@ -930,7 +930,7 @@ public void CanDeserializeInvocationReceiveMessage_03()
930930
Assert.AreEqual(dserResult.target, "ReceiveAdvancedMessage", "target value is not correct");
931931

932932
Assert.AreEqual((int)dserResult.arguments[2], 3, "arguments[2] value is not correct");
933-
Assert.IsType(typeof(ArrayList), dserResult.arguments, "arguments type it's wrong after deserialization");
933+
Assert.IsInstanceOfType(dserResult.arguments, typeof(ArrayList), "arguments type it's wrong after deserialization");
934934
Assert.AreEqual(dserResult.arguments.Count, 3, $"number of arguments is different than expected: {dserResult.arguments.Count}");
935935

936936
Hashtable arg0 = (Hashtable)dserResult.arguments[0];
@@ -971,7 +971,7 @@ public void CanDeserializeInvocationReceiveMessage_04()
971971
Assert.AreEqual(dserResult.target, "ReceiveAdvancedMessage", "target value is not correct");
972972

973973
Assert.AreEqual((int)dserResult.arguments[2], 3, "arguments[2] value is not correct");
974-
Assert.IsType(typeof(ArrayList), dserResult.arguments, "arguments type it's wrong after deserialization");
974+
Assert.IsInstanceOfType(dserResult.arguments, typeof(ArrayList), "arguments type it's wrong after deserialization");
975975
Assert.AreEqual(dserResult.arguments.Count, 3, $"number of arguments is different than expected: {dserResult.arguments.Count}");
976976

977977
OutputHelper.WriteLine("Serializing dserResult.arguments[0]");
@@ -1022,7 +1022,7 @@ public void CanDeserializeInvocationReceiveMessage_05()
10221022
Assert.AreEqual(dserResult.type, 1, "type value is not correct");
10231023
Assert.AreEqual(dserResult.target, "ReceiveMessage", "target value is not correct");
10241024

1025-
Assert.IsType(typeof(ArrayList), dserResult.arguments, "arguments type it's wrong after deserialization");
1025+
Assert.IsInstanceOfType(dserResult.arguments, typeof(ArrayList), "arguments type it's wrong after deserialization");
10261026
Assert.AreEqual(dserResult.arguments.Count, 2, $"number of arguments is different than expected: {dserResult.arguments.Count}");
10271027

10281028
OutputHelper.WriteLine($"SerializingdserResult.arguments[0]:{dserResult.arguments[0]}");
@@ -1081,13 +1081,13 @@ public void DeserializeArrayList()
10811081

10821082
Hashtable desired = (Hashtable)hash["desired"];
10831083

1084-
Assert.IsType(typeof(string), desired["Authorization"], "Authorization is not a string and it should be.");
1084+
Assert.IsInstanceOfType(desired["Authorization"], typeof(string), "Authorization is not a string and it should be.");
10851085

10861086
Assert.AreEqual("sp=r&st=2021-06-12T09:11:53Z&se=2021-06-14T17:11:53Z&spr=https&sv=2020-02-10&sr=c&sig=rn125LiO55RSCoEs4IEaCgg%2BuXKETdEZQPygxVjCHiY%3D", (string)desired["Authorization"], "Authorization string doesn't match original value.");
10871087

10881088
ArrayList files = (ArrayList)desired["Files"];
10891089

1090-
Assert.IsType(typeof(string), files[0]);
1090+
Assert.IsInstanceOfType(files[0], typeof(string));
10911091
Assert.AreEqual("Iot.Device.Bmxx80.pe", (string)files[0]);
10921092
}
10931093

@@ -1100,12 +1100,12 @@ public void DeserializeArrayListElements()
11001100

11011101
Hashtable hash = (Hashtable)JsonConvert.DeserializeObject(correctValue, typeof(Hashtable));
11021102

1103-
Assert.IsType(typeof(string), hash["Authorization"], "Authorization is not a string and it should be.");
1103+
Assert.IsInstanceOfType(hash["Authorization"], typeof(string), "Authorization is not a string and it should be.");
11041104
Assert.AreEqual("sp=r&st=2021-06-12T09:11:53Z&se=2021-06-14T17:11:53Z&spr=https&sv=2020-02-10&sr=c&sig=rn125LiO55RSCoEs4IEaCgg%2BuXKETdEZQPygxVjCHiY%3D", (string)hash["Authorization"], "Authorization string doesn't match original value.");
11051105

11061106
ArrayList files = (ArrayList)hash["Files"];
11071107

1108-
Assert.IsType(typeof(string), files[0]);
1108+
Assert.IsInstanceOfType(files[0], typeof(string));
11091109
Assert.AreEqual("Iot.Device.Bmxx80.pe", (string)files[0]);
11101110
}
11111111

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//
2+
// Copyright (c) .NET Foundation and Contributors
3+
// See LICENSE file in the project root for full license information.
4+
//
5+
6+
using nanoFramework.TestFramework;
7+
8+
namespace nanoFramework.Json.Test
9+
{
10+
[TestClass]
11+
public class JsonSettingsTests
12+
{
13+
[TestMethod]
14+
public void PropertyNameCaseInsensitive_Should_Be_False_By_Default()
15+
{
16+
Assert.IsFalse(JsonSerializerOptions.Default.PropertyNameCaseInsensitive);
17+
}
18+
19+
[TestMethod]
20+
public void ThrowExceptionWhenPropertyNotFound_Should_Be_False_By_Default()
21+
{
22+
Assert.IsFalse(JsonSerializerOptions.Default.PropertyNameCaseInsensitive);
23+
}
24+
}
25+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System;
2+
using nanoFramework.Json.Resolvers;
3+
4+
namespace nanoFramework.Json.Test.Mocks
5+
{
6+
internal class MockMemberResolver: IMemberResolver
7+
{
8+
public MemberSet Get(string memberName, Type objectType, JsonSerializerOptions options)
9+
{
10+
return new MemberSet(true);
11+
}
12+
}
13+
}

nanoFramework.Json.Test/Resolvers/MemberResolverCaseInsensitiveExceptionTests.cs

Lines changed: 14 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@
33
// See LICENSE file in the project root for full license information.
44
//
55

6-
using nanoFramework.Json.Configuration;
76
using nanoFramework.Json.Resolvers;
87
using nanoFramework.TestFramework;
9-
using System;
108

119
namespace nanoFramework.Json.Test.Resolvers
1210
{
@@ -16,57 +14,33 @@ public class MemberResolverCaseInsensitiveExceptionTests
1614
private sealed class TestClass
1715
{
1816
public int NoGetProperty { private get; set; } = 1;
19-
public int NoSetProperty { get; } = 1;
20-
}
21-
22-
[Setup]
23-
public void MemberResolverCaseInsensitiveExceptionTests_Setup()
24-
{
25-
Settings.ThrowExceptionWhenPropertyNotFound = true;
26-
Settings.CaseSensitive = false;
27-
}
28-
29-
[Cleanup]
30-
public void MemberResolverCaseInsensitiveExceptionTests_Cleanup()
31-
{
32-
Settings.ThrowExceptionWhenPropertyNotFound = false;
33-
Settings.CaseSensitive = true;
17+
public int NoSetProperty => 1;
3418
}
3519

3620
[TestMethod]
3721
public void MemberResolverCaseInsensitiveExceptionTests_Get_ShouldSkipPropertyWithoutGet()
3822
{
39-
var resolver = new MemberResolver();
40-
41-
try
42-
{
43-
resolver.Get(nameof(TestClass.NoGetProperty), typeof(TestClass));
44-
}
45-
catch (DeserializationException)
23+
var options = new JsonSerializerOptions
4624
{
47-
// Intended. Method should throw this type of exception when no set method.
48-
return;
49-
}
25+
PropertyNameCaseInsensitive = true,
26+
ThrowExceptionWhenPropertyNotFound = true,
27+
};
5028

51-
throw new InvalidOperationException($"Should throw {nameof(DeserializationException)}.");
29+
var sut = new MemberResolver();
30+
Assert.ThrowsException(typeof(DeserializationException), () => sut.Get(nameof(TestClass.NoGetProperty), typeof(TestClass), options));
5231
}
5332

5433
[TestMethod]
5534
public void MemberResolverCaseInsensitiveExceptionTests_Get_ShouldSkipPropertyWithoutSet()
5635
{
57-
var resolver = new MemberResolver();
58-
59-
try
60-
{
61-
resolver.Get(nameof(TestClass.NoSetProperty), typeof(TestClass));
62-
}
63-
catch (DeserializationException)
36+
var options = new JsonSerializerOptions
6437
{
65-
// Intended. Method should throw this type of exception when no set method.
66-
return;
67-
}
38+
PropertyNameCaseInsensitive = true,
39+
ThrowExceptionWhenPropertyNotFound = true,
40+
};
6841

69-
throw new InvalidOperationException($"Should throw {nameof(DeserializationException)}.");
42+
var sut = new MemberResolver();
43+
Assert.ThrowsException(typeof(DeserializationException), () => sut.Get(nameof(TestClass.NoSetProperty), typeof(TestClass), options));
7044
}
7145
}
72-
}
46+
}

nanoFramework.Json.Test/Resolvers/MemberResolverCaseInsensitiveTests.cs

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
// See LICENSE file in the project root for full license information.
55
//
66

7-
using nanoFramework.Json.Configuration;
87
using nanoFramework.Json.Resolvers;
98
using nanoFramework.TestFramework;
109

@@ -16,29 +15,22 @@ public class MemberResolverCaseInsensitiveTests
1615
private sealed class TestClass
1716
{
1817
public int TestField = 1;
19-
public int TestProperty { get; set; } = 1;
20-
}
21-
22-
[Setup]
23-
public void MemberResolverCaseInsensitiveTests_Setup()
24-
{
25-
Settings.CaseSensitive = false;
26-
}
27-
28-
[Cleanup]
29-
public void MemberResolverCaseInsensitive_Cleanup()
30-
{
31-
Settings.CaseSensitive = true;
18+
public int TestProperty { get; init; } = 1;
3219
}
3320

3421
[TestMethod]
3522
public void MemberResolverCaseInsensitive_Get_ShouldReturnCaseInsensitivePropertyWhenSet()
3623
{
37-
var resolver = new MemberResolver();
24+
var options = new JsonSerializerOptions
25+
{
26+
PropertyNameCaseInsensitive = true
27+
};
28+
29+
var sut = new MemberResolver();
3830
var classInstance = new TestClass();
39-
var valueToSet = 6;
31+
const int valueToSet = 6;
4032

41-
var member = resolver.Get(nameof(TestClass.TestProperty).ToLower(), typeof(TestClass));
33+
var member = sut.Get(nameof(TestClass.TestProperty).ToLower(), typeof(TestClass), options);
4234
member.SetValue(classInstance, valueToSet);
4335

4436
Assert.AreEqual(classInstance.TestProperty, valueToSet);
@@ -49,11 +41,16 @@ public void MemberResolverCaseInsensitive_Get_ShouldReturnCaseInsensitivePropert
4941
[TestMethod]
5042
public void MemberResolverCaseInsensitive_Get_ShouldReturnCaseInsensitiveFieldWhenSet()
5143
{
52-
var resolver = new MemberResolver();
44+
var options = new JsonSerializerOptions
45+
{
46+
PropertyNameCaseInsensitive = true
47+
};
48+
49+
var sut = new MemberResolver();
5350
var classInstance = new TestClass();
54-
var valueToSet = 5;
51+
const int valueToSet = 5;
5552

56-
var member = resolver.Get(nameof(TestClass.TestField).ToLower(), typeof(TestClass));
53+
var member = sut.Get(nameof(TestClass.TestField).ToLower(), typeof(TestClass), options);
5754
member.SetValue(classInstance, valueToSet);
5855

5956
Assert.AreEqual(classInstance.TestField, valueToSet);
@@ -63,11 +60,16 @@ public void MemberResolverCaseInsensitive_Get_ShouldReturnCaseInsensitiveFieldWh
6360
[TestMethod]
6461
public void MemberResolverCaseInsensitive_Get_ShouldSkipWhenNotFoundCaseInsensitiveProperty()
6562
{
66-
var resolver = new MemberResolver();
63+
var options = new JsonSerializerOptions
64+
{
65+
PropertyNameCaseInsensitive = true
66+
};
67+
68+
var sut = new MemberResolver();
6769

68-
var member = resolver.Get("NotExistingProperty", typeof(TestClass));
70+
var member = sut.Get("NotExistingProperty", typeof(TestClass), options);
6971

70-
Assert.AreEqual(member.Skip, true);
72+
Assert.IsTrue(member.Skip);
7173
}
7274
}
7375
}

0 commit comments

Comments
 (0)