Skip to content

Repository files navigation

Ais.Net.Models.Json

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.

Features

  • Source-generated (reflection-free, trim/AOT-friendly) via System.Text.Json.
  • Polymorphic round-trip: any AisMessageBase is written with a $type discriminator (the first property) and read back as the correct concrete message type.
  • UTF-8-first, low allocation: serialize into an IBufferWriter<byte> or a reusable Utf8JsonWriter; deserialize directly from a ReadOnlySpan<byte>.
  • Enums are serialized as numbers; null values are omitted.
  • Ergonomic C# 14 extension members.
  • No NATS dependency — the transport stays a consumer concern.

Usage

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.

Building

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

Licenses

Licensed under the Apache 2.0 License.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages