@@ -17,13 +17,27 @@ public class JsonUtils
1717 Encoder = JavaScriptEncoder . UnsafeRelaxedJsonEscaping
1818 } ;
1919
20+ private static readonly JsonSerializerOptions _defaultSerializeNoIndentedOptions = new ( )
21+ {
22+ WriteIndented = false ,
23+ DefaultIgnoreCondition = JsonIgnoreCondition . WhenWritingNull ,
24+ Encoder = JavaScriptEncoder . UnsafeRelaxedJsonEscaping
25+ } ;
26+
2027 private static readonly JsonSerializerOptions _nullValueSerializeOptions = new ( )
2128 {
2229 WriteIndented = true ,
2330 DefaultIgnoreCondition = JsonIgnoreCondition . Never ,
2431 Encoder = JavaScriptEncoder . UnsafeRelaxedJsonEscaping
2532 } ;
2633
34+ private static readonly JsonSerializerOptions _nullValueSerializeNoIndentedOptions = new ( )
35+ {
36+ WriteIndented = false ,
37+ DefaultIgnoreCondition = JsonIgnoreCondition . Never ,
38+ Encoder = JavaScriptEncoder . UnsafeRelaxedJsonEscaping
39+ } ;
40+
2741 private static readonly JsonDocumentOptions _defaultDocumentOptions = new ( )
2842 {
2943 CommentHandling = JsonCommentHandling . Skip
@@ -104,7 +118,13 @@ public static string Serialize(object? obj, bool indented = true, bool nullValue
104118 {
105119 return result ;
106120 }
107- var options = nullValue ? _nullValueSerializeOptions : _defaultSerializeOptions ;
121+ var options = ( nullValue , indented ) switch
122+ {
123+ ( true , true ) => _nullValueSerializeOptions ,
124+ ( true , false ) => _nullValueSerializeNoIndentedOptions ,
125+ ( false , true ) => _defaultSerializeOptions ,
126+ _ => _defaultSerializeNoIndentedOptions
127+ } ;
108128 result = JsonSerializer . Serialize ( obj , options ) ;
109129 }
110130 catch ( Exception ex )
0 commit comments