JsonEnvelopes 3.0.0
dotnet add package JsonEnvelopes --version 3.0.0
NuGet\Install-Package JsonEnvelopes -Version 3.0.0
<PackageReference Include="JsonEnvelopes" Version="3.0.0" />
<PackageVersion Include="JsonEnvelopes" Version="3.0.0" />
<PackageReference Include="JsonEnvelopes" />
paket add JsonEnvelopes --version 3.0.0
#r "nuget: JsonEnvelopes, 3.0.0"
#:package JsonEnvelopes@3.0.0
#addin nuget:?package=JsonEnvelopes&version=3.0.0
#tool nuget:?package=JsonEnvelopes&version=3.0.0
JsonEnvelopes
Standardized messaging with System.Text.Json serialization.
Purpose
Preform serialization/deserialization of messages such that receiving services can be agnostic with respect to message type.
Get started
Install the JsonEnvelopes NuGet Package (e.g. version 3.0.0).
dotnet add package JsonEnvelopes --version 3.0.0
The package targets .NET 10 and .NET Standard 2.0. It supports .NET Framework
4.7.2+ and .NET 8+. Apps on .NET 10 and later use the net10.0 build, which has no
package dependencies. Every other app uses the netstandard2.0 build, which depends on
System.Text.Json 10.0.0 or later and its transitive packages. On .NET 6 and 7, those
Microsoft packages emit a build warning that the target framework is unsupported.
Usage
Register every type you are willing to receive, then add a converter built from that registry to
your JsonSerializerOptions.
var registry = EnvelopeTypeRegistry.Create(builder => builder
.Add<CastFireball>("CastFireball.v1")
.Add<CreateCharacter>("CreateCharacter.v1"));
var jsonOptions = new JsonSerializerOptions();
jsonOptions.Converters.Add(new EnvelopeJsonConverter(registry));
The logical name you pass to Add is what travels on the wire. It is an opaque string; this
library never parses it. The .v1 suffix above is a suggestion, not a requirement.
Wrap content whose type you know at compile time with Envelope<T>, or let the registry pick the
type at run time with Wrap.
var envelope = new Envelope<CastFireball>(new CastFireball());
var alsoAnEnvelope = registry.Wrap(someCommand);
Serialize by calling JsonSerializer.Serialize<Envelope> with your options. Note that the generic
argument is Envelope, not Envelope<T> — that is what engages EnvelopeJsonConverter.
var json = JsonSerializer.Serialize<Envelope>(envelope, jsonOptions);
The result carries the logical name, not a .NET type name:
{"ContentType":"CastFireball.v1","Content":{ ... }}
Deserialize the same way.
var receivedEnvelope = JsonSerializer.Deserialize<Envelope>(json, jsonOptions);
ContentType gives you the content's CLR type, ready for handler lookup.
var contentType = receivedEnvelope.ContentType; // typeof(CastFireball)
var content = receivedEnvelope.GetContent(); // the CastFireball instance
Unregistered types are refused
A name that is not in the registry throws UnknownContentTypeException, which derives from
JsonException. The library never resolves a type name supplied by the sender, so a message cannot
choose which type your process loads. Catch the narrow type to dead-letter an unrecognized message:
try
{
var received = JsonSerializer.Deserialize<Envelope>(json, jsonOptions);
}
catch (UnknownContentTypeException exception)
{
DeadLetter(json, exception.ContentTypeName);
}
Migrating from 2.x
Version 3.0.0 is a breaking change and its wire format is not compatible with 2.x. Messages written by 2.x cannot be read by 3.0.0, so drain any queue before upgrading.
| 2.x | 3.0.0 |
|---|---|
| No setup; the converter resolved any type name | Build an EnvelopeTypeRegistry and add new EnvelopeJsonConverter(registry) to your options |
ContentType was a string holding an AssemblyQualifiedName |
ContentType is a Type; the wire carries your logical name |
Envelope.WrapContent(content) |
registry.Wrap(content) |
Envelope<T> required T to have a parameterless constructor |
Any reference type, including positional records |
new Envelope<T>() |
Removed; use new Envelope<T>(content) |
An unknown type threw TypeLoadException |
An unregistered name throws UnknownContentTypeException |
Serializing the concrete Envelope<T> worked the same as serializing Envelope |
ContentType is now a Type, which System.Text.Json refuses to serialize by reflection; serializing Envelope<T> by its own type now throws NotSupportedException instead of silently producing different output. Always call JsonSerializer.Serialize<Envelope>(envelope, options) so the converter runs |
Call sites that relied on Type.GetType(envelope.ContentType) simplify to envelope.ContentType.
Examples
See the project JsonEnvelopes.Example for examples of JsonEnvelopes message handling with Dependency Injection and additionally with MediatR.
Credits
The methodology used in JsonEnvelopes was heavily inspired by the work of the extraordinary developer Jonathan Berube. My thanks to him.
| 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 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. |
| .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 (>= 10.0.0)
-
net10.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.