
lsmsgpack
io.github.mlsomers/lsmsgpack
Decodes MsgPack (LsMsgPack schemas, type ids) as text and reads it from variables while debugging.
Documentation
LsMsgPack
Fast MessagePack (MsgPack) serializer for .NET classes (like the xml and json serializers), with an optional indexed schema and type ids for polymorphic object models, ASP.NET Core / Web API / MVC formatters, a TypeScript client, and a MsgPack debugging and validation tool (MsgPack Explorer) that also works as a Fiddler plugin, a Visual Studio debugger visualizer and a VS Code extension.
Start here
dotnet add package LsMsgPack
byte[] data = LsMsgPack.LsMsgPackSerializer.Default.Serialize(order);
Order copy = LsMsgPack.LsMsgPackSerializer.Default.Deserialize<Order>(data);
LsMsgPack is the serializer to use: about 3-5x as fast as System.Text.Json, with 35-47% smaller payloads. The LsMsgPackDebug package is the slower one that builds a tree of MsgPack items for inspecting data, see which package. For ASP.NET, add a formatter instead, it brings LsMsgPack along.
Not sure where to start? The setup wizard asks a few questions (files or network, who reads the data, JavaScript or not) and ends with the packages, settings and code for your case.
ASP.NET Integration
Three packages let ASP.NET controllers and HttpClient receive and return MsgPack the same way they handle JSON. Clients choose the format with the Content-Type and Accept headers: application/msgpack (or application/x-msgpack) for plain MsgPack that any implementation understands, or application/x-lsmsgpack for the indexed schema with type ids (smaller, polymorphic, and with the schema sent only once per client).
| Package | For | Registration |
|---|---|---|
LsMsgPack.AspNetCore | ASP.NET Core 2.x and up | services.AddControllers().AddLsMsgPackSerializerFormatters(); |
LsMsgPack.AspNet.WebApi | ASP.NET Web API 2, and HttpClient on any platform | config.Formatters.Add(new LsMsgPackMediaTypeFormatter()); |
LsMsgPack.AspNet.Mvc | ASP.NET MVC 5 | LsMsgPackMvc.LsMsgPackMvc.Register(); in Application_Start, return this.MsgPack(data); in actions |
See docs/WebFormatters.md for the settings per media type, examples for each package, and how schemas are negotiated.
SignalR
LsMsgPack.SignalR is a MessagePack hub protocol for ASP.NET Core SignalR, for the server and the .NET client: services.AddSignalR().AddLsMsgPackProtocol();. The browser uses @lsmsgpack/client/signalr. Registered as "messagepack" (AddLsMsgPackAsMessagePackProtocol()), it writes the bytes of Microsoft's MessagePack protocol, so existing clients connect unchanged. It writes and reads in about half the time of Microsoft's protocol. See docs/SignalR.md.
Client applications
The serializers run in any .NET client (WinForms, WPF, MAUI, Avalonia...). Save and load your documents with LsMsgPackSerializer.Serialize / Deserialize: with the default settings, files are smaller than JSON, keep loading when your classes gain or lose properties, and support polymorphic object models without extra attributes. To call a web service that uses the formatters above, use LsMsgPack.AspNet.WebApi with HttpClient, or serialize the bodies yourself for any other MsgPack server.
See docs/ClientApps.md for examples (files, web services, trimming).
Which package
Install LsMsgPack to serialize: it is the fast serializer (streaming, source generator, presets for other MsgPack libraries), and the web formatters build on it. Install LsMsgPackDebug only to look inside MsgPack data (item tree, validation, the explorers). LsMsgPack.Core comes with both. The two write exactly the same data with the same settings and read each other's output:
- LsMsgPack: use this for normal use. It writes and reads directly from and to your objects. On the invoices of the benchmark it writes and reads about 4.5-5.5x as fast as System.Text.Json, with 35-47% smaller payloads. It also has presets for exchanging data with MessagePack-CSharp, Nerdbank.MessagePack and non-.NET libraries. The web formatters use it. How it compares with MessagePack-CSharp and Nerdbank.MessagePack (benchmarks, and when to pick which): docs/Comparison.md.
- LsMsgPackDebug: use this when you really want to inspect MsgPack data in your app. It builds a tree of MsgPack items (maps, arrays, strings...) that you can walk, for any payload, also when you don't have the classes. That makes it much slower: about 2-4x slower than System.Text.Json.
NuGet packages
graph BT
Core["LsMsgPack.Core<br/><i>settings, schema, type resolving</i>"]
Debug["LsMsgPackDebug<br/><i>item tree, inspecting</i>"]
Fast["LsMsgPack<br/><i>fast serializer</i>"]
AspNetCore["LsMsgPack.AspNetCore"]
WebApi["LsMsgPack.AspNet.WebApi"]
Mvc["LsMsgPack.AspNet.Mvc"]
Data["LsMsgPack.Data<br/><i>ADO.NET data readers</i>"]
SignalR["LsMsgPack.SignalR<br/><i>SignalR hub protocol</i>"]
Lz4["LsMsgPack.Lz4<br/><i>LZ4 compression</i>"]
Mem(["System.Memory"])
Unsafe(["System.Runtime.CompilerServices.Unsafe"])
AspNetCoreFx(["Microsoft.AspNetCore.Mvc.Core 2.x /<br/>Microsoft.AspNetCore.App"])
WebApiClient(["Microsoft.AspNet.WebApi.Client"])
AspNetMvc(["Microsoft.AspNet.Mvc"])
SignalRCommon(["Microsoft.AspNetCore.SignalR.Common"])
K4os(["K4os.Compression.LZ4"])
Debug --> Core
Fast --> Core
AspNetCore --> Fast
WebApi --> Fast
Mvc --> Fast
Data --> Fast
SignalR --> Fast
Lz4 --> Core
Core -.-> Mem
Debug -.-> Mem
Fast -.-> Mem
Fast -.-> Unsafe
AspNetCore -.-> AspNetCoreFx
WebApi -.-> WebApiClient
Mvc -.-> AspNetMvc
SignalR -.-> SignalRCommon
Lz4 -.-> K4os
Solid arrows are the packages of this repository, dotted ones external dependencies. The web formatters depend on LsMsgPack only, so add LsMsgPackDebug yourself when you want to inspect data. Both serializers share LsMsgPack.Core and its namespaces (LsMsgPack.*), so attributes, type resolvers and filters work with either. LsMsgPack.Data writes the rows of ADO.NET data readers as MsgPack and reads dumps back as a DbDataReader, see docs/DataReader.md. LsMsgPack.SignalR is the SignalR hub protocol, see docs/SignalR.md. LsMsgPack.Lz4 compresses the body with LZ4 (Compression = Lz4Compression.Block, MessagePack-CSharp's format), see docs/Compression.md.
Not in the diagram, because they aren't referenced by your code: the .NET tools LsMsgPack.Mcp (lsmsgpack-mcp, MsgPack for AI agents) and LsMsgPack.TypeScript (lsmsgpack-ts, TypeScript models for the web formatters), which bring their own copies of the libraries, and the npm package @lsmsgpack/client.
Compatibility with other implementations
Serializing classes by creating name-value dictionaries of their properties is not an official standard, and to my surprise I found than a many MsgPack implementations do not. Some just string a list of values into an array. This is indeed efficient and will work well for the first version, however migrating to a new version may pose some compatibility challenges when introducing new properties over time.
For this reason I have submitted a pull request to the official MsgPack specification, including a more standardized choice of solutions and in addition a standard way to support polymorphic class-hierarchies.
Which settings to use to exchange data with MessagePack-CSharp, Nerdbank.MessagePack, Python, JavaScript and others is described in docs/Compatibility.md.
While using dictionaries diminishes the small size of a MsgPack message, it does help bring up the compatibility level with other serializers (XML / JSON) so that it can be used as a drop-in replacement.
Polymorphic class-hierarchy support
- Indexed schema (
UseIndexedSchema, on by default): each message starts with a small schema listing every type and its property names once. The body then refers to them by index. This roughly halves the size of messages with repeating objects, and it stays tolerant to adding or removing properties. Other MsgPack implementations don't understand it, so turn it off when they need to read your data. - Type ids (
AddTypeIdOptions,IfAmbiguousby default): adds the type name (or a custom id) when a property holds a derived type. This gives polymorphic class hierarchies (aList<IPet>holding cats and dogs) out of the box, withoutXmlInclude-style attributes. The types are found again by their short name, in the assembly of the declared type. When they live elsewhere (e.g. a property declared asobject, or plugins), register their assembly once withCacheAssemblyTypes. - Type resolvers (
TypeResolvers): control how type ids are written and resolved, for example with short[XmlRoot]names, numeric ids, or by recognizing a type from its properties. - Plain MsgPack (
UseIndexedSchema = false,AddTypeIdOptions = Never): plain maps of property names and values that any MsgPack implementation can read.
See docs/schema.md for examples of each wire format, their pros and cons, how polymorphic class-hierarchy support works, and how type ids are resolved.
Reading data you don't trust (request bodies, messages from clients)? Type ids let the data pick the classes that are created: see docs/security.md for what the serializers check and how to limit the allowed types with a type guard.
The packages include analyzers that check the classes you serialize while you type: initialized properties whose left-out default would be read back as another value (with code fixes that add the [DefaultValue]), properties declared as object where the data picks the type, public fields that are not serialized, and extension data members the serializers refuse. See docs/Analyzers.md.
Types that shouldn't be written as their own properties (value objects, types of another library, a DateTime a peer expects as Unix time) can be written as another type with a converter: see docs/Converters.md.
Enums are written as their numbers by default, or as their names (EnumFormat.Name, or [MsgPackEnumFormat] on an enum or property). The string enum attributes of System.Text.Json, Json.NET, MessagePack-CSharp and MsgPack.Cli are recognized too, and readers take both forms: see docs/Enums.md.
Unity 6 (editor, Mono and IL2CPP players) is supported: its structs such as Vector3 are written by their fields, [SerializeField] opts a field in, and the source generator runs in Unity: see docs/Unity.md.
F# records, options, unions and lists are written and read without setup: an option as its value, a union case as an object with its case name as the type id or, without fields, as its name: see docs/FSharp.md.
Properties are written by their .NET names, or converted for readers in other languages (invoiceDate, invoice_date) by a naming policy: PropertyNamingPolicy, DictionaryKeyPolicy and EnumNamingPolicy take the same policies as System.Text.Json (MsgPackNamingPolicy.CamelCase, SnakeCaseLower...), and readers take the converted and the .NET names: see docs/NamingPolicies.md.
Data that doesn't match your classes exactly (older or newer versions, a misspelled name) is read anyway, like the JSON serializers do. Pass an out ReadDifferences to Deserialize to find out what was skipped, counted per class and property, with the paths of the objects: see docs/ReadDifferences.md. An older version of a service that reads, changes and writes back an object keeps the members it doesn't have in a MsgPackExtensionData (or a [JsonExtensionData] dictionary) and writes them again: see docs/ExtensionData.md. A class can run code before and after it's written or read, for example to validate what it read (IMsgPackOnDeserialized, or System.Text.Json's IJsonOnDeserialized): see docs/Callbacks.md. Populate reads a payload into an object you already have, and the options can read into the objects members already hold: see docs/Populate.md.
Standalone Debugger (for Windows)
Download the zip file from the latest release. It can open MsgPack files or read data from the clipboard. This UI is used in the Visual Studio and Fiddler integrations. More info and screenshots: infotopie.nl/open-source/msgpack-explorer.

