Skip to content

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 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).
  2. The two-argument constructor overload replaces all of these settings with a caller-supplied Newtonsoft.Json.JsonSerializer instead.
  3. Open a StringWriter → JsonTextWriter with Formatting.Indented and QuoteChar = '"'.
  4. Payloads are pretty-printed on the wire, producing larger request bodies than a compact serialization would.
  5. _serializer.Serialize(jsonTextWriter, obj) and return the accumulated string (BR-CLI-011).
  6. On failure: serialization exceptions propagate uncaught to the caller.

Process flow

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.

Sources

  • src/SAIF.Web.API.Clients/JsonSerializer.cs