Formatter Comparison¶
ServiceBus.Core provides formatter implementations based on both System.Text.Json and Newtonsoft.Json.
The right formatter depends on the application requirements, serializer features and performance profile.
Available Formatters¶
| Package | Formatter | Serializer |
|---|---|---|
ServiceBus.Formatters.Ms |
MsJsonDataFormatter |
System.Text.Json |
ServiceBus.Formatters.Newtonsoft |
JsonDataFormatter |
Newtonsoft.Json |
ServiceBus.Formatters.Newtonsoft |
RwJsonDataFormatter |
Newtonsoft.Json |
Comparison¶
| Formatter | Best For | Notes |
|---|---|---|
MsJsonDataFormatter |
Modern .NET applications | Serializes directly to UTF-8 bytes. |
JsonDataFormatter |
Simple Newtonsoft.Json usage | Uses JsonConvert and Encoding. |
RwJsonDataFormatter |
Allocation-sensitive Newtonsoft.Json usage | Uses reader/writer pipeline to reduce allocation pressure. |
System.Text.Json vs Newtonsoft.Json¶
| Area | System.Text.Json | Newtonsoft.Json |
|---|---|---|
| Default .NET integration | Excellent | External package |
| ASP.NET Core alignment | Strong | Optional |
| Legacy compatibility | Lower | Strong |
| Custom converters | Good | Excellent |
| Performance profile | Strong | Good |
| Advanced polymorphism | More explicit | Very flexible |
Selection Guidelines¶
Choose MsJsonDataFormatter when:
- the application is new;
System.Text.Jsonis sufficient;- ASP.NET Core defaults are preferred;
- direct UTF-8 payload generation is valuable.
Choose JsonDataFormatter when:
- Newtonsoft.Json compatibility is required;
- a simple formatter implementation is preferred;
- allocation pressure is not a primary concern.
Choose RwJsonDataFormatter when:
- Newtonsoft.Json is required;
- message volume is high;
- allocation reduction matters;
- reducing GC pressure is important.
Compatibility Rule¶
Publishers and subscribers must agree on the payload format.
The safest approach is to use the same formatter implementation on both sides of a message flow.
graph LR
Publisher["Publisher<br/>IDataFormatter"]
Payload["byte[] payload"]
Subscriber["Subscriber<br/>IDataFormatter"]
Publisher --> Payload
Payload --> Subscriber
Recommendation¶
For most new .NET applications, start with:
MsJsonDataFormatter
Use Newtonsoft-based formatters when compatibility, existing converters or advanced serializer behavior requires Newtonsoft.Json.