Skip to content

3 Serialization

TimCodes.NET edited this page Dec 1, 2023 · 1 revision

Request Serialization

Requests are serialized with an IApiRequestSerializer. They take an IApiRequest and convert it to the type needed to communicate with an API. When dealing with HTTP, that's an HttpRequestMessage. There are two levels where you can choose the serializer, one as a DefaultRequestSerlializer on your implementation of the IApiClient, the other is to set the serializer against the IApiRequest. If you give a serializer to an IApiRequest directly, it will override the DefaultRequestSerlializer specified on the client.

Serializers

A few serializers are included for you out-the-box:

EmptyApiRequestSerializer

Does nothing whatsoever with your request

JsonHttpApiRequestSerializer

Serializes objects into an application/json request with camel casing.

FormHttpApiRequestSerializer

Serializes objects to a URL encoded form request. Use the HttpFormNameAttribute to specify how property names should be converted to the form encoding. Alternatively, you can pass a string dictionary as the payload.

TextHttpApiRequestSerializer

Serializes objects into a text/plain request by just calling ToString on your object.

Custom Serialization

You are welcome to implement your own serialization by implementing IApiRequestSerializer. The HttpRequestMessage is available when the IApiRequest you receive in Serialize is of type HttpApiRequestBase.

Response Deserialization

Responses are deserialized by an IApiResponseDeserializer. They take the API response and convert it to some usable POCO IApiResponse. These are assigned to the matched ApiResponseVariation (more on those in following pages - essentially they describe an expected response format from the API server). You can set a default in your client by assigning to DefaultResponeDeserlializer.

Deserializers

The following deserializers are provided out-the-box:

JsonHttpApiResponseDeserializer

Deserializes an application/json response into the expected type.

ByteArrayHttpApiResponseDeserializer

Deserializes a byte array response.

DotNetBadRequestHttpApiResponseDeserializer

Deserializes the response you get from a .NET API when you have validation issues in your model. Returns a DotNetValidationProblem.

EmptyHttpApiResponseDeserializer

Does nothing with the response.

StreamHttpApiResponseDeserializer

Maintains the incoming stream from the API for the caller to process themselves.

TextHttpApiResponseDeserializer

Converts a text/plain response to a string return.

Custom Deserializing

To implement your own HTTP deserializing, inherit from HttpApiResponseDeserializerBase and implement the DeserializeAsync method. You can then assign or deserializer to the client or the response variation.