44using System . Text . Encodings . Web ;
55using System . Text . Json ;
66using System . Text . Json . Serialization ;
7+ using JsonApiDotNetCore . Configuration ;
8+ using JsonApiDotNetCore . Resources ;
9+ using JsonApiDotNetCore . Resources . Annotations ;
710using Microsoft . Extensions . Logging ;
811
912namespace JsonApiDotNetCore . Middleware ;
@@ -14,8 +17,69 @@ internal abstract class TraceLogWriter
1417 {
1518 WriteIndented = true ,
1619 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+ }
1829 } ;
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+ }
1983}
2084
2185internal sealed class TraceLogWriter < T > : TraceLogWriter
@@ -139,7 +203,7 @@ private static string SerializeObject(object value)
139203 {
140204 return JsonSerializer . Serialize ( value , SerializerOptions ) ;
141205 }
142- catch ( JsonException )
206+ catch ( Exception exception ) when ( exception is JsonException or NotSupportedException )
143207 {
144208 // Never crash as a result of logging, this is best-effort only.
145209 return "object" ;
0 commit comments