Interface IHttpContentDeserializer
- Namespace
- Kampute.HttpClient.Interfaces
- Assembly
- Kampute.HttpClient.dll
Defines the functionality for deserializing an object from the HTTP request body.
public interface IHttpContentDeserializer
Remarks
The IHttpContentDeserializer interface is designed for the purpose of abstracting the mechanism of deserializing data from HTTP responses into .NET objects. Implementers of this interface provide the logic necessary to convert HTTP content, identified by a specific media type, into instances of model types used within an application.
The GetSupportedMediaTypes(Type) method is designed to communicate the content types that the deserializer can process, effectively informing the server about the media types acceptable to the client. It ensures that the client and server can agree on a common format for data exchange, enhancing the efficiency and compatibility of HTTP communications.
Through the CanDeserialize(string, Type) method, implementations can provide a quick check to ascertain compatibility between the deserializer, the media type of the content, and the target model type. This check is typically performed before attempting deserialization to ensure that the deserializer is capable of processing the content as expected.
The asynchronous DeserializeAsync(HttpContent, Type, CancellationToken) method forms the core of the interface, where the actual deserialization logic is implemented. This method takes HttpContent, along with the target model type, and returns a task that, when completed, yields the deserialized object. Implementations must handle the asynchronous nature of this operation, catering to potential cancellation requests through the provided CancellationToken provided by the parameter.
The implementations of IHttpContentDeserializer should be thread-safe and reusable across multiple deserialization operations to facilitate efficient processing of HTTP response content in a concurrent environment.
Methods
CanDeserialize(string, Type)
Determines whether this deserializer can handle data of a specific content type and deserialize it into the specified model type.
bool CanDeserialize(string mediaType, Type modelType)
Parameters
mediaType
stringThe media type of the content.
modelType
TypeThe type of the model to be deserialized.
Returns
- bool
true if this deserializer can handle the specified content type and model type; otherwise, false.
DeserializeAsync(HttpContent, Type, CancellationToken)
Asynchronously deserializes an object from the provided HttpContent.
Task<object?> DeserializeAsync(HttpContent content, Type modelType, CancellationToken cancellationToken = default)
Parameters
content
HttpContentThe HttpContent from which to deserialize the data.
modelType
TypeThe type of the object to be deserialized.
cancellationToken
CancellationTokenA token for canceling the operation (optional).
Returns
- Task<object>
A task representing the asynchronous deserialization operation. Contains the deserialized object.
Exceptions
- ArgumentNullException
Thrown if
content
ormodelType
is null.
GetSupportedMediaTypes(Type)
Retrieves a collection of supported media types for a specific model type.
IEnumerable<string> GetSupportedMediaTypes(Type modelType)
Parameters
modelType
TypeThe type of the model for which to retrieve supported media types.
Returns
- IEnumerable<string>
An enumerable of strings representing the media types supported for the specified model type.