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
<PackageReference Include="Sparcpoint.Extensions.Json" Version="1.0.2" />
<PackageVersion Include="Sparcpoint.Extensions.Json" Version="1.0.2" />
<PackageReference Include="Sparcpoint.Extensions.Json" />
paket add Sparcpoint.Extensions.Json --version 1.0.2
#r "nuget: Sparcpoint.Extensions.Json, 1.0.2"
#:package Sparcpoint.Extensions.Json@1.0.2
#addin nuget:?package=Sparcpoint.Extensions.Json&version=1.0.2
#tool nuget:?package=Sparcpoint.Extensions.Json&version=1.0.2
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
DefaultJsonTypeInfoResolveris 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
IJsonTypeInfoResolvermodifiers.
🧪 Example: Dynamic Behavior
if (Environment.GetEnvironmentVariable("APP_ENV") == "Production")
{
options.Configure<User>(b =>
b.Property(u => u.Password).Ignore()
);
}
🧾 License
MIT License © Sparcpoint
| Product | Versions 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. |
-
.NETStandard 2.0
- System.Text.Json (>= 8.0.6)
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.