1
1
using System ;
2
2
using System . IO ;
3
+ using JetBrains . Annotations ;
3
4
using Newtonsoft . Json ;
4
5
using Newtonsoft . Json . Serialization ;
5
6
using RestSharp . Serialization ;
6
7
7
- namespace RestSharp . Serializers . NewtonsoftJson
8
- {
9
- public class JsonNetSerializer : IRestSerializer
10
- {
8
+ namespace RestSharp . Serializers . NewtonsoftJson {
9
+ public class JsonNetSerializer : IRestSerializer {
11
10
/// <summary>
12
11
/// Default serialization settings:
13
12
/// - Camel-case contract resolver
@@ -16,8 +15,7 @@ public class JsonNetSerializer : IRestSerializer
16
15
/// - Non-indented formatting
17
16
/// - Allow using non-public constructors
18
17
/// </summary>
19
- public static readonly JsonSerializerSettings DefaultSettings = new JsonSerializerSettings
20
- {
18
+ public static readonly JsonSerializerSettings DefaultSettings = new ( ) {
21
19
ContractResolver = new CamelCasePropertyNamesContractResolver ( ) ,
22
20
DefaultValueHandling = DefaultValueHandling . Include ,
23
21
TypeNameHandling = TypeNameHandling . None ,
@@ -26,7 +24,7 @@ public class JsonNetSerializer : IRestSerializer
26
24
ConstructorHandling = ConstructorHandling . AllowNonPublicDefaultConstructor
27
25
} ;
28
26
29
- [ ThreadStatic ] static WriterBuffer tWriterBuffer ;
27
+ [ ThreadStatic ] static WriterBuffer ? tWriterBuffer ;
30
28
31
29
readonly JsonSerializer _serializer ;
32
30
@@ -41,31 +39,30 @@ public class JsonNetSerializer : IRestSerializer
41
39
/// <param name="settings">Json.Net serializer settings</param>
42
40
public JsonNetSerializer ( JsonSerializerSettings settings ) => _serializer = JsonSerializer . Create ( settings ) ;
43
41
44
- public string Serialize ( object obj )
45
- {
42
+ public string ? Serialize ( object ? obj ) {
43
+ if ( obj == null ) return null ;
44
+
46
45
using var writerBuffer = tWriterBuffer ??= new WriterBuffer ( _serializer ) ;
47
46
48
47
_serializer . Serialize ( writerBuffer . GetJsonTextWriter ( ) , obj , obj . GetType ( ) ) ;
49
48
50
49
return writerBuffer . GetStringWriter ( ) . ToString ( ) ;
51
50
}
52
51
53
- public string Serialize ( Parameter bodyParameter ) => Serialize ( bodyParameter . Value ) ;
52
+ public string ? Serialize ( Parameter bodyParameter ) => Serialize ( bodyParameter . Value ) ;
54
53
55
- public T Deserialize < T > ( IRestResponse response )
56
- {
57
- using var reader = new JsonTextReader ( new StringReader ( response . Content ) ) { CloseInput = true } ;
54
+ public T ? Deserialize < T > ( IRestResponse response ) {
55
+ using var reader = new JsonTextReader ( new StringReader ( response . Content ) ) { CloseInput = true } ;
58
56
59
57
return _serializer . Deserialize < T > ( reader ) ;
60
58
}
61
59
62
- public string [ ] SupportedContentTypes { get ; } =
63
- {
60
+ public string [ ] SupportedContentTypes { get ; } = {
64
61
"application/json" , "text/json" , "text/x-json" , "text/javascript" , "*+json"
65
62
} ;
66
63
67
64
public string ContentType { get ; set ; } = "application/json" ;
68
65
69
- public DataFormat DataFormat { get ; } = DataFormat . Json ;
66
+ public DataFormat DataFormat => DataFormat . Json ;
70
67
}
71
- }
68
+ }
0 commit comments