Skip to content

JsonSerializer

JsonSerializer adapts Newtonsoft.Json.JsonSerializer to RestSharp's ISerializer contract, so JsonHttpClient can control date-time-zone handling and other Newtonsoft settings while still plugging into RestSharp's request-body serialization pipeline. It is adapted from the Apache-2.0 RestSharp default serializer, with attribution retained in the source file header.

Purpose

  • Serialize request bodies to indented JSON with a configurable DateTimeZoneHandling, keeping serialization consistent with response deserialization (BR-CLI-011). See Serialize (JsonSerializer).
  • Satisfy the RestSharp.Serializers.ISerializer contract so JsonHttpClient can assign it to RestRequest.JsonSerializer.

Context

  • Namespace: SAIF.Web.API.Clients
  • Assembly/package: SAIF.Web.API.Clients (NuGet package SAIF.Web.API.Clients)

Public members

Member Signature Purpose
Constructor JsonSerializer(DateTimeZoneHandling dateTimeZoneHandling = DateTimeZoneHandling.Local) Builds an internal Newtonsoft.Json.JsonSerializer with MissingMemberHandling.Ignore, NullValueHandling.Include, DefaultValueHandling.Include, and the given date-time-zone handling.
Constructor JsonSerializer(Newtonsoft.Json.JsonSerializer serializer) Accepts a fully caller-configured Newtonsoft serializer, overriding all default settings from the other constructor.
Serialize string Serialize(object obj) Serializes obj to indented JSON (Formatting.Indented, " quote char) and returns the resulting string.
ContentType string { get; set; } Set to application/json by both constructors; part of the ISerializer contract RestSharp reads when writing the request.
DateFormat string { get; set; } Declared for ISerializer compliance; documented in-source as unused for JSON serialization.
RootElement string { get; set; } Declared for ISerializer compliance; documented in-source as unused for JSON serialization.
Namespace string { get; set; } Declared for ISerializer compliance; documented in-source as unused for JSON serialization.

Behavioral notes

  • Payloads are always pretty-printed (Formatting.Indented) on the wire, producing larger request bodies than compact serialization would.
  • The two-constructor design means callers can either accept the library's fixed Newtonsoft settings (with a chosen date-time-zone mode) or fully replace them by supplying their own Newtonsoft.Json.JsonSerializer.

Constraints and usage notes

  • DateFormat, RootElement, and Namespace exist only to satisfy the ISerializer interface signature and have no effect on JSON output.

Sources

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