Skip to content
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

Replace Settings with JsonSerializerOptions #333

Merged
merged 2 commits into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 48 additions & 4 deletions nanoFramework.Json.Test/Configuration/SettingsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,67 @@
//

using nanoFramework.Json.Configuration;
using nanoFramework.Json.Resolvers;
using nanoFramework.Json.Test.Mocks;
using nanoFramework.TestFramework;

namespace nanoFramework.Json.Test.Configuration
{
[TestClass]
public class SettingsTests
{
private static bool _caseSensitive;
private static IMemberResolver _resolver;
private static bool _throwExceptionWhenPropertyNotFound;

[Cleanup]
public void Cleanup()
{
// Restore default settings
JsonSerializerOptions.Default.PropertyNameCaseInsensitive = _caseSensitive;
JsonSerializerOptions.Default.Resolver = _resolver;
JsonSerializerOptions.Default.ThrowExceptionWhenPropertyNotFound = _throwExceptionWhenPropertyNotFound;
}

[Setup]
public void Setup()
{
// Capture default settings
_caseSensitive = JsonSerializerOptions.Default.PropertyNameCaseInsensitive;
_resolver = JsonSerializerOptions.Default.Resolver;
_throwExceptionWhenPropertyNotFound = JsonSerializerOptions.Default.ThrowExceptionWhenPropertyNotFound;
}

[TestMethod]
public void CaseSensitive_Should_BeTrueByDefault()
public void CaseSensitive_Should_Delegate_To_JsonSettings()
{
Assert.AreEqual(Settings.CaseSensitive, true);
#pragma warning disable CS0618
Settings.CaseSensitive = !Settings.CaseSensitive;

Assert.AreEqual(Settings.CaseSensitive, !JsonSerializerOptions.Default.PropertyNameCaseInsensitive);
#pragma warning restore CS0618
}

[TestMethod]
public void ThrowExceptionWhenPropertyNotFound_Should_BeFalseByDefault()
public void Resolver_Should_Delegate_To_JsonSettings()
{
Assert.AreEqual(Settings.ThrowExceptionWhenPropertyNotFound, false);
var resolver = new MockMemberResolver();

#pragma warning disable CS0618
Settings.Resolver = resolver;

Assert.AreEqual(resolver, JsonSerializerOptions.Default.Resolver);
#pragma warning restore CS0618
}

[TestMethod]
public void ThrowExceptionWhenPropertyNotFound_Should_Delegate_To_JsonSettings()
{
#pragma warning disable CS0618
Settings.ThrowExceptionWhenPropertyNotFound = !Settings.ThrowExceptionWhenPropertyNotFound;

Assert.AreEqual(Settings.ThrowExceptionWhenPropertyNotFound, JsonSerializerOptions.Default.ThrowExceptionWhenPropertyNotFound);
#pragma warning restore CS0618
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
namespace nanoFramework.Json.Test
{
[TestClass]
public class JsonUnitTests
public class JsonSerializerOptionsTests
{
[Setup]
public void Initialize()
Expand All @@ -32,7 +32,7 @@ public void Can_serialize_and_deserialize_object_with_self_referencing_static_fi
OutputHelper.WriteLine("Can_serialize_and_deserialize_object_with_self_referencing_static_field() - Starting test...");

var serialized = JsonConvert.SerializeObject(JsonTestStaticProperty.StaticProperty);
var deserialized = (JsonTestStaticProperty) JsonConvert.DeserializeObject(serialized, typeof(JsonTestStaticProperty));
var deserialized = (JsonTestStaticProperty)JsonConvert.DeserializeObject(serialized, typeof(JsonTestStaticProperty));

Assert.AreEqual(
JsonTestStaticProperty.StaticProperty.InstanceProperty,
Expand Down Expand Up @@ -930,7 +930,7 @@ public void CanDeserializeInvocationReceiveMessage_03()
Assert.AreEqual(dserResult.target, "ReceiveAdvancedMessage", "target value is not correct");

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

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

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

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

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

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

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

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

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.");

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

Assert.IsType(typeof(string), files[0]);
Assert.IsInstanceOfType(files[0], typeof(string));
Assert.AreEqual("Iot.Device.Bmxx80.pe", (string)files[0]);
}

Expand All @@ -1100,12 +1100,12 @@ public void DeserializeArrayListElements()

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

Assert.IsType(typeof(string), hash["Authorization"], "Authorization is not a string and it should be.");
Assert.IsInstanceOfType(hash["Authorization"], typeof(string), "Authorization is not a string and it should be.");
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.");

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

Assert.IsType(typeof(string), files[0]);
Assert.IsInstanceOfType(files[0], typeof(string));
Assert.AreEqual("Iot.Device.Bmxx80.pe", (string)files[0]);
}

Expand Down
25 changes: 25 additions & 0 deletions nanoFramework.Json.Test/JsonSettingsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//
// Copyright (c) .NET Foundation and Contributors
// See LICENSE file in the project root for full license information.
//

using nanoFramework.TestFramework;

namespace nanoFramework.Json.Test
{
[TestClass]
public class JsonSettingsTests
{
[TestMethod]
public void PropertyNameCaseInsensitive_Should_Be_False_By_Default()
{
Assert.IsFalse(JsonSerializerOptions.Default.PropertyNameCaseInsensitive);
}

[TestMethod]
public void ThrowExceptionWhenPropertyNotFound_Should_Be_False_By_Default()
{
Assert.IsFalse(JsonSerializerOptions.Default.PropertyNameCaseInsensitive);
}
}
}
13 changes: 13 additions & 0 deletions nanoFramework.Json.Test/Mocks/MockMemberResolver.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using nanoFramework.Json.Resolvers;

namespace nanoFramework.Json.Test.Mocks
{
internal class MockMemberResolver: IMemberResolver
{
public MemberSet Get(string memberName, Type objectType, JsonSerializerOptions options)
{
return new MemberSet(true);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@
// See LICENSE file in the project root for full license information.
//

using nanoFramework.Json.Configuration;
using nanoFramework.Json.Resolvers;
using nanoFramework.TestFramework;
using System;

namespace nanoFramework.Json.Test.Resolvers
{
Expand All @@ -16,57 +14,33 @@ public class MemberResolverCaseInsensitiveExceptionTests
private sealed class TestClass
{
public int NoGetProperty { private get; set; } = 1;
public int NoSetProperty { get; } = 1;
}

[Setup]
public void MemberResolverCaseInsensitiveExceptionTests_Setup()
{
Settings.ThrowExceptionWhenPropertyNotFound = true;
Settings.CaseSensitive = false;
}

[Cleanup]
public void MemberResolverCaseInsensitiveExceptionTests_Cleanup()
{
Settings.ThrowExceptionWhenPropertyNotFound = false;
Settings.CaseSensitive = true;
public int NoSetProperty => 1;
}

[TestMethod]
public void MemberResolverCaseInsensitiveExceptionTests_Get_ShouldSkipPropertyWithoutGet()
{
var resolver = new MemberResolver();

try
{
resolver.Get(nameof(TestClass.NoGetProperty), typeof(TestClass));
}
catch (DeserializationException)
var options = new JsonSerializerOptions
{
// Intended. Method should throw this type of exception when no set method.
return;
}
PropertyNameCaseInsensitive = true,
ThrowExceptionWhenPropertyNotFound = true,
};

throw new InvalidOperationException($"Should throw {nameof(DeserializationException)}.");
var sut = new MemberResolver();
Assert.ThrowsException(typeof(DeserializationException), () => sut.Get(nameof(TestClass.NoGetProperty), typeof(TestClass), options));
}

[TestMethod]
public void MemberResolverCaseInsensitiveExceptionTests_Get_ShouldSkipPropertyWithoutSet()
{
var resolver = new MemberResolver();

try
{
resolver.Get(nameof(TestClass.NoSetProperty), typeof(TestClass));
}
catch (DeserializationException)
var options = new JsonSerializerOptions
{
// Intended. Method should throw this type of exception when no set method.
return;
}
PropertyNameCaseInsensitive = true,
ThrowExceptionWhenPropertyNotFound = true,
};

throw new InvalidOperationException($"Should throw {nameof(DeserializationException)}.");
var sut = new MemberResolver();
Assert.ThrowsException(typeof(DeserializationException), () => sut.Get(nameof(TestClass.NoSetProperty), typeof(TestClass), options));
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
// See LICENSE file in the project root for full license information.
//

using nanoFramework.Json.Configuration;
using nanoFramework.Json.Resolvers;
using nanoFramework.TestFramework;

Expand All @@ -16,29 +15,22 @@ public class MemberResolverCaseInsensitiveTests
private sealed class TestClass
{
public int TestField = 1;
public int TestProperty { get; set; } = 1;
}

[Setup]
public void MemberResolverCaseInsensitiveTests_Setup()
{
Settings.CaseSensitive = false;
}

[Cleanup]
public void MemberResolverCaseInsensitive_Cleanup()
{
Settings.CaseSensitive = true;
public int TestProperty { get; init; } = 1;
}

[TestMethod]
public void MemberResolverCaseInsensitive_Get_ShouldReturnCaseInsensitivePropertyWhenSet()
{
var resolver = new MemberResolver();
var options = new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true
};

var sut = new MemberResolver();
var classInstance = new TestClass();
var valueToSet = 6;
const int valueToSet = 6;

var member = resolver.Get(nameof(TestClass.TestProperty).ToLower(), typeof(TestClass));
var member = sut.Get(nameof(TestClass.TestProperty).ToLower(), typeof(TestClass), options);
member.SetValue(classInstance, valueToSet);

Assert.AreEqual(classInstance.TestProperty, valueToSet);
Expand All @@ -49,11 +41,16 @@ public void MemberResolverCaseInsensitive_Get_ShouldReturnCaseInsensitivePropert
[TestMethod]
public void MemberResolverCaseInsensitive_Get_ShouldReturnCaseInsensitiveFieldWhenSet()
{
var resolver = new MemberResolver();
var options = new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true
};

var sut = new MemberResolver();
var classInstance = new TestClass();
var valueToSet = 5;
const int valueToSet = 5;

var member = resolver.Get(nameof(TestClass.TestField).ToLower(), typeof(TestClass));
var member = sut.Get(nameof(TestClass.TestField).ToLower(), typeof(TestClass), options);
member.SetValue(classInstance, valueToSet);

Assert.AreEqual(classInstance.TestField, valueToSet);
Expand All @@ -63,11 +60,16 @@ public void MemberResolverCaseInsensitive_Get_ShouldReturnCaseInsensitiveFieldWh
[TestMethod]
public void MemberResolverCaseInsensitive_Get_ShouldSkipWhenNotFoundCaseInsensitiveProperty()
{
var resolver = new MemberResolver();
var options = new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true
};

var sut = new MemberResolver();

var member = resolver.Get("NotExistingProperty", typeof(TestClass));
var member = sut.Get("NotExistingProperty", typeof(TestClass), options);

Assert.AreEqual(member.Skip, true);
Assert.IsTrue(member.Skip);
}
}
}
Loading