@@ -13,9 +13,9 @@ public static class ObjectAssertionsExtensions
13
13
{
14
14
private const decimal NumericPrecision = 0.00000000001M ;
15
15
16
- private static readonly JsonSerializerOptions SerializerOptions = new ( )
16
+ private static readonly JsonWriterOptions JsonWriterOptions = new ( )
17
17
{
18
- WriteIndented = true ,
18
+ Indented = true ,
19
19
Encoder = JavaScriptEncoder . UnsafeRelaxedJsonEscaping
20
20
} ;
21
21
@@ -70,13 +70,23 @@ public static AndConstraint<NullableNumericAssertions<decimal>> BeApproximately(
70
70
[ CustomAssertion ]
71
71
public static void BeJson ( this StringAssertions source , string expected , string because = "" , params object [ ] becauseArgs )
72
72
{
73
- var sourceToken = JsonSerializer . Deserialize < JsonElement > ( source . Subject , SerializerOptions ) ;
74
- var expectedToken = JsonSerializer . Deserialize < JsonElement > ( expected , SerializerOptions ) ;
73
+ using JsonDocument sourceJson = JsonDocument . Parse ( source . Subject ) ;
74
+ using JsonDocument expectedJson = JsonDocument . Parse ( expected ) ;
75
75
76
- string sourceText = sourceToken . ToString ( ) ;
77
- string expectedText = expectedToken . ToString ( ) ;
76
+ string sourceText = ToJsonString ( sourceJson ) ;
77
+ string expectedText = ToJsonString ( expectedJson ) ;
78
78
79
79
sourceText . Should ( ) . Be ( expectedText , because , becauseArgs ) ;
80
80
}
81
+
82
+ private static string ToJsonString ( JsonDocument document )
83
+ {
84
+ using var stream = new MemoryStream ( ) ;
85
+ var writer = new Utf8JsonWriter ( stream , JsonWriterOptions ) ;
86
+
87
+ document . WriteTo ( writer ) ;
88
+ writer . Flush ( ) ;
89
+ return Encoding . UTF8 . GetString ( stream . ToArray ( ) ) ;
90
+ }
81
91
}
82
92
}
0 commit comments