Microsoft Formatter¶
ServiceBus.Formatters.Ms provides formatter and serializer implementations based on System.Text.Json.
Package¶
dotnet add package ServiceBus.Formatters.Ms
Main Components¶
classDiagram
class IDataFormatter
class IJsonSerializer
class MsJsonDataFormatter
class MsJsonSerializer
class JsonSerializerOptions
IDataFormatter <|.. MsJsonDataFormatter
IJsonSerializer <|.. MsJsonSerializer
MsJsonDataFormatter --> JsonSerializerOptions
MsJsonSerializer --> JsonSerializerOptions
MsJsonDataFormatter¶
The formatter implementation is:
MsJsonDataFormatter
It is the IDataFormatter implementation provided by ServiceBus.Formatters.Ms.
Dependencies¶
MsJsonDataFormatter depends on JSON serializer configuration.
Conceptually:
var options = new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
};
IDataFormatter formatter =
new MsJsonDataFormatter(options);
The exact constructor overload depends on the package version, but the important point is that the formatter is configured through System.Text.Json options.
Serialization Flow¶
MsJsonDataFormatter serializes directly to UTF-8 bytes using System.Text.Json.
graph LR
Object["CLR object"]
STJ["System.Text.Json"]
Payload["byte[]"]
Object --> STJ
STJ --> Payload
Conceptually:
JsonSerializer.SerializeToUtf8Bytes(...)
This means the Microsoft formatter does not need to create an intermediate JSON string before producing the broker payload.
Deserialization Flow¶
graph LR
Payload["byte[] / ReadOnlyMemory<byte>"]
STJ["System.Text.Json"]
Object["CLR object"]
Payload --> STJ
STJ --> Object
Conceptually:
JsonSerializer.Deserialize(...)
MsJsonSerializer¶
The serializer implementation is:
MsJsonSerializer
It implements:
IJsonSerializer
and works with JSON strings.
Dependencies¶
var options = new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
};
var serializer =
new MsJsonSerializer(options);
IDataFormatter vs IJsonSerializer¶
| Component | Contract | Output |
|---|---|---|
MsJsonDataFormatter |
IDataFormatter |
byte[] payload |
MsJsonSerializer |
IJsonSerializer |
JSON string |
Broker clients use IDataFormatter.
Use IJsonSerializer only when string-based JSON serialization is required.
When To Use¶
Use the Microsoft formatter when:
- your application uses
System.Text.Json; - performance is important;
- ASP.NET Core JSON defaults are already used;
- Newtonsoft-specific features are not required.
Recommended Scenarios¶
The Microsoft formatter is generally the preferred choice for modern .NET applications.
It integrates naturally with:
- ASP.NET Core;
- Minimal APIs;
- modern JSON serialization patterns;
- high-throughput services.
Considerations¶
System.Text.Json is intentionally strict and may require additional configuration for advanced scenarios.
If your application relies heavily on Newtonsoft.Json-specific features, consider using the Newtonsoft formatter package instead.