MSP-8292 Up deps

This commit is contained in:
Синельник Тимур Романович committed 2026-08-12 16:00:30 +03:00
1 parent 3a66223a89
commit 5daaea50a1
186 files changed
+7092 -1579

No files matched your search

+149 -65
View File
@@ -1,7 +1,26 @@
[![Go Report Card](https://goreportcard.com/badge/github.com/buger/jsonparser)](https://goreportcard.com/report/github.com/buger/jsonparser) ![License](https://img.shields.io/dub/l/vibe-d.svg)
[![Go Report Card](https://goreportcard.com/badge/github.com/buger/jsonparser)](https://goreportcard.com/report/github.com/buger/jsonparser) [![Audit](https://img.shields.io/badge/ReqProof-L3%20Assurance-success)](https://reqproof.com) ![License](https://img.shields.io/dub/l/vibe-d.svg)
# Alternative JSON parser for Go (10x times faster standard library)
It does not require you to know the structure of the payload (eg. create structs), and allows accessing fields by providing the path to them. It is up to **10 times faster** than standard `encoding/json` package (depending on payload size and usage), **allocates no memory**. See benchmarks below.
It does not require you to know the structure of the payload (eg. create structs), and allows accessing fields by providing the path to them. It is up to **6x faster** than standard `encoding/json` package (depending on payload size and usage), **allocates no memory**. See benchmarks below.
---
## 🔒 Formally Verified — the first Go library proven to L3 assurance by [ReqProof](https://reqproof.com)
jsonparser is the **reference case study** for [ReqProof](https://reqproof.com) — a git-native requirements-engineering and formal-verification platform. Every public API is traced to a formal requirement, every requirement is tested with **100% Modified Condition/Decision Coverage (MC/DC)**, and the entire parser is fuzzed by a custom **structure-aware JSON fuzzer** ([github.com/probelabs/json-fuzz](https://github.com/probelabs/json-fuzz)) that generates grammar-valid mutations at 250,000 inputs/second.
| Metric | Value |
|---|---|
| Requirements traced | 118 (7 stakeholder + 111 system) |
| Proof audit | **0 errors, 0 warnings** (L3 strict) |
| Code-level MC/DC | **100% decisions, 100% conditions** |
| Requirement-side MC/DC | **377/377 witness rows covered** |
| Fuzz executions | 16M+ (structure-aware + path-mutation + encoding/json differential) |
| Bugs found & fixed by the proof review | 7 (4 panics, 2 data-corruption, 1 encoding bug) |
The proof review caught bugs that years of community use, OSS-Fuzz, and standard fuzzing had missed — including a panic class across 8 unchecked-dereference sites, a silent data-loss bug in `Set`, and a malformed-output bug in `Delete`. [Read the full root-cause analysis →](docs/proof-gap-root-cause.md)
---
## Rationale
Originally I made this for a project that relies on a lot of 3rd party APIs that can be unpredictable and complex.
@@ -55,16 +74,16 @@ if value, err := jsonparser.GetInt(data, "company", "size"); err == nil {
size = value
}
// You can use `ArrayEach` helper to iterate items [item1, item2 .... itemN]
jsonparser.ArrayEach(data, func(value []byte, dataType jsonparser.ValueType, offset int, err error) {
// You can use `EachArray` helper to iterate items [item1, item2 .... itemN]
jsonparser.EachArray(data, func(value []byte, dataType jsonparser.ValueType, offset int, err error) {
fmt.Println(jsonparser.Get(value, "url"))
}, "person", "avatars")
// Or use can access fields by index!
jsonparser.GetString(data, "person", "avatars", "[0]", "url")
// You can use `ObjectEach` helper to iterate objects { "key1":object1, "key2":object2, .... "keyN":objectN }
jsonparser.ObjectEach(data, func(key []byte, value []byte, dataType jsonparser.ValueType, offset int) error {
// You can use `EachObject` helper to iterate objects { "key1":object1, "key2":object2, .... "keyN":objectN }
jsonparser.EachObject(data, func(key []byte, value []byte, dataType jsonparser.ValueType, offset int) error {
fmt.Printf("Key: '%s'\n Value: '%s'\n Type: %s\n", string(key), string(value), dataType)
return nil
}, "person", "name")
@@ -90,6 +109,72 @@ jsonparser.EachKey(data, func(idx int, value []byte, vt jsonparser.ValueType, er
// For more information see docs below
```
## Lenient Parsing
The package-level functions remain strict RFC 8259 parsers. For inputs that use
single-quoted strings or non-standard escapes, use a `Config` explicitly:
```go
data := []byte(`{'name':'Ada','role':'engineer'}`)
name, err := jsonparser.Lenient.GetString(data, "name")
// name == "Ada"
```
`Lenient` enables both compatibility options. You can also enable only the
extension your input requires:
```go
config := jsonparser.Config{AllowUnknownEscapes: true}
data := []byte("{\"path\":\"docs\\`draft\\x\"}")
path, err := config.GetString(data, "path")
// path == "docs`draftx"
```
The config-aware `Get`, `GetString`, `Set`, `Delete`, `ArrayEach`, and
`ObjectEach` methods share the same signatures and behavior as their
package-level counterparts apart from the enabled parsing extensions.
`jsonparser.DefaultConfig` is strict; `jsonparser.Lenient` enables
`AllowSingleQuotes` and `AllowUnknownEscapes`.
## Streaming
`ReaderParser` provides the same path-based lookup model for JSON read from an
`io.Reader`, so a large document does not need to be loaded into a single byte
slice:
```go
file, err := os.Open("large.json")
if err != nil {
log.Fatal(err)
}
defer file.Close()
parser := jsonparser.NewReaderParser(file)
name, err := parser.GetString("person", "name")
```
To process a root array incrementally, use `ArrayEach`. Each callback value is
valid for the duration of the callback; copy it if it must be retained:
```go
parser := jsonparser.NewReaderParser(file)
err := parser.ArrayEach(func(value []byte, valueType jsonparser.ValueType, err error) {
if err != nil {
return
}
process(value, valueType)
})
```
The parser reads in 64 KiB chunks and discards completed prefixes. Its default
sliding-window target is 64 MiB; customize it with
`jsonparser.Config{MaxBufferSize: size}`. A single returned value or array
element may exceed that target because its complete bytes are supplied to the
caller. Create a new `ReaderParser` for each lookup or array traversal.
`ReaderParser` also honors `AllowSingleQuotes` and `AllowUnknownEscapes`.
## Reference
Library API is really simple. You just need the `Get` method to perform any operation. The rest is just helpers around it.
@@ -146,15 +231,19 @@ func GetInt(data []byte, keys ...string) (val int64, err error)
If you know the key type, you can use the helpers above.
If key data type do not match, it will return error.
### **`ArrayEach`**
### **`EachArray`**
```go
func ArrayEach(data []byte, cb func(value []byte, dataType jsonparser.ValueType, offset int, err error), keys ...string)
func EachArray(data []byte, cb func(value []byte, dataType jsonparser.ValueType, offset int, err error), keys ...string)
```
Needed for iterating arrays, accepts a callback function with the same return arguments as `Get`.
`ArrayEach` remains available as a backward-compatible alias.
The error-returning and wildcard variants follow the same naming convention:
use `EachArrayErr` and `EachArrayWildcard`; `ArrayEachErr` and
`ArrayEachWildcard` remain available for backward compatibility.
### **`ObjectEach`**
### **`EachObject`**
```go
func ObjectEach(data []byte, callback func(key []byte, value []byte, dataType ValueType, offset int) error, keys ...string) (err error)
func EachObject(data []byte, callback func(key []byte, value []byte, dataType ValueType, offset int) error, keys ...string) (err error)
```
Needed for iterating object, accepts a callback function. Example:
```go
@@ -162,8 +251,9 @@ var handler func([]byte, []byte, jsonparser.ValueType, int) error
handler = func(key []byte, value []byte, dataType jsonparser.ValueType, offset int) error {
//do stuff here
}
jsonparser.ObjectEach(myJson, handler)
jsonparser.EachObject(myJson, handler)
```
`ObjectEach` remains available as a backward-compatible alias.
### **`EachKey`**
@@ -224,6 +314,20 @@ Accepts multiple keys to specify path to JSON value (in case of updating or crea
Note that keys can be an array indexes: `jsonparser.Delete(data, "person", "avatars", "[0]", "url")`
### **`Append`**
```go
func Append(data []byte, value []byte, keys ...string) ([]byte, error)
```
Appends `value` to the end of the JSON array addressed by `keys`. When `keys` is
empty, `Append` addresses the top-level value. If a keyed path does not exist,
`Append` creates it as a single-element array using `Set`'s auto-vivification
behavior. Returns `MalformedArrayError` if the addressed value is not an array.
```go
// Append to an array without knowing its length
data, _ = jsonparser.Append(data, []byte(`"new_item"`), "items")
```
## What makes it so fast?
* It does not rely on `encoding/json`, `reflection` or `interface{}`, the only real package dependency is `bytes`.
@@ -236,11 +340,12 @@ Note that keys can be an array indexes: `jsonparser.Delete(data, "person", "avat
There are 3 benchmark types, trying to simulate real-life usage for small, medium and large JSON payloads.
For each metric, the lower value is better. Time/op is in nanoseconds. Values better than standard encoding/json marked as bold text.
Benchmarks run on standard Linode 1024 box.
> **Methodology:** Benchmarks run with `go test -bench=. -benchmem -count=5` on Apple M4 Max (ARM64, darwin), Go 1.26.3. Median of 5 runs. All comparison libraries updated to their latest versions as of 2026-07-28. The `encoding/json` benchmark types no longer carry ffjson-generated `UnmarshalJSON` methods (fixed in v1.3.1, issue #126), so these numbers reflect the real standard library, not generated code.
Compared libraries:
* https://golang.org/pkg/encoding/json
* https://github.com/Jeffail/gabs
* https://github.com/Jeffail/gabs/v2
* https://github.com/a8m/djson
* https://github.com/bitly/go-simplejson
* https://github.com/antonholmquist/jason
@@ -251,42 +356,34 @@ Compared libraries:
* https://github.com/buger/jsonparser
#### TLDR
If you want to skip next sections we have 2 winner: `jsonparser` and `easyjson`.
`jsonparser` is up to 10 times faster than standard `encoding/json` package (depending on payload size and usage), and almost infinitely (literally) better in memory consumption because it operates with data on byte level, and provide direct slice pointers.
`easyjson` wins in CPU in medium tests and frankly i'm impressed with this package: it is remarkable results considering that it is almost drop-in replacement for `encoding/json` (require some code generation).
It's hard to fully compare `jsonparser` and `easyjson` (or `ffson`), they a true parsers and fully process record, unlike `jsonparser` which parse only keys you specified.
`jsonparser` is up to **6.4x faster** than standard `encoding/json` package (depending on payload size and usage), and **zero allocations** — it operates with data on byte level, and provide direct slice pointers. On large payloads, `jsonparser` is also faster than `easyjson` and `ffjson`.
If you searching for replacement of `encoding/json` while keeping structs, `easyjson` is an amazing choice. If you want to process dynamic JSON, have memory constrains, or more control over your data you should try `jsonparser`.
`jsonparser` performance heavily depends on usage, and it works best when you do not need to process full record, only some keys. The more calls you need to make, the slower it will be, in contrast `easyjson` (or `ffjson`, `encoding/json`) parser record only 1 time, and then you can make as many calls as you want.
With great power comes great responsibility! :)
#### Small payload
Each test processes 190 bytes of http log as a JSON record.
It should read multiple fields.
https://github.com/buger/jsonparser/blob/master/benchmark/benchmark_small_payload_test.go
Library | time/op | bytes/op | allocs/op
------ | ------- | -------- | -------
encoding/json struct | 7879 | 880 | 18
encoding/json interface{} | 8946 | 1521 | 38
Jeffail/gabs | 10053 | 1649 | 46
bitly/go-simplejson | 10128 | 2241 | 36
antonholmquist/jason | 27152 | 7237 | 101
github.com/ugorji/go/codec | 8806 | 2176 | 31
mreiferson/go-ujson | **7008** | **1409** | 37
a8m/djson | 3862 | 1249 | 30
pquerna/ffjson | **3769** | **624** | **15**
mailru/easyjson | **2002** | **192** | **9**
buger/jsonparser | **1367** | **0** | **0**
buger/jsonparser (EachKey API) | **809** | **0** | **0**
| Library | time/op | bytes/op | allocs/op |
| ------ | ------- | -------- | ------- |
| encoding/json struct | 1335 | 415 | 9 |
| encoding/json interface{} | 1441 | 1336 | 30 |
| Jeffail/gabs | 1558 | 1464 | 38 |
| bitly/go-simplejson | 1594 | 2120 | 34 |
| github.com/ugorji/go/codec | 1286 | 1288 | 13 |
| antonholmquist/jason | 4354 | 6816 | 105 |
| mreiferson/go-ujson | 1031 | 1392 | 36 |
| a8m/djson | 880 | 1160 | 25 |
| pquerna/ffjson | **652** | **520** | **10** |
| mailru/easyjson | **312** | **80** | **4** |
| buger/jsonparser (ObjectEach) | **214** | 64 | 2 |
| buger/jsonparser (EachKey) | **241** | **0** | **0** |
| buger/jsonparser (Get) | **382** | **0** | **0** |
Winners are ffjson, easyjson and jsonparser, where jsonparser is up to 9.8x faster than encoding/json and 4.6x faster than ffjson, and slightly faster than easyjson.
If you look at memory allocation, jsonparser has no rivals, as it makes no data copy and operates with raw []byte structures and pointers to it.
`jsonparser` with `EachKey` is **5.5x faster** than `encoding/json` and **zero allocations**.
#### Medium payload
@@ -297,28 +394,17 @@ https://github.com/buger/jsonparser/blob/master/benchmark/benchmark_medium_paylo
| Library | time/op | bytes/op | allocs/op |
| ------- | ------- | -------- | --------- |
| encoding/json struct | 57749 | 1336 | 29 |
| encoding/json interface{} | 79297 | 10627 | 215 |
| Jeffail/gabs | 83807 | 11202 | 235 |
| bitly/go-simplejson | 88187 | 17187 | 220 |
| antonholmquist/jason | 94099 | 19013 | 247 |
| github.com/ugorji/go/codec | 114719 | 6712 | 152 |
| mreiferson/go-ujson | **56972** | 11547 | 270 |
| a8m/djson | 28525 | 10196 | 198 |
| pquerna/ffjson | **20298** | **856** | **20** |
| mailru/easyjson | **10512** | **336** | **12** |
| buger/jsonparser | **15955** | **0** | **0** |
| buger/jsonparser (EachKey API) | **8916** | **0** | **0** |
The difference between ffjson and jsonparser in CPU usage is smaller, while the memory consumption difference is growing. On the other hand `easyjson` shows remarkable performance for medium payload.
`gabs`, `go-simplejson` and `jason` are based on encoding/json and map[string]interface{} and actually only helpers for unstructured JSON, their performance correlate with `encoding/json interface{}`, and they will skip next round.
`go-ujson` while have its own parser, shows same performance as `encoding/json`, also skips next round. Same situation with `ugorji/go/codec`, but it showed unexpectedly bad performance for complex payloads.
| encoding/json struct | 10564 | 616 | 18 |
| pquerna/ffjson | 3962 | 736 | 15 |
| mailru/easyjson | **2444** | **216** | **7** |
| buger/jsonparser (Get) | **3894** | **0** | **0** |
| buger/jsonparser (EachKey) | **1923** | **0** | **0** |
`jsonparser` with `EachKey` beats `easyjson` on medium payload and remains zero-allocation.
#### Large payload
Each test processes a 24kb JSON record (based on Discourse API)
Each test processes a 24kb JSON record (based on Discourse API).
It should read 2 arrays, and for each item in array get a few fields.
Basically it means processing a full JSON file.
@@ -326,16 +412,14 @@ https://github.com/buger/jsonparser/blob/master/benchmark/benchmark_large_payloa
| Library | time/op | bytes/op | allocs/op |
| --- | --- | --- | --- |
| encoding/json struct | 748336 | 8272 | 307 |
| encoding/json interface{} | 1224271 | 215425 | 3395 |
| a8m/djson | 510082 | 213682 | 2845 |
| pquerna/ffjson | **312271** | **7792** | **298** |
| mailru/easyjson | **154186** | **6992** | **288** |
| buger/jsonparser | **85308** | **0** | **0** |
| encoding/json struct | 134123 | 4432 | 147 |
| encoding/json interface{} | 176246 | 135824 | 2814 |
| a8m/djson | 99295 | 135136 | 2679 |
| pquerna/ffjson | 60129 | 4822 | 144 |
| mailru/easyjson | 32765 | 4016 | 134 |
| buger/jsonparser | **20788** | **0** | **0** |
`jsonparser` now is a winner, but do not forget that it is way more lightweight parser than `ffson` or `easyjson`, and they have to parser all the data, while `jsonparser` parse only what you need. All `ffjson`, `easysjon` and `jsonparser` have their own parsing code, and does not depend on `encoding/json` or `interface{}`, thats one of the reasons why they are so fast. `easyjson` also use a bit of `unsafe` package to reduce memory consuption (in theory it can lead to some unexpected GC issue, but i did not tested enough)
Also last benchmark did not included `EachKey` test, because in this particular case we need to read lot of Array values, and using `ArrayEach` is more efficient.
`jsonparser` is the **fastest library overall** on large payloads: **6.4x faster than encoding/json**, **1.6x faster than easyjson**, and **zero allocations**.
## Formal Verification