Skip to content
m4bwav edited this page Sep 30, 2026 · 2 revisions

Does it validate the JSON?

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.

My comment ate the next key

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.

Does it sort keys, remove duplicates or minify?

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.

Why do lines end with a line feed and not Environment.NewLine?

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.

Why four spaces?

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.

What does it depend on?

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.

How big is it?

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.

Can I share a printer between threads?

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.

How large a document can it handle?

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.

Does it work with trimming and native AOT?

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.

Does it work on .NET Framework?

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.

Does it work in Unity?

Not tested. The netstandard2.0 build needs System.Memory and, for the helpers, System.Text.Json and its dependencies; see Getting started.

Some characters show as question marks

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.

ToJson() output is full of \u00XX

System.Text.Json's default encoder escapes quotes, apostrophes, <, >, & and everything outside ASCII. Pass new JsonSerializerOptions { Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping }; Serialisation helpers explains the "unsafe".

Is there an online version?

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().

What happened to 2.1.0 and 3.0.0?

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.

I am on 1.0.1.1. What breaks?

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.

Where do I report a problem?

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.

Clone this wiki locally