Wiaoj.Serialization.Security
0.4.2-alpha.1
dotnet add package Wiaoj.Serialization.Security --version 0.4.2-alpha.1
NuGet\Install-Package Wiaoj.Serialization.Security -Version 0.4.2-alpha.1
<PackageReference Include="Wiaoj.Serialization.Security" Version="0.4.2-alpha.1" />
<PackageVersion Include="Wiaoj.Serialization.Security" Version="0.4.2-alpha.1" />
<PackageReference Include="Wiaoj.Serialization.Security" />
paket add Wiaoj.Serialization.Security --version 0.4.2-alpha.1
#r "nuget: Wiaoj.Serialization.Security, 0.4.2-alpha.1"
#:package Wiaoj.Serialization.Security@0.4.2-alpha.1
#addin nuget:?package=Wiaoj.Serialization.Security&version=0.4.2-alpha.1&prerelease
#tool nuget:?package=Wiaoj.Serialization.Security&version=0.4.2-alpha.1&prerelease
Wiaoj.Serialization
One serializer abstraction for .NET, with several formats and behaviours side by side in one application. Each configuration is registered under a key type: the API writes camel-case JSON, the cache writes compressed MessagePack, secrets are encrypted with a Wiaoj.Security key ring, and code asks for ISerializer<CacheKey> without knowing which library is behind it.
Packages
| Package | What it adds |
|---|---|
Wiaoj.Serialization.Abstractions |
ISerializer, ISerializer<TKey>, ISerializerKey, the exception types |
Wiaoj.Serialization.DependencyInjection |
AddWiaojSerializer, ISerializationBuilder, the default ISerializer |
Wiaoj.Serialization.SystemTextJson |
UseSystemTextJson: JSON, including IAsyncEnumerable streaming |
Wiaoj.Serialization.MessagePack |
UseMessagePack: compact binary, readable from other languages |
Wiaoj.Serialization.MemoryPack |
UseMemoryPack: compact binary between .NET applications (limits) |
Wiaoj.Serialization.Bson |
UseBson: MongoDB BSON documents |
Wiaoj.Serialization.YamlDotNet |
UseYamlDotNet: YAML |
Wiaoj.Serialization.Compression |
WithGzipCompression, WithBrotliCompression |
Wiaoj.Serialization.Security |
WithEncryption<TKey, TContext>: AES-GCM with a Wiaoj.Security key ring |
Wiaoj.Serialization.Transcoding |
ITranscoder: convert bytes from one registered format to another |
dotnet add package Wiaoj.Serialization.DependencyInjection
dotnet add package Wiaoj.Serialization.SystemTextJson # and any other format you need
Namespaces:
AddWiaojSerializeris inMicrosoft.Extensions.DependencyInjection.- The contracts and every registration method (
Use…,TryUse…,With…,AddTranscoding) are inWiaoj.Serialization. - Each package's own types are in its namespace, for example
Wiaoj.Serialization.Security.DecryptionFailedExceptionandWiaoj.Serialization.Transcoding.ITranscoder.
Quick start
1. Define keys
A key is a marker type that names one configuration.
using Wiaoj.Serialization;
public readonly struct ApiKey : ISerializerKey;
public readonly struct CacheKey : ISerializerKey;
2. Register
builder.Services.AddWiaojSerializer(serializers => {
// Keyless: resolved as the non-keyed ISerializer
serializers.UseSystemTextJson(new JsonSerializerOptions(JsonSerializerDefaults.Web));
serializers.UseSystemTextJson<ApiKey>();
serializers.UseMessagePack<CacheKey>()
.WithBrotliCompression(CompressionLevel.Fastest);
});
AddWiaojSerializer() without a callback returns the builder instead, for one or two calls.
3. Use
public sealed class ProductCache(ISerializer<CacheKey> serializer, IDistributedCache cache) {
public Task SetAsync(Product product, CancellationToken ct) =>
cache.SetAsync($"product:{product.Id}", serializer.Serialize(product), ct);
}
The non-keyed ISerializer is the keyless serializer when one is registered, otherwise the only registered serializer. With several keyed serializers and no keyless one there is none: GetService<ISerializer>() returns null.
Registering the same key twice throws.
TryUse…(every format) registers only when the key is free.Replace…(SystemTextJson and MemoryPack) swaps an existing registration.
Behaviours
With… methods wrap the serializer in a decorator, in the order they are called. Compressing and then encrypting runs as follows:
value → format serializer → compression → encryption → bytes
Compress before you encrypt: ciphertext does not compress.
Compression
serializers.UseSystemTextJson<LogKey>().WithGzipCompression();
serializers.UseMessagePack<CacheKey>().WithBrotliCompression(CompressionLevel.Fastest);
Data that does not decompress throws DeserializationFormatException.
Encryption with a Wiaoj.Security key ring
Encrypts everything a serializer writes with the ISecretProtector<TContext> of a Wiaoj.Security context. That gives authenticated AES-GCM, keys held in your key store under a master key, and rotation. Data written under an older key stays readable while the key ring still holds that key.
public readonly struct SecureDataKey : ISerializerKey;
public sealed class SecureDataContext : ISecretContext;
builder.Services.AddWiaojSecurity()
.AddEnvironmentMasterKey()
.AddEntityFrameworkKeyStore<AppDbContext>()
.AddManagedProtector<SecureDataContext>();
builder.Services.AddWiaojSerializer(serializers => serializers
.UseSystemTextJson<SecureDataKey>()
.WithEncryption<SecureDataKey, SecureDataContext>());
- Bytes are the key version (4 bytes, big-endian) followed by the ciphertext. Text is the
EncryptedSecretcompact form,v{version}.{blob}. - Anything that cannot be decrypted throws
DecryptionFailedException: data that was tampered with or truncated, written under another context, or written under a key the ring no longer holds. - Encryption is not deterministic. The same value gives different bytes each time, so encrypted output cannot be used as a cache key or compared for equality.
Transcoding
Convert bytes from one registered format to another, for example to re-encode cached JSON as MessagePack:
serializers.AddTranscoding().UseSystemTextJson<JsonKey>();
serializers.UseMessagePack<PackKey>();
byte[] pack = transcoder.From<JsonKey>(jsonBytes).To<PackKey, Order>();
Streaming a JSON array
SystemTextJsonSerializer also implements IAsyncEnumerableSerializer, which reads the items of a JSON array one at a time:
if(serializer is IAsyncEnumerableSerializer streaming) {
await foreach(Product? product in streaming.DeserializeAsyncEnumerable<Product>(stream, ct)) {
// ...
}
}
The other formats, and a JSON serializer wrapped in compression or encryption, do not implement it.
Formats
YAML and BSON
serializers.UseYamlDotNet<ConfigKey>(yaml => yaml.WithNamingConvention(CamelCaseNamingConvention.Instance));
serializers.UseBson<MongoKey>();
BSON needs a representation configured before it can write a Guid, and it writes a DateTimeOffset as an array. If your types use either, configure MongoDB's serializers for them.
MemoryPack
UseMemoryPack<TKey>() writes a compact binary format with very little work per value, but it only suits data exchanged between .NET applications:
- Every type it serializes must be
[MemoryPackable]andpartial(a source generator writes the serializer). - By default, members are read by position. Adding, removing or reordering a member makes data written earlier unreadable. For anything that outlives a deployment, such as outbox rows, cache entries or messages in a queue, declare the type
[MemoryPackable(GenerateType.VersionTolerant)]and number its members with[MemoryPackOrder], or use JSON. - There are no readers outside .NET. Use MessagePack when another language reads the data.
Guarantees
Every format passes the same contract suite (tests/Serialization/Wiaoj.Serialization.Tests.Unit). So do JSON behind compression and JSON and MessagePack behind encryption. The suite checks:
- round trips through
byte[],string,IBufferWriter<byte>and single- and multi-segmentReadOnlySequence<byte>, generic and non-generic - round trips through
Stream - malformed input throws a
WiaojSerializationException, and theTry…methods returnfalsefor it
To cover a new format or behaviour, derive one test class from SerializerContractTests.
Contributing
See CONTRIBUTING.md. Licensed under MIT; see LICENSE.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. 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. |
-
net10.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 10.0.12)
- Wiaoj.Extensions.DependencyInjection (>= 0.4.2-alpha.1)
- Wiaoj.Primitives (>= 0.4.2-alpha.1)
- Wiaoj.Security.Abstractions (>= 0.4.2-alpha.1)
- Wiaoj.Serialization.Abstractions (>= 0.4.2-alpha.1)
- Wiaoj.Serialization.DependencyInjection (>= 0.4.2-alpha.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 0.4.2-alpha.1 | 8 | 12/2/2026 |
| 0.4.1-alpha.3 | 57 | 11/30/2026 |
| 0.4.1-alpha.2 | 59 | 11/29/2026 |
| 0.4.1-alpha.1 | 60 | 11/28/2026 |
| 0.4.0-alpha.5 | 62 | 11/27/2026 |
| 0.4.0-alpha.4 | 72 | 11/15/2026 |
| 0.4.0-alpha.3 | 75 | 11/4/2026 |
| 0.4.0-alpha.1 | 84 | 10/30/2026 |
| 0.3.0-alpha.1 | 118 | 10/5/2026 |
| 0.2.0-alpha.3 | 77 | 9/24/2026 |
| 0.2.0-alpha.2 | 73 | 9/24/2026 |
| 0.2.0-alpha.1 | 84 | 9/24/2026 |
| 0.1.0-alpha.9 | 81 | 9/21/2026 |
| 0.1.0-alpha.8 | 79 | 9/21/2026 |
| 0.1.0-alpha.7 | 90 | 9/18/2026 |
| 0.1.0-alpha.6 | 92 | 9/16/2026 |
| 0.1.0-alpha.5 | 83 | 9/16/2026 |
| 0.1.0-alpha.4 | 92 | 9/16/2026 |
| 0.1.0-alpha.3 | 81 | 9/15/2026 |
| 0.1.0-alpha.2 | 82 | 9/15/2026 |