This blog explains a common problem of parsing broken or cut-off JSON and Harness' fix for it.
.png)
For Harness, cut-off or broken JSON happens constantly. The Harness WAF sees a huge amount of traffic, and we run anomaly detection and decision making over billions of data points. To keep that affordable and efficient, we truncate spans once they pass a specific size limit. The limit has no idea where a JSON string, number or escape sequence ends, so a large body can end up looking like this:
{
"user": "alice",
"roles": [
"admin",
"dev"
],
"token": "abc123
The user and roles are all there, but Jackson, Gson and fastjson2 throw on it because the document never closes. A higher limit would only move the cut point, and bigger spans cost more to store, ship and analyze, so we keep the existing limit and the parser reads what it gets in that limit.
Why broken or cut-off JSON creates problems
A strict parser treats a broken body as a failed body. One missing bracket and the whole payload is rejected, including the parts that arrived intact. That leaves two bad options. Skip the body, and a payload that is cut off or malformed, by accident or on purpose, goes uninspected. Or repair it first and parse it afterwards, as we did, and every payload is read twice, which costs time and compute on a path that runs for every request. The repair logic also has to live somewhere. Each WAF engine that powers the Harness WAF had its own truncation handling, duplicated across call sites, so every fix had to be built and maintained more than once.
We wanted one parser, one pass, in one place, without giving up speed. So we created laxjson4j: on a 97 KB document it parses in 165 µs, against 182.7 µs for Jackson and 208.5 µs for Gson.

What “lenient” means
A lenient parser reads JSON that doesn't strictly follow the spec, such as cut-off or slightly malformed input, and returns what it can recover instead of throwing an error. That fits this problem because a cut-off body still carries almost all of its information, so parsing what's there beats rejecting it.
laxjson4j accepts everything valid that JSON accepts, plus the mistakes that real traffic actually contains. Instead of throwing, it absorbs them and returns an ordinary tree you can navigate.
The rule is to parse whatever is there, and don't drop or make up data. Duplicate keys are a good example. Most parsers keep the last value and quietly drop the others. In a security product, that's a problem, since sending the same key twice is an old trick for getting one value past a check while the backend uses the other. We keep all of them in an array.

Truncated spans are what we built it for, but the same parser handles other broken input:
- LLM responses cut off at the token limit, or returned with trailing commas and comments
- JSON embedded in log lines, often partial or clipped by the logger
- Config files edited by hand, which pick up comments and missing commas
- Request and response bodies that end early
Attackers also send broken payloads on purpose, because a strict parser will drop them before anything inspects them. If you only need clean JSON back, fixJsonString does that in one call.
Where version 1 stood
On a 100 KB valid document, version 1 managed about 8,000 operations per second. Jackson did roughly 9,500 and Fastjson2 about 17,500. Only Hjson, the best-known lenient parser, was clearly behind at around 3,400.
When we profiled it, very little of the time went to handling bad input. Most of it came from how the parser was built, as a textbook pipeline:

We tried tuning it first: a 128-entry dispatch table, char instead of Character in the lexer, plain enums instead of Token objects, a char buffer for input, pooled parsers per thread, and a lazy mode that skipped nested objects until they were read. These helped some, but the parser still ran in two stages, continued to box every number, and tracked line and column for every character. So we rewrote it.
Version 2: rebuilding the design

For v2 we kept the behavior and redesigned everything underneath it around five decisions.
First, one pass, no tokens. A single recursive-descent scanner builds tree nodes directly. Anything it doesn’t recognize becomes a string, and the end of input closes whatever is still open. Nothing gets parsed twice, and no exception is thrown and caught on the hot path.

Second, a chunked char[] window. Input is copied into a reusable 32K buffer and refilled as the parser goes. The JIT handles a primitive array far better than repeated String.charAt calls. Our tests also run with 8-character buffers to push every token across a chunk boundary.
![Diagram of the chunked char[] window: a reusable 32K buffer refilled as the scanner reads, with tests using 8-character buffers](https://cdn.prod.website-files.com/69e786e187c925d08ee02b92/6b104e54de20b41dae634294_6b104da8b6509ba2d1297b85_laxjson4j-06-chunked-char-window.png)
Third, no steady-state allocation. Each thread keeps a pooled parser with its own buffers, so after the first call, parsing allocates only the tree. Oversized buffers are trimmed on release, and re-entrant calls get a fresh parser.
Fourth, a lighter tree. Numbers live in typed nodes (IntNode, LongNode, DoubleNode, BigIntegerNode, BigDecimalNode), so nothing is boxed, and small objects keep their fields in flat arrays instead of a map.
Fifth, lock-free name interning. Field names repeat constantly, so we intern them by hashing the raw characters, and a known name allocates no new String. Each thread reads its own snapshot of the name table and publishes new names with one compare-and-swap when the parse ends.

Closing the gap on parsing
The v2 design removed the structural costs, but it was not yet faster than Jackson. On a single thread, a 97 KB parse took 229.8 µs against Jackson's 182.7 µs. The changes below brought it to about 165 µs.
Numbers. Digit parsing is unrolled for numbers of up to 9 digits (which fit in an int), split for 10 to 18 digits (which fit in a long), and range-checked at 19 digits, where a long can overflow. Doubles go through the wrandelshofer/fastdoubleparser library, and very long numbers stay as text until someone asks for them.

Strings. A 128-entry character-class table answers "quote, backslash, or newline?" in one load, so a normal string costs one new String. The fast path keeps the end indices and this table in locals. On the slow path, a run of plain characters is copied with one array copy instead of one call per character.
Objects and arrays. An object with up to 8 fields keeps its keys and values in two flat arrays and only moves to a LinkedHashMap if it grows past that, which removed 45.7% of parse allocations on larger payloads. Arrays dropped ArrayList for a plain JsonNode[], which also removes a cast on every access.

Hot loops. The input buffer is a field, so every buf[ptr] in the inner loops reloaded it. We now copy it into a local first (final char[] b = buf) in skipWhitespace, parseObject, and parseArray, the paths that read the buffer repeatedly. skipWhitespace also returns as soon as it sees a character above a space, so {, quotes, and digits skip the whitespace checks.
// before: reloads the field on every access
while (buf[ptr] <= ' ') ptr++;
// after: local copy, which lets the JIT keep it in a register
final char[] b = buf;
while (b[ptr] <= ' ') ptr++;
Beyond parsing
Parsing was only part of the gap. Object conversion, serialization, and output formatting all had the same problem: repeated work and a lot of short-lived garbage.
Object conversion. The JSON-to-POJO path used to rediscover fields and constructors by reflection on every call. Class metadata is now computed once per class and cached. The converter also handles records, the common java.time types, and generic collections without SimpleDateFormat.
Serialization took the most work. In the stringify profile, each object built its JSON text field by field, concatenating strings as it went, so every field produced around three short-lived String objects before the result came together. As the chart shows, byte[] and String together made up almost 90% of what stringify allocated, nearly all of it thrown away right after. We changed it to write the whole tree into one pre-sized buffer and escape in place. The output string is now the only large allocation left.
![Bar chart of where stringify allocated memory before the change: byte[] 64.7%, String 25.1%, other 10.2%](https://cdn.prod.website-files.com/69e786e187c925d08ee02b92/6b104e54de20b41dae634271_6b104da8dc24ebc4aedd8b65_laxjson4j-10-stringify-allocations.png)
Pretty-print and flatten had the same allocation pattern. Pretty-print built a new string for every indent level, so it now writes into one StringBuilder and reuses cached indent strings. Flatten built a new path string at every level, so it now appends to one StringBuilder and rewinds it on the way back out. On the 97 KB document, pretty-print went from 1607.9 µs to 134.0 µs, and flatten from 1083.1 µs to 190.7 µs.
How we measured
Every benchmark ran under JMH, one operation at a time (parse, stringify, pretty-print, flatten) for each parser, with proper compiler warmup. We measured single-thread latency and multi-thread throughput on three inputs, up to a 3.5 MB document, against Jackson 2.18.2, Gson 2.10.1, Fastjson2 2.0.56, and Hjson 3.1.0. The full configuration is in the note under the results.
A benchmark tells you what is slow, but not why. For the why, we used async-profiler and pprof to look at CPU and allocations, so every change below started with evidence.
Results
Everything below comes from our JMH suite. Each ratio compares the other parser against laxjson4j.
Single-thread parse, 97 KB document:

Multi-thread parse throughput, 3.5 MB document, all cores:

Internal improvements on a 97 KB document, before and after the optimization pass:

Benchmarks run with JMH 1.37 on a MacBook Pro (Apple M1 Pro, 10 cores: 8 performance + 2 efficiency). Each benchmark used 3 forks with 5 warmup and 5 measurement iterations of 3 s. Single-thread: 1 thread, 512 MB heap. Multi-thread: 10 threads (all cores), 1 GB heap.
Fastjson2 is faster on strict JSON, and we aren't trying to beat it there. A big part of its edge comes from leaning on sun.misc.Unsafe for raw memory and string access. That is an uncomfortable foundation for a security product, since unsafe operations have long attracted exploits and many enterprises restrict libraries that depend on them. laxjson4j stays in plain, safe Java. Fastjson2 also rejects the truncated and unquoted inputs this project exists for.
Against Jackson, Gson and Hjson we parse trees faster. In the multi-thread test, the pooled design keeps throughput steady where per-parse allocation lowers it for Jackson and Gson. On lenient input, only laxjson4j and, to a degree, Hjson (about 3.5x slower) can parse at all. For truncated input we don't know of another fast option.
How it held up on the request path
We also tested the parser in one of our inline request-inspection services. It sits on the WAF path, checks every request against security rules and has to answer allow or block within a strict time budget. We ran the same load (about 1.3k requests per second, 10 KB bodies), resources, and timeouts twice: once with our existing JSON engine and once with laxjson4j.

Throughput and the mix of decisions stayed the same, and neither run failed open. The gain is in the tail. p99 had been sitting right at the request timeout, at about 150 ms. With laxjson4j it dropped to about 70 ms, and decision timeouts fell from about 45 per second to about 2.6, roughly 17 times fewer. A profile of the service confirmed the JSON engine was the only difference between the two runs
What’s next?
The parser already ships in the shared security library our inline services are built on, so the results above come from the same code that runs on the WAF path. On the library side, we're working on configurable parser options and a byte[] input path so callers can skip the String conversion.
We'd like to hear from you. If you have a payload that doesn't read the way you expect, an idea for an extension, or a change you'd like to contribute, open an issue or a pull request on laxjson4j's Github page. Try fixJsonString on the strangest JSON you have and tell us how it goes.