Fiddler Integration
In order to use this tool as a Fiddler plugin, copy the following files to the Fiddler Inspectors directory (usually C:\Program Files\Fiddler2\Inspectors):
- MsgPackExplorer.exe
- LsMsgPackFiddlerInspector.dll
- LsMsgPackDebug.dll
- LsMsgPack.Core.dll
- System.Memory.dll
- System.Buffers.dll
- System.Numerics.Vectors.dll
- System.Runtime.CompilerServices.Unsafe.dll
Restart fiddler and you should see a MsgPack option in the Inspectors list.
Visual Studio Integration
The tool can also be used as a debugging Visualizer in Visual Studio. Directly inspect byte[] or Stream instances in the debugger. It can be installed via the Visual Studio Marketplace.
VS Code Integration
LsMsgPackVsCode is the same explorer as a VS Code extension (any OS, needs the .NET 8 runtime): right-click a byte[], Stream, List<byte>, Memory<byte>, HTTP content or base64 string in the Variables view while debugging and choose View as MsgPack, or open a .msgpack file. It also reads typed arrays in JavaScript and bytes in Python. It can be installed via the Visual Studio Marketplace.

AI agents (MCP)
LsMsgPackMcpServer (lsmsgpack-mcp) is an MCP server and command line tool for AI agents. It decodes MsgPack into JSON with comments (types from the indexed schema and type ids, timestamps, decimals, errors with their offsets). It also reads the bytes of a variable while a program is paused in VS Code or Visual Studio, through their MsgPack extensions. See docs/Mcp.md.
TypeScript
@lsmsgpack/client reads the application/x-lsmsgpack responses of the web formatters into complete JavaScript objects (also the properties the server left out, and polymorphic values) and writes request bodies they read, as fast as JSON.parse and JSON.stringify. See docs/TypeScriptClient.md.
LsMsgPackTypeScript (lsmsgpack-ts) writes TypeScript classes for the .NET models of a server (with the values their constructors set) and the schemas the server will write, so the client holds them before its first request. See docs/TypeScriptModels.md.
Source documentation
The repository holds the shared core (LsMsgPack.Core), the two serializers (LsMsgPackDebug with its item tree, LsMsgPack without it), the web formatters, MsgPack Explorer with its Fiddler and Visual Studio wrappers, the VS Code extension, and their tests. See docs/Architecture.md for what each project does, how the serializers work, and the class diagrams.
LsMsgPack.Mcp# nuget: LsMsgPack.Mcp