88using System ;
99using System . Text . Json ;
1010using System . Text . Json . Nodes ;
11+ using Microsoft . Extensions . Logging ;
1112
1213namespace com . IvanMurzak . ReflectorNet
1314{
1415 public static class ExtensionsJson
1516 {
16- public static JsonElement ToJsonElement ( this object data , Reflector ? reflector , JsonSerializerOptions ? options = null )
17- => JsonSerializer . SerializeToElement ( data , options ?? reflector ? . JsonSerializerOptions ) ;
17+ public static JsonElement ToJsonElement ( this object data , Reflector ? reflector , JsonSerializerOptions ? options = null , ILogger ? logger = null )
18+ {
19+ if ( logger ? . IsEnabled ( LogLevel . Trace ) == true )
20+ logger . LogTrace ( "Converting object of type {Type} to JsonElement." ,
21+ data ? . GetType ( ) . GetTypeId ( ) . ValueOrNull ( ) ) ;
22+
23+ return JsonSerializer . SerializeToElement ( data , options ?? reflector ? . JsonSerializerOptions ) ;
24+ }
1825
1926 public static JsonElement ? ToJsonElement ( this JsonNode ? node )
2027 {
@@ -29,31 +36,31 @@ public static JsonElement ToJsonElement(this object data, Reflector? reflector,
2936 return document . RootElement . Clone ( ) ;
3037 }
3138
32- public static string ToJson ( this object ? value , Reflector ? reflector , JsonSerializerOptions ? options = null )
33- => ToJson (
39+ public static string ToJson ( this object ? value , Reflector ? reflector , JsonSerializerOptions ? options = null , ILogger ? logger = null )
40+ {
41+ return ToJson (
3442 value : value ,
3543 defaultValue : Utils . JsonSerializer . EmptyJsonObject , // Use empty JSON object as default value
3644 reflector : reflector ,
37- options : options ) ;
38-
39- public static string ToJsonOrEmptyJsonObject ( this object ? value , Reflector ? reflector , JsonSerializerOptions ? options = null )
40- => ToJson (
41- value : value ,
42- defaultValue : Utils . JsonSerializer . EmptyJsonObject ,
43- reflector : reflector ,
44- options : options ) ;
45+ options : options ,
46+ logger : logger ) ;
47+ }
4548
46- public static string ToJson ( this object ? value , string defaultValue , Reflector ? reflector , JsonSerializerOptions ? options = null )
49+ public static string ToJson ( this object ? value , string defaultValue , Reflector ? reflector , JsonSerializerOptions ? options = null , ILogger ? logger = null )
4750 {
4851 if ( value == null )
4952 return defaultValue ;
5053
5154 if ( value is Utils . JsonSerializer )
5255 throw new ArgumentException ( "Cannot serialize JsonSerializer instance." , nameof ( value ) ) ;
5356
57+ if ( logger ? . IsEnabled ( LogLevel . Trace ) == true )
58+ logger . LogTrace ( "Serializing object of type {Type} to JSON string." ,
59+ value . GetType ( ) . GetTypeId ( ) . ValueOrNull ( ) ) ;
60+
5461 return JsonSerializer . Serialize (
5562 value : value ,
56- options : options ?? reflector ? . JsonSerializerOptions ?? new JsonSerializerOptions ( ) ) ;
63+ options : options ?? reflector ? . JsonSerializerOptions ) ;
5764 }
5865 }
5966}
0 commit comments