diff --git a/docs/serializer-settings.md b/docs/serializer-settings.md index 9ce6840989..1a12c7133d 100644 --- a/docs/serializer-settings.md +++ b/docs/serializer-settings.md @@ -135,7 +135,7 @@ var settings = new JsonSerializerSettings DefaultValueHandling = DefaultValueHandling.Ignore }; ``` -snippet source | anchor +snippet source | anchor diff --git a/src/Directory.Build.props b/src/Directory.Build.props index 8ea2ad3168..522093048b 100644 --- a/src/Directory.Build.props +++ b/src/Directory.Build.props @@ -2,7 +2,7 @@ CS1591;CS0649;xUnit1026;xUnit1013;CS1573;VerifyTestsProjectDir;VerifySetParameters - 23.5.0 + 23.5.1 enable preview 1.0.0 diff --git a/src/StrictJsonTests/SerializationTests.CancellationToken_Cancelled.verified.json b/src/StrictJsonTests/SerializationTests.CancellationToken_Cancelled.verified.json new file mode 100644 index 0000000000..40c972296d --- /dev/null +++ b/src/StrictJsonTests/SerializationTests.CancellationToken_Cancelled.verified.json @@ -0,0 +1,10 @@ +{ + "IsCancellationRequested": true, + "Token": true, + "WaitHandle": { + "SafeWaitHandle": { + "IsInvalid": false, + "IsClosed": false + } + } +} \ No newline at end of file diff --git a/src/StrictJsonTests/SerializationTests.CancellationToken_CancelledDisposed.verified.json b/src/StrictJsonTests/SerializationTests.CancellationToken_CancelledDisposed.verified.json new file mode 100644 index 0000000000..fe3f2febf2 --- /dev/null +++ b/src/StrictJsonTests/SerializationTests.CancellationToken_CancelledDisposed.verified.json @@ -0,0 +1,4 @@ +{ + "IsCancellationRequested": true, + "Token": true +} \ No newline at end of file diff --git a/src/Verify.Tests/Serialization/SerializationTests.CancellationToken_Cancelled.verified.txt b/src/Verify.Tests/Serialization/SerializationTests.CancellationToken_Cancelled.verified.txt new file mode 100644 index 0000000000..80112d3134 --- /dev/null +++ b/src/Verify.Tests/Serialization/SerializationTests.CancellationToken_Cancelled.verified.txt @@ -0,0 +1,10 @@ +{ + IsCancellationRequested: true, + Token: true, + WaitHandle: { + SafeWaitHandle: { + IsInvalid: false, + IsClosed: false + } + } +} \ No newline at end of file diff --git a/src/Verify.Tests/Serialization/SerializationTests.CancellationToken_CancelledDisposed.verified.txt b/src/Verify.Tests/Serialization/SerializationTests.CancellationToken_CancelledDisposed.verified.txt new file mode 100644 index 0000000000..e380fc52f9 --- /dev/null +++ b/src/Verify.Tests/Serialization/SerializationTests.CancellationToken_CancelledDisposed.verified.txt @@ -0,0 +1,4 @@ +{ + IsCancellationRequested: true, + Token: true +} \ No newline at end of file diff --git a/src/Verify.Tests/Serialization/SerializationTests.cs b/src/Verify.Tests/Serialization/SerializationTests.cs index 7585a0f9d6..f67cc10c3d 100644 --- a/src/Verify.Tests/Serialization/SerializationTests.cs +++ b/src/Verify.Tests/Serialization/SerializationTests.cs @@ -3894,4 +3894,22 @@ public class Child { public Parent Parent { get; set; } } + + [Fact] + public async Task CancellationToken_Cancelled() + { + var cancelSource = new CancelSource(); + cancelSource.Cancel(); + await Verify(cancelSource.Token); + } + + [Fact] + public async Task CancellationToken_CancelledDisposed() + { + var cancelSource = new CancelSource(); + cancelSource.Cancel(); + var token = cancelSource.Token; + cancelSource.Dispose(); + await Verify(token); + } } \ No newline at end of file diff --git a/src/Verify/Serialization/Converters/CancelConverter.cs b/src/Verify/Serialization/Converters/CancelConverter.cs new file mode 100644 index 0000000000..b6d8ed5af8 --- /dev/null +++ b/src/Verify/Serialization/Converters/CancelConverter.cs @@ -0,0 +1,20 @@ +class CancelConverter : + WriteOnlyJsonConverter +{ + public override void Write(VerifyJsonWriter writer, Cancel value) + { + writer.WriteStartObject(); + + writer.WriteMember(value, value.IsCancellationRequested, "IsCancellationRequested"); + writer.WriteMember(value, value.CanBeCanceled, "Token"); + try + { + writer.WriteMember(value, value.WaitHandle, "WaitHandle"); + } + catch (ObjectDisposedException) + { + } + + writer.WriteEndObject(); + } +} \ No newline at end of file diff --git a/src/Verify/Serialization/SerializationSettings.cs b/src/Verify/Serialization/SerializationSettings.cs index 8ebcd5361a..65226fb63a 100644 --- a/src/Verify/Serialization/SerializationSettings.cs +++ b/src/Verify/Serialization/SerializationSettings.cs @@ -21,6 +21,7 @@ partial class SerializationSettings static MethodInfoConverter methodInfoConverter = new(); static FieldInfoConverter fieldInfoConverter = new(); static ConstructorInfoConverter constructorInfoConverter = new(); + static CancelConverter cancelConverter = new(); static ParameterInfoConverter parameterInfoConverter = new(); static PropertyInfoConverter propertyInfoConverter = new(); static ClaimConverter claimConverter = new(); @@ -108,6 +109,7 @@ JsonSerializerSettings BuildSettings() converters.Add(methodInfoConverter); converters.Add(fieldInfoConverter); converters.Add(constructorInfoConverter); + converters.Add(cancelConverter); converters.Add(propertyInfoConverter); converters.Add(parameterInfoConverter); converters.Add(claimConverter);