4
4
using System . Text . Encodings . Web ;
5
5
using System . Text . Json ;
6
6
using System . Text . Json . Serialization ;
7
+ using JsonApiDotNetCore . Configuration ;
8
+ using JsonApiDotNetCore . Resources ;
9
+ using JsonApiDotNetCore . Resources . Annotations ;
7
10
using Microsoft . Extensions . Logging ;
8
11
9
12
namespace JsonApiDotNetCore . Middleware ;
@@ -14,8 +17,69 @@ internal abstract class TraceLogWriter
14
17
{
15
18
WriteIndented = true ,
16
19
Encoder = JavaScriptEncoder . UnsafeRelaxedJsonEscaping ,
17
- ReferenceHandler = ReferenceHandler . Preserve
20
+ ReferenceHandler = ReferenceHandler . IgnoreCycles ,
21
+ DefaultIgnoreCondition = JsonIgnoreCondition . WhenWritingNull ,
22
+ Converters =
23
+ {
24
+ new JsonStringEnumConverter ( ) ,
25
+ new ResourceTypeInTraceJsonConverter ( ) ,
26
+ new ResourceFieldInTraceJsonConverterFactory ( ) ,
27
+ new IdentifiableInTraceJsonConverter ( )
28
+ }
18
29
} ;
30
+
31
+ private sealed class ResourceTypeInTraceJsonConverter : JsonConverter < ResourceType >
32
+ {
33
+ public override ResourceType Read ( ref Utf8JsonReader reader , Type typeToConvert , JsonSerializerOptions options )
34
+ {
35
+ throw new NotSupportedException ( ) ;
36
+ }
37
+
38
+ public override void Write ( Utf8JsonWriter writer , ResourceType value , JsonSerializerOptions options )
39
+ {
40
+ writer . WriteStringValue ( value . PublicName ) ;
41
+ }
42
+ }
43
+
44
+ private sealed class ResourceFieldInTraceJsonConverterFactory : JsonConverterFactory
45
+ {
46
+ public override bool CanConvert ( Type typeToConvert )
47
+ {
48
+ return typeToConvert . IsAssignableTo ( typeof ( ResourceFieldAttribute ) ) ;
49
+ }
50
+
51
+ public override JsonConverter CreateConverter ( Type typeToConvert , JsonSerializerOptions options )
52
+ {
53
+ return new ResourceFieldInTraceJsonConverter ( ) ;
54
+ }
55
+
56
+ private sealed class ResourceFieldInTraceJsonConverter : JsonConverter < ResourceFieldAttribute >
57
+ {
58
+ public override ResourceFieldAttribute Read ( ref Utf8JsonReader reader , Type typeToConvert , JsonSerializerOptions options )
59
+ {
60
+ throw new NotSupportedException ( ) ;
61
+ }
62
+
63
+ public override void Write ( Utf8JsonWriter writer , ResourceFieldAttribute value , JsonSerializerOptions options )
64
+ {
65
+ writer . WriteStringValue ( value . PublicName ) ;
66
+ }
67
+ }
68
+ }
69
+
70
+ private sealed class IdentifiableInTraceJsonConverter : JsonConverter < IIdentifiable >
71
+ {
72
+ public override IIdentifiable Read ( ref Utf8JsonReader reader , Type typeToConvert , JsonSerializerOptions options )
73
+ {
74
+ throw new NotSupportedException ( ) ;
75
+ }
76
+
77
+ public override void Write ( Utf8JsonWriter writer , IIdentifiable value , JsonSerializerOptions options )
78
+ {
79
+ Type runtimeType = value . GetType ( ) ;
80
+ JsonSerializer . Serialize ( writer , value , runtimeType , options ) ;
81
+ }
82
+ }
19
83
}
20
84
21
85
internal sealed class TraceLogWriter < T > : TraceLogWriter
@@ -139,7 +203,7 @@ private static string SerializeObject(object value)
139
203
{
140
204
return JsonSerializer . Serialize ( value , SerializerOptions ) ;
141
205
}
142
- catch ( JsonException )
206
+ catch ( Exception exception ) when ( exception is JsonException or NotSupportedException )
143
207
{
144
208
// Never crash as a result of logging, this is best-effort only.
145
209
return "object" ;
0 commit comments