Repository navigation
FAQ
No. It tracks strings, escapes and bracket nesting and copies everything else through. The only errors are a closing bracket with nothing to close, a closing bracket of the wrong kind, and null input. Not a validator shows what each kind of broken input produces, and Recipes how to check first.
Whitespace outside strings is discarded, and the line break that ends a // comment is whitespace, so the next token lands on the comment's line. Strip comments before printing; Not a validator has the three lines.
No, no and no. Keys stay in their order, duplicates stay, and the printer only adds whitespace. To minify, JsonNode.Parse(text)!.ToJsonString(). To sort keys, parse into a SortedDictionary, or reorder a JsonObject yourself, serialise, and then pretty print.
Since 3.0 the output is the same on every OS, which is what tests, snapshots and files checked into Git want. 1.x and 2.x used Environment.NewLine, which is CRLF on Windows. new JsonPrettyPrintOptions { NewLine = Environment.NewLine } restores it.
Because that is what the library wrote in 2014, and its output for well-formed input is the contract. IndentSize = 2 and UseTabs = true are one option away. System.Text.Json and Newtonsoft.Json both default to two spaces.
On net10.0, nothing. On netstandard2.0, System.Memory 4.6.3 (the printer works on ReadOnlySpan<char>) and System.Text.Json 10.0.12 (the serialisation helpers), which bring Microsoft.Bcl.AsyncInterfaces, System.Buffers, System.IO.Pipelines, System.Numerics.Vectors, System.Runtime.CompilerServices.Unsafe, System.Text.Encodings.Web and System.Threading.Tasks.Extensions with them. A project targeting net10.0 gets the dependency-free build; anything older gets the netstandard2.0 build and its packages. The System.Text.Json reference was kept in 3.0 on purpose, so that .NET Framework projects have the helpers too.
The download is 37 KB and the assembly 12.8 KB per target framework. At run time the engine keeps a sixteen-entry scope stack that grows only for deeper nesting.
PrettyPrintJson(), yes: it keeps one printer per thread. A JsonPrettyPrinter you created, no: one per thread, or a lock. JsonPrettyPrintOptions is immutable and shareable. Performance and threading has the table.
The input has to be in memory as a string or span; the output can stream to a TextWriter. 1 MB formats in about 4.5 ms on a desktop CPU, and a document nested a thousand levels deep is fine. There is no TextReader or Stream input overload; read the text first.
The net10.0 build is marked trimmable and AOT compatible, and the printer uses no reflection. The reflection-based ToJson and DeserializeFromJson overloads warn under trimming and throw under AOT; the JsonTypeInfo<T> overloads are the AOT path. .NET 10 file-based apps are AOT by default, so the same applies to dotnet run app.cs unless the file says #:property PublishAot=false. Serialisation helpers has the code.
Yes, 4.6.2 and later, through the netstandard2.0 build. The repository's tests run on net48 in CI, so that build is executed, not just compiled.
Not tested. The netstandard2.0 build needs System.Memory and, for the helpers, System.Text.Json and its dependencies; see Getting started.
The strings are right; the console is not showing them. Set Console.OutputEncoding = System.Text.Encoding.UTF8; before writing, or write to a UTF-8 file.
System.Text.Json's default encoder escapes quotes, apostrophes, <, >, & and everything outside ASCII. Pass new JsonSerializerOptions { Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping }; Serialisation helpers explains the "unsafe".
The JSON formatter at https://www.markdavidrogers.com/tools and the site's MCP tool prettify_json are built on System.Text.Json (two-space indent, input validated), not on this package, so their output differs from PrettyPrintJson().
Their tags exist in the repository, but the packages never reached nuget.org. Each tag was pushed to a commit whose CI run failed. Tags are not moved here, so the fixed commits became 2.1.1 and 3.0.1. Same code, new numbers. Versions and upgrading has the whole table.
The target framework (net35 is gone), the line endings (a line feed now), ToJSON() (now ToJson(), on System.Text.Json instead of JavaScriptSerializer, with different date and escape output), and any code that touched JsonPPStrategyContext. Versions and upgrading goes through each.
Bugs and questions go to the issues. Include the input text that misbehaved; the whole printer is one pass over the characters, so a reproduction is usually a one-liner.
This wiki describes JsonPrettyPrinter 3.0.2 and was last updated on 2026-09-30. The library is MIT licensed. Report problems in the issues.
Using it
- Getting started
- API reference
- Output format
- Not a validator
- Serialisation helpers
- Recipes
- Performance and threading
- FAQ
The releases
Contributing
Elsewhere