| 1 |
//go:build go1.27 && goexperiment.jsonv2 |
| 2 |
|
| 3 |
package benchmark |
| 4 |
|
| 5 |
import ( |
| 6 |
stdjson "encoding/json" |
| 7 |
"encoding/json/jsontext" |
| 8 |
jsonv2 "encoding/json/v2" |
| 9 |
|
| 10 |
"benchmark/report" |
| 11 |
|
| 12 |
"github.com/bytedance/sonic" |
| 13 |
gojson "github.com/goccy/go-json" |
| 14 |
jsoniter "github.com/json-iterator/go" |
| 15 |
segmentio "github.com/segmentio/encoding/json" |
| 16 |
) |
| 17 |
|
| 18 |
// The configurations of encoding/json/v2, where Go has it, and the category of its behavior. |
| 19 |
// |
| 20 |
// encoding/json/v2 is in every category: with the options of the behavior of encoding/json ( DefaultOptionsV1 ) in |
| 21 |
// "std", with those and the ones which drop HTML escaping and key sorting in "fast", and with its default options, |
| 22 |
// the baseline of "v2". There, the other libraries are configured as close to it as their options allow, and |
| 23 |
// encoding/json/v2 is also shown at its fastest, with the checks of duplicate keys and of UTF-8 turned off. |
| 24 |
|
| 25 |
func v2Marshal(opts ...jsonv2.Options) func(any) ([]byte, error) { |
| 26 |
return func(v any) ([]byte, error) { return jsonv2.Marshal(v, opts...) } |
| 27 |
} |
| 28 |
|
| 29 |
func v2Unmarshal(opts ...jsonv2.Options) func([]byte) func(any) error { |
| 30 |
return stdUnmarshal(func(data []byte, v any) error { return jsonv2.Unmarshal(data, v, opts...) }) |
| 31 |
} |
| 32 |
|
| 33 |
var ( |
| 34 |
sonicV2 = sonic.Config{CompactMarshaler: true, CopyString: true, ValidateString: true, NoNullSliceOrMap: true, CaseSensitive: true, UseUnicodeErrors: true}.Froze() |
| 35 |
jsoniterV2 = jsoniter.Config{EscapeHTML: false, SortMapKeys: false, CaseSensitive: true, ValidateJsonRawMessage: true}.Froze() |
| 36 |
) |
| 37 |
|
| 38 |
func init() { |
| 39 |
v1 := stdjson.DefaultOptionsV1() |
| 40 |
std := &reportConfig{ |
| 41 |
Config: report.Config{ID: "std/encoding/json/v2", Library: "encoding/json/v2", Category: "std", |
| 42 |
Title: "encoding/json/v2", Setting: "encoding/json/v2 with json.DefaultOptionsV1()"}, |
| 43 |
module: "std", marshal: v2Marshal(v1), unmarshal: v2Unmarshal(v1), |
| 44 |
} |
| 45 |
fast := &reportConfig{ |
| 46 |
Config: report.Config{ID: "fast/encoding/json/v2", Library: "encoding/json/v2", Category: "fast", |
| 47 |
Title: "encoding/json/v2", Setting: "encoding/json/v2 with json.DefaultOptionsV1(), jsontext.EscapeForHTML( false ), json.Deterministic( false )"}, |
| 48 |
module: "std", |
| 49 |
marshal: v2Marshal(v1, jsontext.EscapeForHTML(false), jsonv2.Deterministic(false)), |
| 50 |
unmarshal: v2Unmarshal(v1), |
| 51 |
} |
| 52 |
fastest := jsonv2.JoinOptions(jsontext.AllowDuplicateNames(true), jsontext.AllowInvalidUTF8(true)) |
| 53 |
v2 := []*reportConfig{ |
| 54 |
{ |
| 55 |
Config: report.Config{ID: "v2/encoding/json/v2", Library: "encoding/json/v2", Category: "v2", Title: "encoding/json/v2", |
| 56 |
Setting: "json.Marshal / json.Unmarshal of encoding/json/v2"}, |
| 57 |
module: "std", marshal: v2Marshal(), unmarshal: v2Unmarshal(), |
| 58 |
}, |
| 59 |
{ |
| 60 |
Config: report.Config{ID: "v2/encoding/json/v2/fastest", Library: "encoding/json/v2", Category: "v2", Title: "encoding/json/v2 ( fastest )", |
| 61 |
Setting: "encoding/json/v2 with jsontext.AllowDuplicateNames( true ), jsontext.AllowInvalidUTF8( true )"}, |
| 62 |
module: "std", marshal: v2Marshal(fastest), unmarshal: v2Unmarshal(fastest), |
| 63 |
}, |
| 64 |
{ |
| 65 |
Config: report.Config{ID: "v2/encoding/json", Library: "encoding/json", Category: "v2", Title: "encoding/json", |
| 66 |
Setting: "json.Encoder with SetEscapeHTML( false ) / json.Unmarshal ( no option for the rest )"}, |
| 67 |
module: "std", marshal: stdMarshalNoHTMLEscape, unmarshal: stdUnmarshal(stdjson.Unmarshal), |
| 68 |
}, |
| 69 |
{ |
| 70 |
Config: report.Config{ID: "v2/go-json", Library: "goccy/go-json", Category: "v2", Title: "goccy/go-json", |
| 71 |
Setting: "json.MarshalWithOption( DisableHTMLEscape, UnorderedMap ) / json.Unmarshal ( no option for the rest )"}, |
| 72 |
module: goJSONModule, marshal: goJSONMarshal(gojson.DisableHTMLEscape(), gojson.UnorderedMap()), unmarshal: goJSONUnmarshal(), |
| 73 |
}, |
| 74 |
{ |
| 75 |
Config: report.Config{ID: "v2/go-json/of", Library: "goccy/go-json", Category: "v2", Title: "goccy/go-json ( UnmarshalOf )", |
| 76 |
Setting: "json.UnmarshalOf, which takes the value by its type ( decode only )"}, |
| 77 |
module: goJSONModule, decodeOf: []gojson.DecodeOptionFunc{}, |
| 78 |
}, |
| 79 |
{ |
| 80 |
Config: report.Config{ID: "v2/sonic", Library: "bytedance/sonic", Category: "v2", Title: "bytedance/sonic", |
| 81 |
Setting: "sonic.Config{ CompactMarshaler, CopyString, ValidateString, NoNullSliceOrMap, CaseSensitive, UseUnicodeErrors: true }"}, |
| 82 |
module: sonicModule, marshal: sonicV2.Marshal, unmarshal: stdUnmarshal(sonicV2.Unmarshal), |
| 83 |
}, |
| 84 |
{ |
| 85 |
Config: report.Config{ID: "v2/jsoniter", Library: "json-iterator/go", Category: "v2", Title: "json-iterator/go", |
| 86 |
Setting: "jsoniter.Config{ EscapeHTML: false, SortMapKeys: false, CaseSensitive: true, ValidateJsonRawMessage: true }"}, |
| 87 |
module: jsoniterModule, marshal: jsoniterV2.Marshal, unmarshal: stdUnmarshal(jsoniterV2.Unmarshal), |
| 88 |
}, |
| 89 |
{ |
| 90 |
Config: report.Config{ID: "v2/segmentio", Library: "segmentio/encoding", Category: "v2", Title: "segmentio/encoding", |
| 91 |
Setting: "json.Append( 0 ) / json.Parse( DontMatchCaseInsensitiveStructFields )"}, |
| 92 |
module: segmentioModule, marshal: segmentioMarshal(0), unmarshal: segmentioUnmarshal(segmentio.DontMatchCaseInsensitiveStructFields), |
| 93 |
}, |
| 94 |
} |
| 95 |
insertAfter("std/encoding/json", std) |
| 96 |
insertAfter("fast/encoding/json", fast) |
| 97 |
insertAfter("fastest/encoding/json", &reportConfig{ |
| 98 |
Config: report.Config{ID: "fastest/encoding/json/v2", Library: "encoding/json/v2", Category: "fastest", Title: "encoding/json/v2", |
| 99 |
Setting: "encoding/json/v2 with jsontext.AllowDuplicateNames( true ), jsontext.AllowInvalidUTF8( true )"}, |
| 100 |
module: "std", marshal: v2Marshal(fastest), unmarshal: v2Unmarshal(fastest), |
| 101 |
}) |
| 102 |
reportConfigs = append(reportConfigs, v2...) |
| 103 |
// after the category of encoding/json |
| 104 |
reportCategories = append(reportCategories[:1], append([]report.Category{{ |
| 105 |
ID: "v2", |
| 106 |
Title: "Same behavior as encoding/json/v2", |
| 107 |
Description: "The default behavior of encoding/json/v2: HTML is not escaped, the keys of a map are in any order, " + |
| 108 |
"a nil slice or map is encoded as [] or {}, invalid UTF-8 and duplicate keys are errors, and the keys match the " + |
| 109 |
"fields by their case. The other libraries are configured as close to it as their options allow, and " + |
| 110 |
"encoding/json/v2 is also shown at its fastest, without the checks of duplicate keys and of UTF-8.", |
| 111 |
Baseline: "v2/encoding/json/v2", |
| 112 |
}}, reportCategories[1:]...)...) |
| 113 |
} |
| 114 |
|
| 115 |
// insertAfter inserts the configuration after the one of the ID. |
| 116 |
func insertAfter(id string, c *reportConfig) { |
| 117 |
for i, x := range reportConfigs { |
| 118 |
if x.ID == id { |
| 119 |
reportConfigs = append(reportConfigs[:i+1], append([]*reportConfig{c}, reportConfigs[i+1:]...)...) |
| 120 |
return |
| 121 |
} |
| 122 |
} |
| 123 |
panic("no configuration " + id) |
| 124 |
} |