Mugi.Json
0.1.3
dotnet add package Mugi.Json --version 0.1.3
NuGet\Install-Package Mugi.Json -Version 0.1.3
<PackageReference Include="Mugi.Json" Version="0.1.3" />
<PackageVersion Include="Mugi.Json" Version="0.1.3" />
<PackageReference Include="Mugi.Json" />
paket add Mugi.Json --version 0.1.3
#r "nuget: Mugi.Json, 0.1.3"
#:package Mugi.Json@0.1.3
#addin nuget:?package=Mugi.Json&version=0.1.3
#tool nuget:?package=Mugi.Json&version=0.1.3
Mugi.Json
Mugi.Json is the NativeAOT-compatible JSON serializer used by the Mugi web framework. It reads and writes JSON through per-type codecs registered ahead of time, so serialization works without reflection or runtime code generation.
In a Mugi application you normally never call this package directly: the Mugi.Generators package writes a codec for every type your handlers serialize, and c.Json(...) and c.Req.Json<T>() use them. Reference this package on its own when you need the serializer outside a handler or want to write a codec by hand.
Serialize and deserialize
using System.Buffers;
using Mugi.Json;
var buffer = new ArrayBufferWriter<byte>();
Json.Serialize(buffer, new User("42", "Ada"));
var user = Json.Deserialize<User>(buffer.WrittenSpan);
public sealed record User(string Id, string Name);
Json.Serialize<T> writes UTF-8 JSON to any IBufferWriter<byte>, and Json.Deserialize<T> parses a ReadOnlySpan<byte>. Both take an optional JsonOptions. A codec for T must be registered first; in a Mugi app the generator does this for the types it sees at call sites, and Json.Include<T>() covers a type reached only through generics.
Writing a codec by hand
Implement IJsonCodec<T> and register it with Json.Register. A registered codec is used everywhere that type is serialized. JsonWriter and JsonReader are ref struct types over the output buffer and input span, with methods such as WriteString, WriteNumber, WriteRaw, ReadBeginObject, ReadPropertyName, and SkipValue.
using Mugi.Json;
Json.Register(UserCodec.Instance);
Limits for untrusted input
Parsing enforces limits so hostile JSON cannot exhaust memory or the stack: document size, nesting depth, string length, collection size, and number length are all capped through JsonOptions. NaN and Infinity are rejected by default.
Full documentation is at github.com/hckaye/Mugi.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net9.0
- No dependencies.
NuGet packages (4)
Showing the top 4 NuGet packages that depend on Mugi.Json:
| Package | Downloads |
|---|---|
|
Mugi
A fast, simple web application framework for .NET, built for Native AOT. |
|
|
Mugi.Jwt
Native AOT-compatible JWT signing, validation, and authentication middleware for Mugi. |
|
|
Mugi.Schema
Typed request binding and validation for Mugi. |
|
|
Mugi.Reflection
Opt-in reflection-based JSON codecs for Mugi. |
GitHub repositories
This package is not used by any popular GitHub repositories.