Skip to content

Commit 014aace

Browse files
author
Bart Koelman
committed
Switched to STJ in assertions
Note we need JsonDateTimeOffsetFormatSpecifier now, because STJ never tries to infer the CLR type from JSON values between quotes, while Newtonsoft does. So Newtonsoft would convert both values to date/time, effectively hiding the textual difference that was always there.
1 parent e2cd9bf commit 014aace

File tree

2 files changed

+16
-13
lines changed

2 files changed

+16
-13
lines changed

test/JsonApiDotNetCoreTests/IntegrationTests/Serialization/SerializationTests.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ namespace JsonApiDotNetCoreTests.IntegrationTests.Serialization
1818
{
1919
public sealed class SerializationTests : IClassFixture<IntegrationTestContext<TestableStartup<SerializationDbContext>, SerializationDbContext>>
2020
{
21+
private const string JsonDateTimeOffsetFormatSpecifier = "yyyy-MM-ddTHH:mm:ss.FFFFFFFK";
22+
2123
private readonly IntegrationTestContext<TestableStartup<SerializationDbContext>, SerializationDbContext> _testContext;
2224
private readonly SerializationFakers _fakers = new();
2325

@@ -110,7 +112,7 @@ await _testContext.RunOnDatabaseAsync(async dbContext =>
110112
""id"": """ + meetings[0].StringId + @""",
111113
""attributes"": {
112114
""title"": """ + meetings[0].Title + @""",
113-
""startTime"": """ + meetings[0].StartTime.ToString("O") + @""",
115+
""startTime"": """ + meetings[0].StartTime.ToString(JsonDateTimeOffsetFormatSpecifier) + @""",
114116
""duration"": """ + meetings[0].Duration + @""",
115117
""location"": {
116118
""lat"": " + meetings[0].Location.Latitude.ToString(CultureInfo.InvariantCulture) + @",
@@ -191,7 +193,7 @@ await _testContext.RunOnDatabaseAsync(async dbContext =>
191193
""id"": """ + meetings[0].StringId + @""",
192194
""attributes"": {
193195
""title"": """ + meetings[0].Title + @""",
194-
""startTime"": """ + meetings[0].StartTime.ToString("O") + @""",
196+
""startTime"": """ + meetings[0].StartTime.ToString(JsonDateTimeOffsetFormatSpecifier) + @""",
195197
""duration"": """ + meetings[0].Duration + @""",
196198
""location"": {
197199
""lat"": " + meetings[0].Location.Latitude.ToString(CultureInfo.InvariantCulture) + @",
@@ -245,7 +247,7 @@ await _testContext.RunOnDatabaseAsync(async dbContext =>
245247
""id"": """ + meeting.StringId + @""",
246248
""attributes"": {
247249
""title"": """ + meeting.Title + @""",
248-
""startTime"": """ + meeting.StartTime.ToString("O") + @""",
250+
""startTime"": """ + meeting.StartTime.ToString(JsonDateTimeOffsetFormatSpecifier) + @""",
249251
""duration"": """ + meeting.Duration + @""",
250252
""location"": {
251253
""lat"": " + meeting.Location.Latitude.ToString(CultureInfo.InvariantCulture) + @",
@@ -328,7 +330,7 @@ await _testContext.RunOnDatabaseAsync(async dbContext =>
328330
""id"": """ + attendee.Meeting.StringId + @""",
329331
""attributes"": {
330332
""title"": """ + attendee.Meeting.Title + @""",
331-
""startTime"": """ + attendee.Meeting.StartTime.ToString("O") + @""",
333+
""startTime"": """ + attendee.Meeting.StartTime.ToString(JsonDateTimeOffsetFormatSpecifier) + @""",
332334
""duration"": """ + attendee.Meeting.Duration + @""",
333335
""location"": {
334336
""lat"": " + attendee.Meeting.Location.Latitude.ToString(CultureInfo.InvariantCulture) + @",
@@ -575,7 +577,7 @@ public async Task Can_create_resource_with_side_effects()
575577
""id"": """ + newMeeting.StringId + @""",
576578
""attributes"": {
577579
""title"": """ + newMeeting.Title + @""",
578-
""startTime"": """ + newMeeting.StartTime.ToString("O") + @""",
580+
""startTime"": """ + newMeeting.StartTime.ToString(JsonDateTimeOffsetFormatSpecifier) + @""",
579581
""duration"": """ + newMeeting.Duration + @""",
580582
""location"": {
581583
""lat"": " + newMeeting.Location.Latitude.ToString(CultureInfo.InvariantCulture) + @",

test/TestBuildingBlocks/ObjectAssertionsExtensions.cs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
using System;
2+
using System.Text.Encodings.Web;
3+
using System.Text.Json;
24
using FluentAssertions;
35
using FluentAssertions.Numeric;
46
using FluentAssertions.Primitives;
57
using JetBrains.Annotations;
6-
using Newtonsoft.Json;
7-
using Newtonsoft.Json.Linq;
88

99
namespace TestBuildingBlocks
1010
{
@@ -13,9 +13,10 @@ public static class ObjectAssertionsExtensions
1313
{
1414
private const decimal NumericPrecision = 0.00000000001M;
1515

16-
private static readonly JsonSerializerSettings DeserializationSettings = new()
16+
private static readonly JsonSerializerOptions SerializerOptions = new()
1717
{
18-
Formatting = Formatting.Indented
18+
WriteIndented = true,
19+
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping
1920
};
2021

2122
/// <summary>
@@ -69,11 +70,11 @@ public static AndConstraint<NullableNumericAssertions<decimal>> BeApproximately(
6970
[CustomAssertion]
7071
public static void BeJson(this StringAssertions source, string expected, string because = "", params object[] becauseArgs)
7172
{
72-
var sourceToken = JsonConvert.DeserializeObject<JToken>(source.Subject, DeserializationSettings);
73-
var expectedToken = JsonConvert.DeserializeObject<JToken>(expected, DeserializationSettings);
73+
var sourceToken = JsonSerializer.Deserialize<JsonElement>(source.Subject, SerializerOptions);
74+
var expectedToken = JsonSerializer.Deserialize<JsonElement>(expected, SerializerOptions);
7475

75-
string sourceText = sourceToken?.ToString();
76-
string expectedText = expectedToken?.ToString();
76+
string sourceText = sourceToken.ToString();
77+
string expectedText = expectedToken.ToString();
7778

7879
sourceText.Should().Be(expectedText, because, becauseArgs);
7980
}

0 commit comments

Comments
 (0)