Sparcpoint.Extensions.Json 1.0.2

dotnet add package Sparcpoint.Extensions.Json --version 1.0.2
                    
NuGet\Install-Package Sparcpoint.Extensions.Json -Version 1.0.2
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Sparcpoint.Extensions.Json" Version="1.0.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Sparcpoint.Extensions.Json" Version="1.0.2" />
                    
Directory.Packages.props
<PackageReference Include="Sparcpoint.Extensions.Json" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Sparcpoint.Extensions.Json --version 1.0.2
                    
#r "nuget: Sparcpoint.Extensions.Json, 1.0.2"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Sparcpoint.Extensions.Json@1.0.2
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Sparcpoint.Extensions.Json&version=1.0.2
                    
Install as a Cake Addin
#tool nuget:?package=Sparcpoint.Extensions.Json&version=1.0.2
                    
Install as a Cake Tool

Sparcpoint.Extensions.Json

A Fluent API for customizing JSON serialization in .NET using System.Text.Json.

This library provides a fluent, code-based alternative to using attributes (like [JsonPropertyName], [JsonIgnore], etc.). It allows you to configure how types are serialized externally—without modifying the source code of those types.

Ideal for:

  • Sharing the same model across multiple serialization contexts.
  • Avoiding attribute clutter.
  • Dynamically applying serialization rules at runtime.

📦 Installation

Install via NuGet:

dotnet add package Sparcpoint.Extensions.Json

🚀 Getting Started

You configure serialization rules using the fluent JsonEntityBuilder<T> API. These configurations can be applied to JsonSerializerOptions directly.

using System.Text.Json;

var options = new JsonSerializerOptions()
    .Configure<Person>(entity => entity
        .Property(p => p.FirstName).Name("first_name")
        .Property(p => p.LastName).Ignore()
        .Property(p => p.Age).Required()
    );

var json = JsonSerializer.Serialize(new Person
{
    FirstName = "John",
    LastName = "Doe",
    Age = 42
}, options);

Console.WriteLine(json);
// Output: {"first_name":"John","Age":42}

🧱 Fluent API Overview

JsonEntityBuilder<TEntity>

Represents configuration for a single entity type. Used internally by the .Configure<T>().

You typically won’t instantiate this directly—instead, you pass a lambda to configure it:

options.Configure<User>(entity => {
    entity.Property(u => u.Email).Ignore();
});

Property()

Selects a property on the entity to configure.

entity.Property(u => u.Email)

This returns a JsonMemberBuilder<TEntity, TProperty> for chaining member-level configuration.


Member Configuration Methods

Method Description
.Name(string name) Changes the JSON property name.
.Ignore() Excludes the property from serialization.
.Order(int order) Sets the order of the property in serialized JSON.
.Required() Marks the property as required.
.NumberHandling(JsonNumberHandling? handling) Applies a custom number handling rule.
.ObjectCreationHandling(JsonObjectCreationHandling? handling) Controls object creation behavior.
.CustomConverter(JsonConverter converter) Assigns a custom converter for this property.

Example:

entity.Property(u => u.Id)
      .Order(1)
      .Required()
      .Name("user_id");

⚙️ Applying Configuration to JsonSerializerOptions

The extension method is provided for integrating your configurations:

.Configure<T>()

Adds the configuration at the front of the TypeInfoResolverChain. This means it takes precedence over existing resolvers.

NOTE: It will always ensure that at least one DefaultJsonTypeInfoResolver is at the end of the chain.

options.Configure<MyModel>(builder => {
    builder.Property(m => m.Secret).Ignore();
});

Both methods support an optional parameter to control inheritance:

.Configure<MyModel>(builder => { ... }, includeInheritedTypes: true)

🧩 Example: Combining Multiple Rules

You can chain multiple configuration calls for clarity:

options
    .Configure<Order>(entity => entity
        .Property(o => o.OrderId).Name("id")
        .Property(o => o.CustomerName).Name("customer")
        .Property(o => o.InternalNotes).Ignore()
    )
    .Configure<Customer>(entity => entity
        .Property(c => c.Email).Required()
    );

🧠 Why Fluent Configuration?

This API helps you:

  • Keep DTOs free of serialization attributes.
  • Apply serialization behavior conditionally (e.g., per environment or feature flag).
  • Centralize serialization configuration in one place.
  • Modify serialization for third-party or auto-generated classes without source changes.

🧩 Notes and Constraints

  • Only classes and structs can be configured (no primitives, enums, arrays, or collections).
  • System types (in System.* namespaces) are excluded by default.
  • Configurations are applied via IJsonTypeInfoResolver modifiers.

🧪 Example: Dynamic Behavior

if (Environment.GetEnvironmentVariable("APP_ENV") == "Production")
{
    options.Configure<User>(b =>
        b.Property(u => u.Password).Ignore()
    );
}

🧾 License

MIT License © Sparcpoint

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Sparcpoint.Extensions.Json:

Package Downloads
Sparcpoint.Oscal.Json

JSON Implementation for OSCAL Models (Loader and Serialization Options). Includes schema validation

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.2 261 11/6/2025
1.0.1 234 11/3/2025
1.0.0 229 11/3/2025