---
title: Serialize (JsonSerializer)
summary: Serialize a request body to indented JSON for the outbound request.
status: draft
owner: bendy
last_reviewed: 2026-09-15
library: SAIF.Web.API.Clients
reference_type: package
operation_id: OP-10
trigger: RestSharp invokes it when AddJsonBody content is written
---

# Serialize (JsonSerializer)

`JsonSerializer.Serialize` is the `RestSharp.Serializers.ISerializer` adapter method RestSharp calls to turn a request body object into the JSON text sent on the wire, using a fixed set of Newtonsoft.Json settings configured at construction.

| Field | Value |
| --- | --- |
| **Operation ID** | OP-10 |
| **Trigger** | RestSharp invokes it when `AddJsonBody` content is written ([GetJsonResponse](get-json-response.md) steps 5–6). |
| **Inputs** | `object obj` (the request body). |
| **Outputs** | The serialized JSON string. |
| **Primary components** | `SAIF.Web.API.Clients.JsonSerializer.Serialize(object)` |
| **Entry point** | `src/SAIF.Web.API.Clients/JsonSerializer.cs` |

## Steps

1. (Constructor) Set `ContentType = "application/json"` and build an internal Newtonsoft serializer with `MissingMemberHandling.Ignore`, `NullValueHandling.Include`, `DefaultValueHandling.Include`, and the caller's `DateTimeZoneHandling` ([BR-CLI-011](../business-rules.md#br-cli-011-date-time-zone-handling-consistency)).
   - The two-argument constructor overload replaces all of these settings with a caller-supplied `Newtonsoft.Json.JsonSerializer` instead.
2. Open a `StringWriter` → `JsonTextWriter` with `Formatting.Indented` and `QuoteChar = '"'`.
   - Payloads are pretty-printed on the wire, producing larger request bodies than a compact serialization would.
3. `_serializer.Serialize(jsonTextWriter, obj)` and return the accumulated string ([BR-CLI-011](../business-rules.md#br-cli-011-date-time-zone-handling-consistency)).
   - On failure: serialization exceptions propagate uncaught to the caller.

## Process flow

```mermaid
flowchart TD
	Start([Serialize called]) --> Writer["Open StringWriter /<br>JsonTextWriter,<br>Formatting.Indented"]
	Writer --> Serialize["Newtonsoft serializer<br>writes obj"]
	Serialize --> EndOk([Return JSON string])
```

## Failure and edge-case behavior
- Serialization exceptions (for example from a non-serializable object graph) propagate uncaught to the calling `RestRequest`/`GetJsonResponse` chain.

## Related documentation
- [Operations and processes](../operations-processes.md)
- [Business rules](../business-rules.md)

## Sources
- `src/SAIF.Web.API.Clients/JsonSerializer.cs`
