Json5 1.0.4
dotnet add package Json5 --version 1.0.4
NuGet\Install-Package Json5 -Version 1.0.4
<PackageReference Include="Json5" Version="1.0.4" />
<PackageVersion Include="Json5" Version="1.0.4" />
<PackageReference Include="Json5" />
paket add Json5 --version 1.0.4
#r "nuget: Json5, 1.0.4"
#:package Json5@1.0.4
#addin nuget:?package=Json5&version=1.0.4
#tool nuget:?package=Json5&version=1.0.4
A JSON5 parser for .NET built entirely on top of the public
System.Text.Json API surface. Requires .NET 10+.
Usage
Parse to JsonNode
using Json5;
JsonNode? node = Json5.Parse("""
{
// comments are allowed
unquoted: 'single-quoted string',
hex: 0xDECAF,
leadingDot: .5,
trailing: 'comma',
}
""");
Deserialize to a typed object
var config = Json5.Deserialize<AppConfig>(json5String);
Parse to JsonDocument
using var doc = Json5.ParseDocument(json5String);
JsonElement root = doc.RootElement;
Convert to standard JSON
string json = Json5.ToJson(json5String);
byte[] utf8 = Json5.ToUtf8Json(json5Bytes);
Write to Utf8JsonWriter
Json5.WriteTo(json5String, writer);
JSON5 Features
All JSON5 extensions beyond standard JSON are supported:
| Feature | Example |
|---|---|
| Unquoted object keys | { foo: 1 } |
| Single-quoted strings | 'hello' |
| Multi-line strings (escaped newlines) | 'line1\↵line2' |
| Hexadecimal numbers | 0xFF |
| Leading/trailing decimal points | .5, 2. |
| Explicit positive sign | +1 |
| Infinity, -Infinity, NaN | Infinity |
| Single and multi-line comments | // … and /* … */ |
| Trailing commas | [1, 2,] |
| Additional escape sequences | \v, \0, \xHH |
| Extended whitespace | Unicode Zs category, BOM |
Auto-Dedent Multi-line Strings
When parsing multi-line string values, you can automatically remove common leading
whitespace via Json5ReaderOptions.AutoDedent. This makes it easier to write readable
indented code without the indentation appearing in the final string value.
var options = new Json5ReaderOptions { AutoDedent = true };
var node = Json5.Parse("""
{
description: '
This is a multi-line string
with consistent indentation
that will be removed.
'
}
""", options);
// description will be: "This is a multi-line string\nwith consistent indentation\nthat will be removed."
The algorithm:
- Strips the first line if blank
- Strips the last line if blank
- Finds the minimum common leading whitespace across all remaining non-blank lines
- Removes that minimum indent from every line
Only string values are affected; property names are never dedented. Blank lines within the string are preserved.
Infinity / NaN Handling
Since standard JSON has no representation for Infinity and NaN, the behavior
is configurable via Json5ReaderOptions.SpecialNumbers:
| Mode | Behavior |
|---|---|
AsString (default) |
Emits as "Infinity", "-Infinity", or "NaN" |
AsNull |
Emits as null |
Throw |
Throws Json5Exception |
var options = new Json5ReaderOptions
{
SpecialNumbers = SpecialNumberHandling.AsNull
};
var node = Json5.Parse("Infinity", options); // returns null
Open Source Maintenance Fee
To ensure the long-term sustainability of this project, users of this package who generate revenue must pay an Open Source Maintenance Fee. While the source code is freely available under the terms of the License, this package and other aspects of the project require adherence to the Maintenance Fee.
To pay the Maintenance Fee, become a Sponsor at the proper OSMF tier. A single fee covers all of Devlooped packages.
Sponsors
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. 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 is compatible. 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. |
-
net10.0
- No dependencies.
-
net8.0
- No dependencies.
NuGet packages (1)
Showing the top 1 NuGet packages that depend on Json5:
| Package | Downloads |
|---|---|
|
Json5.Configuration
A Microsoft.Extensions.Configuration provider for JSON5 files, on top of the Json5 parser. |
GitHub repositories
This package is not used by any popular GitHub repositories.