1
+ // Copyright © 2009-2021 John Sheehan, Andrew Young, Alexey Zimarev and RestSharp community
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+
1
15
using Newtonsoft . Json . Serialization ;
2
16
3
- namespace RestSharp . Serializers . NewtonsoftJson ;
17
+ namespace RestSharp . Serializers . NewtonsoftJson ;
4
18
5
- public class JsonNetSerializer : IRestSerializer {
19
+ public class JsonNetSerializer : IRestSerializer , ISerializer , IDeserializer {
6
20
/// <summary>
7
21
/// Default serialization settings:
8
22
/// - Camel-case contract resolver
@@ -37,7 +51,7 @@ public class JsonNetSerializer : IRestSerializer {
37
51
38
52
public string ? Serialize ( object ? obj ) {
39
53
if ( obj == null ) return null ;
40
-
54
+
41
55
using var writerBuffer = _writerBuffer ??= new WriterBuffer ( _serializer ) ;
42
56
43
57
_serializer . Serialize ( writerBuffer . GetJsonTextWriter ( ) , obj , obj . GetType ( ) ) ;
@@ -50,11 +64,15 @@ public class JsonNetSerializer : IRestSerializer {
50
64
public T ? Deserialize < T > ( RestResponse response ) {
51
65
if ( response . Content == null )
52
66
throw new DeserializationException ( response , new InvalidOperationException ( "Response content is null" ) ) ;
67
+
53
68
using var reader = new JsonTextReader ( new StringReader ( response . Content ) ) { CloseInput = true } ;
54
69
55
70
return _serializer . Deserialize < T > ( reader ) ;
56
71
}
57
72
73
+ public ISerializer Serializer => this ;
74
+ public IDeserializer Deserializer => this ;
75
+
58
76
public string [ ] SupportedContentTypes { get ; } = {
59
77
"application/json" , "text/json" , "text/x-json" , "text/javascript" , "*+json"
60
78
} ;
0 commit comments