encoding/json: Add custom JSON package with build tag support for Sonic (#1623)

* tag optional sonic and allow full library conversion

* Add workflow and disallow arm and darwin usage

* Add basic hotswap benchmark

* linter: fix

* use bash

* linter: fix?

* Fix whoopsie, add to make file, also add mention in features list.

* test enforcement

* actually read documentation see if this works

* linter: fix

* linter: fix

* sonic: bump tagged version

* encoding/json: drop build tag arch and os filters

* encoding/json: consolidate tests

* encoding/json: log build tag usage

* rm superfluous builds

* glorious/nits: add template change and regen docs

* glorious/nits: update commentary on nolint directive

* glorious/nits: rm init func and log results in main.go

* Test to actually pull flag in

* linter: fix

* thrasher: nits

* gk: nits 4 goflags goooooooooo!

* gk: nits rn

* make sonic default json implementation

* screen 386

* linter: fix

* Add commentary

* glorious: nits Makefile not working

* gk: nits

* gk: nits whoops

* whoopsirino

* mention 32bit systems won't be sonic

* gk: super-duper nit of extremes

---------

Co-authored-by: Ryan O'Hara-Reid <ryan.oharareid@thrasher.io>
This commit is contained in:
Ryan O'Hara-Reid
2025-02-20 16:05:55 +11:00
committed by GitHub
parent 3748c97b12
commit e99adca86f
156 changed files with 321 additions and 170 deletions

View File

@@ -0,0 +1,12 @@
package json
import "testing"
// BenchmarkUnmarshal-16 838503 1282 ns/op 816 B/op 24 allocs/op (encoding/json)
// BenchmarkUnmarshal-16 1859184 653.3 ns/op 900 B/op 18 allocs/op (bytedance/sonic) Usage: go test --tags=sonic -bench=BenchmarkUnmarshal -v
func BenchmarkUnmarshal(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_ = Unmarshal([]byte(`{"Name":"Wednesday","Age":6,"Parents":["Gomez","Morticia"]}`), &map[string]interface{}{})
}
}

16
encoding/json/common.go Normal file
View File

@@ -0,0 +1,16 @@
// json is an abstraction middleware package to allow switching between json encoder/decoder implementations
// The default implementation is sonic.
// Build with `sonic_off` or `386` tags to switch to golang.org/encoding/json.
package json
import "encoding/json" //nolint:depguard // Acceptable use in gct json wrapper
type (
// RawMessage is a raw encoded JSON value.
// It implements [Marshaler] and [Unmarshaler] and can
// be used to delay JSON decoding or precompute a JSON encoding.
RawMessage = json.RawMessage
// An UnmarshalTypeError describes a JSON value that was
// not appropriate for a value of a specific Go type.
UnmarshalTypeError = json.UnmarshalTypeError
)

23
encoding/json/json.go Normal file
View File

@@ -0,0 +1,23 @@
//go:build sonic_off || 386
package json
import "encoding/json" //nolint:depguard // Acceptable use in gct json wrapper
// Implementation is a constant string that represents the current JSON implementation package
const Implementation = "encoding/json"
var (
// Marshal returns the JSON encoding of v. See the "encoding/json" documentation for Marshal
Marshal = json.Marshal
// Unmarshal parses the JSON-encoded data and stores the result in the value pointed to by v. See the "encoding/json" documentation for Unmarshal
Unmarshal = json.Unmarshal
// NewEncoder returns a new encoder that writes to w. See the "encoding/json" documentation for NewEncoder
NewEncoder = json.NewEncoder
// NewDecoder returns a new decoder that reads from r. See the "encoding/json" documentation for NewDecoder
NewDecoder = json.NewDecoder
// MarshalIndent is like Marshal but applies Indent to format the output. See the "encoding/json" documentation for MarshalIndent
MarshalIndent = json.MarshalIndent
// Valid reports whether data is a valid JSON encoding. See the "encoding/json" documentation for Valid
Valid = json.Valid
)

25
encoding/json/sonic.go Normal file
View File

@@ -0,0 +1,25 @@
//go:build !sonic_off && !386
package json
import (
"github.com/bytedance/sonic"
)
// Implementation is a constant string that represents the current JSON implementation package
const Implementation = "bytedance/sonic"
var (
// Marshal returns the JSON encoding of v. See the "github.com/bytedance/sonic" documentation for Marshal
Marshal = sonic.ConfigStd.Marshal
// Unmarshal parses the JSON-encoded data and stores the result in the value pointed to by v. See the "github.com/bytedance/sonic" documentation for Unmarshal
Unmarshal = sonic.ConfigStd.Unmarshal
// NewEncoder returns a new encoder that writes to w. See the "github.com/bytedance/sonic" documentation for NewEncoder
NewEncoder = sonic.ConfigStd.NewEncoder
// NewDecoder returns a new decoder that reads from r. See the "github.com/bytedance/sonic" documentation for NewDecoder
NewDecoder = sonic.ConfigStd.NewDecoder
// MarshalIndent is like Marshal but applies Indent to format the output. See the "github.com/bytedance/sonic" documentation for MarshalIndent
MarshalIndent = sonic.ConfigStd.MarshalIndent
// Valid reports whether data is a valid JSON encoding. See the "github.com/bytedance/sonic" documentation for Valid
Valid = sonic.ConfigStd.Valid
)