Serialization¶
Run the complete page example.
Your app works with C# objects, but a service sends and receives data in a format such as JSON or XML. Refit uses a serializer to turn your objects into request bodies and turn reply bodies back into values your app can use.
Refit selects one content serializer through RefitSettings.ContentSerializer. The default is
SystemTextJsonContentSerializer, which writes typed request bodies and reads typed replies.
Every serializer implements IHttpContentSerializer. A serializer can also implement
ISynchronousContentSerializer for buffered or streamed request bodies, or
IStreamingContentSerializer for incremental IAsyncEnumerable<T> replies. These capabilities
are optional, so a custom serializer only needs to support the work its client requires.
Start with System.Text.Json and generated metadata for a new client. Generated JSON metadata keeps the model contract available for trimming and Native AOT. Use the other pages when the service or an existing app requires a different serializer or content format.
| Page | Use it for |
|---|---|
| System.Text.Json | The default JSON serializer, generated metadata, context reuse and combining registrations. |
| Newtonsoft.Json | The Refit.Newtonsoft.Json package, existing JSON converters and serialization rules. |
| XML | The Refit.Xml package, XML models, namespaces and reader/writer configuration. |
| Content serializers | JSON Lines, streaming response formats, synchronous capabilities and custom serializers. |
The JSON and content pages include complete API tables with links to the implementation and examples. The request bodies and streaming replies pages explain how those serializer capabilities fit into a Refit interface method.