Skip to content

Fast.Serialization.Newtonsoft.Json

Public members, parameters, returns, and exceptions

Targets: net8.0;net9.0;net10.0.

Contract and constraints

Newtonsoft.Json integration in the Fast.Serialization namespace. Service collection registration initializes shared tool options; MVC builder registration enables the MVC Newtonsoft formatter. Calling only the service extension does not switch MVC. The callback currently affects static tool options, so configure MVC overrides separately through MVC options. Minimal API System.Text.Json is not changed. ToJsonString, ToObject and DeepCopy use JsonContext.SerializerOptions. A copy retains only data allowed by serializer settings, converters and attributes. Invalid JSON and null-input errors follow the extension and Newtonsoft.Json contracts; there is no guarantee of a fresh object on failure. Do not reference both Fast.Serialization implementations together: their namespaces and public names overlap.

Installation and examples

Install the package

bash
dotnet add package Fast.Serialization.Newtonsoft.Json

Example 1

csharp
using Fast.Serialization;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Builder;

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddSerialization();
builder.Services.AddControllers().AddSerialization();

var app = builder.Build();
app.MapControllers();
app.Run();

Example 2

csharp
using Fast.Serialization;

public sealed class ItemSnapshot
{
    public long Id { get; set; }
    public string Name { get; set; } = "";
}

public sealed class SnapshotCodec
{
    public string Encode(ItemSnapshot item) => item.ToJsonString();
    public ItemSnapshot Decode(string json) => json.ToObject<ItemSnapshot>();
    public ItemSnapshot Copy(ItemSnapshot item) => item.DeepCopy();
}

Web-host examples belong in a consumer project using Microsoft.NET.Sdk.Web and a supported .NET target. IaaS examples can be used in an ordinary class library. Registration precedes builder.Build(); configure middleware and endpoints before app.Run(). Samples retain their original code and comments. No host, external service, database operation or client generator was run, and these snippets have not been compiled in this update.

Source reference

Decimal precision

Default decimal/nullable-decimal output keeps the decimal value without conversion through double. Explicit Places still applies the existing Math.Round policy; null remains null. JavaScript number precision is a separate consumer constraint and may require a suitable high-precision representation.