Low-allocation, UTF-8-first, polymorphic System.Text.Json serialization for the Ais.Net.Models AIS message types.
Designed for broadcasting decoded AIS messages over byte-oriented transports such as NATS and reconstructing the concrete .NET type on receipt. Sponsored by endjin.
- Source-generated (reflection-free, trim/AOT-friendly) via
System.Text.Json. - Polymorphic round-trip: any
AisMessageBaseis written with a$typediscriminator (the first property) and read back as the correct concrete message type. - UTF-8-first, low allocation: serialize into an
IBufferWriter<byte>or a reusableUtf8JsonWriter; deserialize directly from aReadOnlySpan<byte>. - Enums are serialized as numbers; null values are omitted.
- Ergonomic C# 14 extension members.
- No NATS dependency — the transport stays a consumer concern.
using Ais.Net.Models;
using Ais.Net.Models.Json;
// Serialize to UTF-8 JSON (with a $type discriminator).
byte[] utf8 = AisMessageJson.SerializeToUtf8Bytes(message);
// ...or, lower allocation, into a buffer writer or a reusable writer:
AisMessageJson.Serialize(bufferWriter, message);
// ...or via extension members:
byte[] utf8b = message.ToJsonUtf8Bytes();
// Deserialize back into the concrete message type.
AisMessageBase? received = AisMessageJson.Deserialize(utf8);
if (received is AisMessageType18 positionReport)
{
// ...
}For NATS, use the dedicated Ais.Net.Models.Json.Nats package — a ready-made NATS.Net serializer and registry built on these primitives.
Requires the .NET 10 SDK. Depends on the Ais.Net.Models package (1.0.0 or later).
dotnet build Solutions/Ais.Net.Models.Json.slnx -c Release
Licensed under the Apache 2.0 License.