Skip to content

Commit eec84f6

Browse files
committed
Fixes #2164
1 parent 873ecde commit eec84f6

File tree

1 file changed

+12
-13
lines changed

1 file changed

+12
-13
lines changed

docs/docs/advanced/serialization.md

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -126,24 +126,23 @@ Here is an example of a custom serializer that uses `System.Text.Json`:
126126

127127
```csharp
128128
public class SimpleJsonSerializer : IRestSerializer {
129-
public string Serialize(object obj) => JsonSerializer.Serialize(obj);
129+
public string? Serialize(object? obj) => obj == null ? null : JsonSerializer.Serialize(obj);
130130

131-
public string Serialize(Parameter bodyParameter) => Serialize(bodyParameter.Value);
131+
public string? Serialize(Parameter bodyParameter) => Serialize(bodyParameter.Value);
132132

133-
public T Deserialize<T>(IRestResponse response) => JsonSerializer.Deserialize<T>(response.Content);
133+
public T? Deserialize<T>(RestResponse response) => JsonSerializer.Deserialize<T>(response.Content!);
134134

135-
public string[] SupportedContentTypes { get; } = {
136-
"application/json", "text/json", "text/x-json", "text/javascript", "*+json"
137-
};
135+
public ContentType ContentType { get; set; } = ContentType.Json;
138136

139-
public string ContentType { get; set; } = "application/json";
140-
141-
public DataFormat DataFormat { get; } = DataFormat.Json;
137+
public ISerializer Serializer => this;
138+
public IDeserializer Deserializer => this;
139+
public DataFormat DataFormat => DataFormat.Json;
140+
public string[] AcceptedContentTypes => ContentType.JsonAccept;
141+
public SupportsContentType SupportsContentType
142+
=> contentType => contentType.Value.EndsWith("json", StringComparison.InvariantCultureIgnoreCase);
142143
}
143144
```
144145

145-
The value of the `SupportedContentTypes` property will be used to match the
146-
serializer with the response `Content-Type` headers.
146+
The `SupportedContentTypes` function will be used to check if the serializer is able to deserialize the response based on the `Content-Type` response header.
147147

148-
The `ContentType` property will be used when making a request so the
149-
server knows how to handle the payload.
148+
The `ContentType` property will be used when making a request so the server knows how to handle the payload.

0 commit comments

Comments
 (0)