mirror of
https://github.com/json-iterator/go.git
synced 2024-11-27 08:30:57 +02:00
consolidate slice tests
This commit is contained in:
parent
48a4a1e4db
commit
bd4e013f98
@ -1,152 +0,0 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
fuzz "github.com/google/gofuzz"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
func Test_Roundtrip(t *testing.T) {
|
||||
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
|
||||
for i := 0; i < 100; i++ {
|
||||
var before typeForTest
|
||||
fz.Fuzz(&before)
|
||||
|
||||
jbStd, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with stdlib: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbStd))) == 0 {
|
||||
t.Fatal("stdlib marshal produced empty result and no error")
|
||||
}
|
||||
jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with jsoniter: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbIter))) == 0 {
|
||||
t.Fatal("jsoniter marshal produced empty result and no error")
|
||||
}
|
||||
if string(jbStd) != string(jbIter) {
|
||||
t.Fatalf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
|
||||
indent(jbStd, " "), indent(jbIter, " "), dump(before))
|
||||
}
|
||||
|
||||
var afterStd typeForTest
|
||||
err = json.Unmarshal(jbIter, &afterStd)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
var afterIter typeForTest
|
||||
err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||
t.Fatalf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
|
||||
dump(afterStd), dump(afterIter), indent(jbIter, " "))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const indentStr = "> "
|
||||
|
||||
func fingerprint(obj interface{}) string {
|
||||
c := spew.ConfigState{
|
||||
SortKeys: true,
|
||||
SpewKeys: true,
|
||||
}
|
||||
return c.Sprintf("%v", obj)
|
||||
}
|
||||
|
||||
func dump(obj interface{}) string {
|
||||
cfg := spew.ConfigState{
|
||||
Indent: indentStr,
|
||||
}
|
||||
return cfg.Sdump(obj)
|
||||
}
|
||||
|
||||
func indent(src []byte, prefix string) string {
|
||||
var buf bytes.Buffer
|
||||
err := json.Indent(&buf, src, prefix, indentStr)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("!!! %v", err)
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var obj typeForTest
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&obj)
|
||||
for i := 0; i < t.N; i++ {
|
||||
jb, err := fn(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var before typeForTest
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&before)
|
||||
jb, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal: %v", err)
|
||||
}
|
||||
|
||||
for i := 0; i < t.N; i++ {
|
||||
var after typeForTest
|
||||
err = fn(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStandardMarshal(t *testing.B) {
|
||||
benchmarkMarshal(t, "stdlib", json.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "stdlib", json.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalFastest(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalFastest(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalDefault(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalDefault(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalCompatible(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal)
|
||||
}
|
@ -1,3 +0,0 @@
|
||||
package test
|
||||
|
||||
type typeForTest [4][4]bool
|
@ -1,152 +0,0 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
fuzz "github.com/google/gofuzz"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
func Test_Roundtrip(t *testing.T) {
|
||||
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
|
||||
for i := 0; i < 100; i++ {
|
||||
var before typeForTest
|
||||
fz.Fuzz(&before)
|
||||
|
||||
jbStd, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with stdlib: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbStd))) == 0 {
|
||||
t.Fatal("stdlib marshal produced empty result and no error")
|
||||
}
|
||||
jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with jsoniter: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbIter))) == 0 {
|
||||
t.Fatal("jsoniter marshal produced empty result and no error")
|
||||
}
|
||||
if string(jbStd) != string(jbIter) {
|
||||
t.Fatalf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
|
||||
indent(jbStd, " "), indent(jbIter, " "), dump(before))
|
||||
}
|
||||
|
||||
var afterStd typeForTest
|
||||
err = json.Unmarshal(jbIter, &afterStd)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
var afterIter typeForTest
|
||||
err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||
t.Fatalf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
|
||||
dump(afterStd), dump(afterIter), indent(jbIter, " "))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const indentStr = "> "
|
||||
|
||||
func fingerprint(obj interface{}) string {
|
||||
c := spew.ConfigState{
|
||||
SortKeys: true,
|
||||
SpewKeys: true,
|
||||
}
|
||||
return c.Sprintf("%v", obj)
|
||||
}
|
||||
|
||||
func dump(obj interface{}) string {
|
||||
cfg := spew.ConfigState{
|
||||
Indent: indentStr,
|
||||
}
|
||||
return cfg.Sdump(obj)
|
||||
}
|
||||
|
||||
func indent(src []byte, prefix string) string {
|
||||
var buf bytes.Buffer
|
||||
err := json.Indent(&buf, src, prefix, indentStr)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("!!! %v", err)
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var obj typeForTest
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&obj)
|
||||
for i := 0; i < t.N; i++ {
|
||||
jb, err := fn(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var before typeForTest
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&before)
|
||||
jb, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal: %v", err)
|
||||
}
|
||||
|
||||
for i := 0; i < t.N; i++ {
|
||||
var after typeForTest
|
||||
err = fn(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStandardMarshal(t *testing.B) {
|
||||
benchmarkMarshal(t, "stdlib", json.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "stdlib", json.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalFastest(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalFastest(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalDefault(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalDefault(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalCompatible(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal)
|
||||
}
|
@ -1,3 +0,0 @@
|
||||
package test
|
||||
|
||||
type typeForTest [4][4]byte
|
@ -1,152 +0,0 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
fuzz "github.com/google/gofuzz"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
func Test_Roundtrip(t *testing.T) {
|
||||
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
|
||||
for i := 0; i < 100; i++ {
|
||||
var before typeForTest
|
||||
fz.Fuzz(&before)
|
||||
|
||||
jbStd, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with stdlib: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbStd))) == 0 {
|
||||
t.Fatal("stdlib marshal produced empty result and no error")
|
||||
}
|
||||
jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with jsoniter: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbIter))) == 0 {
|
||||
t.Fatal("jsoniter marshal produced empty result and no error")
|
||||
}
|
||||
if string(jbStd) != string(jbIter) {
|
||||
t.Fatalf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
|
||||
indent(jbStd, " "), indent(jbIter, " "), dump(before))
|
||||
}
|
||||
|
||||
var afterStd typeForTest
|
||||
err = json.Unmarshal(jbIter, &afterStd)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
var afterIter typeForTest
|
||||
err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||
t.Fatalf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
|
||||
dump(afterStd), dump(afterIter), indent(jbIter, " "))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const indentStr = "> "
|
||||
|
||||
func fingerprint(obj interface{}) string {
|
||||
c := spew.ConfigState{
|
||||
SortKeys: true,
|
||||
SpewKeys: true,
|
||||
}
|
||||
return c.Sprintf("%v", obj)
|
||||
}
|
||||
|
||||
func dump(obj interface{}) string {
|
||||
cfg := spew.ConfigState{
|
||||
Indent: indentStr,
|
||||
}
|
||||
return cfg.Sdump(obj)
|
||||
}
|
||||
|
||||
func indent(src []byte, prefix string) string {
|
||||
var buf bytes.Buffer
|
||||
err := json.Indent(&buf, src, prefix, indentStr)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("!!! %v", err)
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var obj typeForTest
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&obj)
|
||||
for i := 0; i < t.N; i++ {
|
||||
jb, err := fn(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var before typeForTest
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&before)
|
||||
jb, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal: %v", err)
|
||||
}
|
||||
|
||||
for i := 0; i < t.N; i++ {
|
||||
var after typeForTest
|
||||
err = fn(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStandardMarshal(t *testing.B) {
|
||||
benchmarkMarshal(t, "stdlib", json.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "stdlib", json.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalFastest(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalFastest(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalDefault(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalDefault(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalCompatible(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal)
|
||||
}
|
@ -1,3 +0,0 @@
|
||||
package test
|
||||
|
||||
type typeForTest [4][4]float64
|
@ -1,152 +0,0 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
fuzz "github.com/google/gofuzz"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
func Test_Roundtrip(t *testing.T) {
|
||||
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
|
||||
for i := 0; i < 100; i++ {
|
||||
var before typeForTest
|
||||
fz.Fuzz(&before)
|
||||
|
||||
jbStd, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with stdlib: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbStd))) == 0 {
|
||||
t.Fatal("stdlib marshal produced empty result and no error")
|
||||
}
|
||||
jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with jsoniter: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbIter))) == 0 {
|
||||
t.Fatal("jsoniter marshal produced empty result and no error")
|
||||
}
|
||||
if string(jbStd) != string(jbIter) {
|
||||
t.Fatalf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
|
||||
indent(jbStd, " "), indent(jbIter, " "), dump(before))
|
||||
}
|
||||
|
||||
var afterStd typeForTest
|
||||
err = json.Unmarshal(jbIter, &afterStd)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
var afterIter typeForTest
|
||||
err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||
t.Fatalf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
|
||||
dump(afterStd), dump(afterIter), indent(jbIter, " "))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const indentStr = "> "
|
||||
|
||||
func fingerprint(obj interface{}) string {
|
||||
c := spew.ConfigState{
|
||||
SortKeys: true,
|
||||
SpewKeys: true,
|
||||
}
|
||||
return c.Sprintf("%v", obj)
|
||||
}
|
||||
|
||||
func dump(obj interface{}) string {
|
||||
cfg := spew.ConfigState{
|
||||
Indent: indentStr,
|
||||
}
|
||||
return cfg.Sdump(obj)
|
||||
}
|
||||
|
||||
func indent(src []byte, prefix string) string {
|
||||
var buf bytes.Buffer
|
||||
err := json.Indent(&buf, src, prefix, indentStr)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("!!! %v", err)
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var obj typeForTest
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&obj)
|
||||
for i := 0; i < t.N; i++ {
|
||||
jb, err := fn(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var before typeForTest
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&before)
|
||||
jb, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal: %v", err)
|
||||
}
|
||||
|
||||
for i := 0; i < t.N; i++ {
|
||||
var after typeForTest
|
||||
err = fn(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStandardMarshal(t *testing.B) {
|
||||
benchmarkMarshal(t, "stdlib", json.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "stdlib", json.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalFastest(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalFastest(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalDefault(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalDefault(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalCompatible(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal)
|
||||
}
|
@ -1,3 +0,0 @@
|
||||
package test
|
||||
|
||||
type typeForTest [4][4]int32
|
@ -1,152 +0,0 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
fuzz "github.com/google/gofuzz"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
func Test_Roundtrip(t *testing.T) {
|
||||
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
|
||||
for i := 0; i < 100; i++ {
|
||||
var before typeForTest
|
||||
fz.Fuzz(&before)
|
||||
|
||||
jbStd, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with stdlib: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbStd))) == 0 {
|
||||
t.Fatal("stdlib marshal produced empty result and no error")
|
||||
}
|
||||
jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with jsoniter: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbIter))) == 0 {
|
||||
t.Fatal("jsoniter marshal produced empty result and no error")
|
||||
}
|
||||
if string(jbStd) != string(jbIter) {
|
||||
t.Fatalf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
|
||||
indent(jbStd, " "), indent(jbIter, " "), dump(before))
|
||||
}
|
||||
|
||||
var afterStd typeForTest
|
||||
err = json.Unmarshal(jbIter, &afterStd)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
var afterIter typeForTest
|
||||
err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||
t.Fatalf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
|
||||
dump(afterStd), dump(afterIter), indent(jbIter, " "))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const indentStr = "> "
|
||||
|
||||
func fingerprint(obj interface{}) string {
|
||||
c := spew.ConfigState{
|
||||
SortKeys: true,
|
||||
SpewKeys: true,
|
||||
}
|
||||
return c.Sprintf("%v", obj)
|
||||
}
|
||||
|
||||
func dump(obj interface{}) string {
|
||||
cfg := spew.ConfigState{
|
||||
Indent: indentStr,
|
||||
}
|
||||
return cfg.Sdump(obj)
|
||||
}
|
||||
|
||||
func indent(src []byte, prefix string) string {
|
||||
var buf bytes.Buffer
|
||||
err := json.Indent(&buf, src, prefix, indentStr)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("!!! %v", err)
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var obj typeForTest
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&obj)
|
||||
for i := 0; i < t.N; i++ {
|
||||
jb, err := fn(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var before typeForTest
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&before)
|
||||
jb, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal: %v", err)
|
||||
}
|
||||
|
||||
for i := 0; i < t.N; i++ {
|
||||
var after typeForTest
|
||||
err = fn(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStandardMarshal(t *testing.B) {
|
||||
benchmarkMarshal(t, "stdlib", json.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "stdlib", json.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalFastest(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalFastest(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalDefault(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalDefault(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalCompatible(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal)
|
||||
}
|
@ -1,3 +0,0 @@
|
||||
package test
|
||||
|
||||
type typeForTest [4][4]*string
|
@ -1,152 +0,0 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
fuzz "github.com/google/gofuzz"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
func Test_Roundtrip(t *testing.T) {
|
||||
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
|
||||
for i := 0; i < 100; i++ {
|
||||
var before typeForTest
|
||||
fz.Fuzz(&before)
|
||||
|
||||
jbStd, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with stdlib: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbStd))) == 0 {
|
||||
t.Fatal("stdlib marshal produced empty result and no error")
|
||||
}
|
||||
jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with jsoniter: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbIter))) == 0 {
|
||||
t.Fatal("jsoniter marshal produced empty result and no error")
|
||||
}
|
||||
if string(jbStd) != string(jbIter) {
|
||||
t.Fatalf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
|
||||
indent(jbStd, " "), indent(jbIter, " "), dump(before))
|
||||
}
|
||||
|
||||
var afterStd typeForTest
|
||||
err = json.Unmarshal(jbIter, &afterStd)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
var afterIter typeForTest
|
||||
err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||
t.Fatalf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
|
||||
dump(afterStd), dump(afterIter), indent(jbIter, " "))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const indentStr = "> "
|
||||
|
||||
func fingerprint(obj interface{}) string {
|
||||
c := spew.ConfigState{
|
||||
SortKeys: true,
|
||||
SpewKeys: true,
|
||||
}
|
||||
return c.Sprintf("%v", obj)
|
||||
}
|
||||
|
||||
func dump(obj interface{}) string {
|
||||
cfg := spew.ConfigState{
|
||||
Indent: indentStr,
|
||||
}
|
||||
return cfg.Sdump(obj)
|
||||
}
|
||||
|
||||
func indent(src []byte, prefix string) string {
|
||||
var buf bytes.Buffer
|
||||
err := json.Indent(&buf, src, prefix, indentStr)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("!!! %v", err)
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var obj typeForTest
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&obj)
|
||||
for i := 0; i < t.N; i++ {
|
||||
jb, err := fn(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var before typeForTest
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&before)
|
||||
jb, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal: %v", err)
|
||||
}
|
||||
|
||||
for i := 0; i < t.N; i++ {
|
||||
var after typeForTest
|
||||
err = fn(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStandardMarshal(t *testing.B) {
|
||||
benchmarkMarshal(t, "stdlib", json.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "stdlib", json.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalFastest(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalFastest(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalDefault(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalDefault(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalCompatible(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal)
|
||||
}
|
@ -1,3 +0,0 @@
|
||||
package test
|
||||
|
||||
type typeForTest [4][4]string
|
@ -1,152 +0,0 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
fuzz "github.com/google/gofuzz"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
func Test_Roundtrip(t *testing.T) {
|
||||
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
|
||||
for i := 0; i < 100; i++ {
|
||||
var before typeForTest
|
||||
fz.Fuzz(&before)
|
||||
|
||||
jbStd, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with stdlib: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbStd))) == 0 {
|
||||
t.Fatal("stdlib marshal produced empty result and no error")
|
||||
}
|
||||
jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with jsoniter: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbIter))) == 0 {
|
||||
t.Fatal("jsoniter marshal produced empty result and no error")
|
||||
}
|
||||
if string(jbStd) != string(jbIter) {
|
||||
t.Fatalf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
|
||||
indent(jbStd, " "), indent(jbIter, " "), dump(before))
|
||||
}
|
||||
|
||||
var afterStd typeForTest
|
||||
err = json.Unmarshal(jbIter, &afterStd)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
var afterIter typeForTest
|
||||
err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||
t.Fatalf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
|
||||
dump(afterStd), dump(afterIter), indent(jbIter, " "))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const indentStr = "> "
|
||||
|
||||
func fingerprint(obj interface{}) string {
|
||||
c := spew.ConfigState{
|
||||
SortKeys: true,
|
||||
SpewKeys: true,
|
||||
}
|
||||
return c.Sprintf("%v", obj)
|
||||
}
|
||||
|
||||
func dump(obj interface{}) string {
|
||||
cfg := spew.ConfigState{
|
||||
Indent: indentStr,
|
||||
}
|
||||
return cfg.Sdump(obj)
|
||||
}
|
||||
|
||||
func indent(src []byte, prefix string) string {
|
||||
var buf bytes.Buffer
|
||||
err := json.Indent(&buf, src, prefix, indentStr)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("!!! %v", err)
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var obj typeForTest
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&obj)
|
||||
for i := 0; i < t.N; i++ {
|
||||
jb, err := fn(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var before typeForTest
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&before)
|
||||
jb, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal: %v", err)
|
||||
}
|
||||
|
||||
for i := 0; i < t.N; i++ {
|
||||
var after typeForTest
|
||||
err = fn(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStandardMarshal(t *testing.B) {
|
||||
benchmarkMarshal(t, "stdlib", json.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "stdlib", json.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalFastest(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalFastest(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalDefault(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalDefault(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalCompatible(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal)
|
||||
}
|
@ -1,3 +0,0 @@
|
||||
package test
|
||||
|
||||
type typeForTest [4][4]uint8
|
@ -1,152 +0,0 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
fuzz "github.com/google/gofuzz"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
func Test_Roundtrip(t *testing.T) {
|
||||
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
|
||||
for i := 0; i < 100; i++ {
|
||||
var before typeForTest
|
||||
fz.Fuzz(&before)
|
||||
|
||||
jbStd, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with stdlib: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbStd))) == 0 {
|
||||
t.Fatal("stdlib marshal produced empty result and no error")
|
||||
}
|
||||
jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with jsoniter: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbIter))) == 0 {
|
||||
t.Fatal("jsoniter marshal produced empty result and no error")
|
||||
}
|
||||
if string(jbStd) != string(jbIter) {
|
||||
t.Fatalf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
|
||||
indent(jbStd, " "), indent(jbIter, " "), dump(before))
|
||||
}
|
||||
|
||||
var afterStd typeForTest
|
||||
err = json.Unmarshal(jbIter, &afterStd)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
var afterIter typeForTest
|
||||
err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||
t.Fatalf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
|
||||
dump(afterStd), dump(afterIter), indent(jbIter, " "))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const indentStr = "> "
|
||||
|
||||
func fingerprint(obj interface{}) string {
|
||||
c := spew.ConfigState{
|
||||
SortKeys: true,
|
||||
SpewKeys: true,
|
||||
}
|
||||
return c.Sprintf("%v", obj)
|
||||
}
|
||||
|
||||
func dump(obj interface{}) string {
|
||||
cfg := spew.ConfigState{
|
||||
Indent: indentStr,
|
||||
}
|
||||
return cfg.Sdump(obj)
|
||||
}
|
||||
|
||||
func indent(src []byte, prefix string) string {
|
||||
var buf bytes.Buffer
|
||||
err := json.Indent(&buf, src, prefix, indentStr)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("!!! %v", err)
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var obj typeForTest
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&obj)
|
||||
for i := 0; i < t.N; i++ {
|
||||
jb, err := fn(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var before typeForTest
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&before)
|
||||
jb, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal: %v", err)
|
||||
}
|
||||
|
||||
for i := 0; i < t.N; i++ {
|
||||
var after typeForTest
|
||||
err = fn(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStandardMarshal(t *testing.B) {
|
||||
benchmarkMarshal(t, "stdlib", json.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "stdlib", json.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalFastest(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalFastest(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalDefault(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalDefault(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalCompatible(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal)
|
||||
}
|
@ -1,3 +0,0 @@
|
||||
package test
|
||||
|
||||
type typeForTest []bool
|
@ -1,152 +0,0 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
fuzz "github.com/google/gofuzz"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
func Test_Roundtrip(t *testing.T) {
|
||||
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
|
||||
for i := 0; i < 100; i++ {
|
||||
var before typeForTest
|
||||
fz.Fuzz(&before)
|
||||
|
||||
jbStd, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with stdlib: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbStd))) == 0 {
|
||||
t.Fatal("stdlib marshal produced empty result and no error")
|
||||
}
|
||||
jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with jsoniter: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbIter))) == 0 {
|
||||
t.Fatal("jsoniter marshal produced empty result and no error")
|
||||
}
|
||||
if string(jbStd) != string(jbIter) {
|
||||
t.Fatalf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
|
||||
indent(jbStd, " "), indent(jbIter, " "), dump(before))
|
||||
}
|
||||
|
||||
var afterStd typeForTest
|
||||
err = json.Unmarshal(jbIter, &afterStd)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
var afterIter typeForTest
|
||||
err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||
t.Fatalf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
|
||||
dump(afterStd), dump(afterIter), indent(jbIter, " "))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const indentStr = "> "
|
||||
|
||||
func fingerprint(obj interface{}) string {
|
||||
c := spew.ConfigState{
|
||||
SortKeys: true,
|
||||
SpewKeys: true,
|
||||
}
|
||||
return c.Sprintf("%v", obj)
|
||||
}
|
||||
|
||||
func dump(obj interface{}) string {
|
||||
cfg := spew.ConfigState{
|
||||
Indent: indentStr,
|
||||
}
|
||||
return cfg.Sdump(obj)
|
||||
}
|
||||
|
||||
func indent(src []byte, prefix string) string {
|
||||
var buf bytes.Buffer
|
||||
err := json.Indent(&buf, src, prefix, indentStr)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("!!! %v", err)
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var obj typeForTest
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&obj)
|
||||
for i := 0; i < t.N; i++ {
|
||||
jb, err := fn(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var before typeForTest
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&before)
|
||||
jb, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal: %v", err)
|
||||
}
|
||||
|
||||
for i := 0; i < t.N; i++ {
|
||||
var after typeForTest
|
||||
err = fn(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStandardMarshal(t *testing.B) {
|
||||
benchmarkMarshal(t, "stdlib", json.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "stdlib", json.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalFastest(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalFastest(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalDefault(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalDefault(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalCompatible(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal)
|
||||
}
|
@ -1,3 +0,0 @@
|
||||
package test
|
||||
|
||||
type typeForTest []byte
|
@ -1,152 +0,0 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
fuzz "github.com/google/gofuzz"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
func Test_Roundtrip(t *testing.T) {
|
||||
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
|
||||
for i := 0; i < 100; i++ {
|
||||
var before typeForTest
|
||||
fz.Fuzz(&before)
|
||||
|
||||
jbStd, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with stdlib: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbStd))) == 0 {
|
||||
t.Fatal("stdlib marshal produced empty result and no error")
|
||||
}
|
||||
jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with jsoniter: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbIter))) == 0 {
|
||||
t.Fatal("jsoniter marshal produced empty result and no error")
|
||||
}
|
||||
if string(jbStd) != string(jbIter) {
|
||||
t.Fatalf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
|
||||
indent(jbStd, " "), indent(jbIter, " "), dump(before))
|
||||
}
|
||||
|
||||
var afterStd typeForTest
|
||||
err = json.Unmarshal(jbIter, &afterStd)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
var afterIter typeForTest
|
||||
err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||
t.Fatalf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
|
||||
dump(afterStd), dump(afterIter), indent(jbIter, " "))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const indentStr = "> "
|
||||
|
||||
func fingerprint(obj interface{}) string {
|
||||
c := spew.ConfigState{
|
||||
SortKeys: true,
|
||||
SpewKeys: true,
|
||||
}
|
||||
return c.Sprintf("%v", obj)
|
||||
}
|
||||
|
||||
func dump(obj interface{}) string {
|
||||
cfg := spew.ConfigState{
|
||||
Indent: indentStr,
|
||||
}
|
||||
return cfg.Sdump(obj)
|
||||
}
|
||||
|
||||
func indent(src []byte, prefix string) string {
|
||||
var buf bytes.Buffer
|
||||
err := json.Indent(&buf, src, prefix, indentStr)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("!!! %v", err)
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var obj typeForTest
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&obj)
|
||||
for i := 0; i < t.N; i++ {
|
||||
jb, err := fn(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var before typeForTest
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&before)
|
||||
jb, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal: %v", err)
|
||||
}
|
||||
|
||||
for i := 0; i < t.N; i++ {
|
||||
var after typeForTest
|
||||
err = fn(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStandardMarshal(t *testing.B) {
|
||||
benchmarkMarshal(t, "stdlib", json.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "stdlib", json.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalFastest(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalFastest(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalDefault(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalDefault(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalCompatible(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal)
|
||||
}
|
@ -1,3 +0,0 @@
|
||||
package test
|
||||
|
||||
type typeForTest []float64
|
@ -1,152 +0,0 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
fuzz "github.com/google/gofuzz"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
func Test_Roundtrip(t *testing.T) {
|
||||
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
|
||||
for i := 0; i < 100; i++ {
|
||||
var before typeForTest
|
||||
fz.Fuzz(&before)
|
||||
|
||||
jbStd, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with stdlib: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbStd))) == 0 {
|
||||
t.Fatal("stdlib marshal produced empty result and no error")
|
||||
}
|
||||
jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with jsoniter: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbIter))) == 0 {
|
||||
t.Fatal("jsoniter marshal produced empty result and no error")
|
||||
}
|
||||
if string(jbStd) != string(jbIter) {
|
||||
t.Fatalf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
|
||||
indent(jbStd, " "), indent(jbIter, " "), dump(before))
|
||||
}
|
||||
|
||||
var afterStd typeForTest
|
||||
err = json.Unmarshal(jbIter, &afterStd)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
var afterIter typeForTest
|
||||
err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||
t.Fatalf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
|
||||
dump(afterStd), dump(afterIter), indent(jbIter, " "))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const indentStr = "> "
|
||||
|
||||
func fingerprint(obj interface{}) string {
|
||||
c := spew.ConfigState{
|
||||
SortKeys: true,
|
||||
SpewKeys: true,
|
||||
}
|
||||
return c.Sprintf("%v", obj)
|
||||
}
|
||||
|
||||
func dump(obj interface{}) string {
|
||||
cfg := spew.ConfigState{
|
||||
Indent: indentStr,
|
||||
}
|
||||
return cfg.Sdump(obj)
|
||||
}
|
||||
|
||||
func indent(src []byte, prefix string) string {
|
||||
var buf bytes.Buffer
|
||||
err := json.Indent(&buf, src, prefix, indentStr)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("!!! %v", err)
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var obj typeForTest
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&obj)
|
||||
for i := 0; i < t.N; i++ {
|
||||
jb, err := fn(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var before typeForTest
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&before)
|
||||
jb, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal: %v", err)
|
||||
}
|
||||
|
||||
for i := 0; i < t.N; i++ {
|
||||
var after typeForTest
|
||||
err = fn(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStandardMarshal(t *testing.B) {
|
||||
benchmarkMarshal(t, "stdlib", json.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "stdlib", json.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalFastest(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalFastest(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalDefault(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalDefault(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalCompatible(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal)
|
||||
}
|
@ -1,3 +0,0 @@
|
||||
package test
|
||||
|
||||
type typeForTest []int32
|
@ -1,152 +0,0 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
fuzz "github.com/google/gofuzz"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
func Test_Roundtrip(t *testing.T) {
|
||||
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
|
||||
for i := 0; i < 100; i++ {
|
||||
var before typeForTest
|
||||
fz.Fuzz(&before)
|
||||
|
||||
jbStd, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with stdlib: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbStd))) == 0 {
|
||||
t.Fatal("stdlib marshal produced empty result and no error")
|
||||
}
|
||||
jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with jsoniter: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbIter))) == 0 {
|
||||
t.Fatal("jsoniter marshal produced empty result and no error")
|
||||
}
|
||||
if string(jbStd) != string(jbIter) {
|
||||
t.Fatalf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
|
||||
indent(jbStd, " "), indent(jbIter, " "), dump(before))
|
||||
}
|
||||
|
||||
var afterStd typeForTest
|
||||
err = json.Unmarshal(jbIter, &afterStd)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
var afterIter typeForTest
|
||||
err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||
t.Fatalf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
|
||||
dump(afterStd), dump(afterIter), indent(jbIter, " "))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const indentStr = "> "
|
||||
|
||||
func fingerprint(obj interface{}) string {
|
||||
c := spew.ConfigState{
|
||||
SortKeys: true,
|
||||
SpewKeys: true,
|
||||
}
|
||||
return c.Sprintf("%v", obj)
|
||||
}
|
||||
|
||||
func dump(obj interface{}) string {
|
||||
cfg := spew.ConfigState{
|
||||
Indent: indentStr,
|
||||
}
|
||||
return cfg.Sdump(obj)
|
||||
}
|
||||
|
||||
func indent(src []byte, prefix string) string {
|
||||
var buf bytes.Buffer
|
||||
err := json.Indent(&buf, src, prefix, indentStr)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("!!! %v", err)
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var obj typeForTest
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&obj)
|
||||
for i := 0; i < t.N; i++ {
|
||||
jb, err := fn(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var before typeForTest
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&before)
|
||||
jb, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal: %v", err)
|
||||
}
|
||||
|
||||
for i := 0; i < t.N; i++ {
|
||||
var after typeForTest
|
||||
err = fn(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStandardMarshal(t *testing.B) {
|
||||
benchmarkMarshal(t, "stdlib", json.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "stdlib", json.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalFastest(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalFastest(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalDefault(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalDefault(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalCompatible(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal)
|
||||
}
|
@ -1,3 +0,0 @@
|
||||
package test
|
||||
|
||||
type typeForTest []int64
|
@ -1,152 +0,0 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
fuzz "github.com/google/gofuzz"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
func Test_Roundtrip(t *testing.T) {
|
||||
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
|
||||
for i := 0; i < 100; i++ {
|
||||
var before typeForTest
|
||||
fz.Fuzz(&before)
|
||||
|
||||
jbStd, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with stdlib: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbStd))) == 0 {
|
||||
t.Fatal("stdlib marshal produced empty result and no error")
|
||||
}
|
||||
jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with jsoniter: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbIter))) == 0 {
|
||||
t.Fatal("jsoniter marshal produced empty result and no error")
|
||||
}
|
||||
if string(jbStd) != string(jbIter) {
|
||||
t.Fatalf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
|
||||
indent(jbStd, " "), indent(jbIter, " "), dump(before))
|
||||
}
|
||||
|
||||
var afterStd typeForTest
|
||||
err = json.Unmarshal(jbIter, &afterStd)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
var afterIter typeForTest
|
||||
err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||
t.Fatalf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
|
||||
dump(afterStd), dump(afterIter), indent(jbIter, " "))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const indentStr = "> "
|
||||
|
||||
func fingerprint(obj interface{}) string {
|
||||
c := spew.ConfigState{
|
||||
SortKeys: true,
|
||||
SpewKeys: true,
|
||||
}
|
||||
return c.Sprintf("%v", obj)
|
||||
}
|
||||
|
||||
func dump(obj interface{}) string {
|
||||
cfg := spew.ConfigState{
|
||||
Indent: indentStr,
|
||||
}
|
||||
return cfg.Sdump(obj)
|
||||
}
|
||||
|
||||
func indent(src []byte, prefix string) string {
|
||||
var buf bytes.Buffer
|
||||
err := json.Indent(&buf, src, prefix, indentStr)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("!!! %v", err)
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var obj typeForTest
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&obj)
|
||||
for i := 0; i < t.N; i++ {
|
||||
jb, err := fn(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var before typeForTest
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&before)
|
||||
jb, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal: %v", err)
|
||||
}
|
||||
|
||||
for i := 0; i < t.N; i++ {
|
||||
var after typeForTest
|
||||
err = fn(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStandardMarshal(t *testing.B) {
|
||||
benchmarkMarshal(t, "stdlib", json.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "stdlib", json.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalFastest(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalFastest(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalDefault(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalDefault(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalCompatible(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal)
|
||||
}
|
@ -1,3 +0,0 @@
|
||||
package test
|
||||
|
||||
type typeForTest []map[int32]string
|
@ -1,152 +0,0 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
fuzz "github.com/google/gofuzz"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
func Test_Roundtrip(t *testing.T) {
|
||||
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
|
||||
for i := 0; i < 100; i++ {
|
||||
var before typeForTest
|
||||
fz.Fuzz(&before)
|
||||
|
||||
jbStd, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with stdlib: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbStd))) == 0 {
|
||||
t.Fatal("stdlib marshal produced empty result and no error")
|
||||
}
|
||||
jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with jsoniter: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbIter))) == 0 {
|
||||
t.Fatal("jsoniter marshal produced empty result and no error")
|
||||
}
|
||||
if string(jbStd) != string(jbIter) {
|
||||
t.Fatalf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
|
||||
indent(jbStd, " "), indent(jbIter, " "), dump(before))
|
||||
}
|
||||
|
||||
var afterStd typeForTest
|
||||
err = json.Unmarshal(jbIter, &afterStd)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
var afterIter typeForTest
|
||||
err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||
t.Fatalf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
|
||||
dump(afterStd), dump(afterIter), indent(jbIter, " "))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const indentStr = "> "
|
||||
|
||||
func fingerprint(obj interface{}) string {
|
||||
c := spew.ConfigState{
|
||||
SortKeys: true,
|
||||
SpewKeys: true,
|
||||
}
|
||||
return c.Sprintf("%v", obj)
|
||||
}
|
||||
|
||||
func dump(obj interface{}) string {
|
||||
cfg := spew.ConfigState{
|
||||
Indent: indentStr,
|
||||
}
|
||||
return cfg.Sdump(obj)
|
||||
}
|
||||
|
||||
func indent(src []byte, prefix string) string {
|
||||
var buf bytes.Buffer
|
||||
err := json.Indent(&buf, src, prefix, indentStr)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("!!! %v", err)
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var obj typeForTest
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&obj)
|
||||
for i := 0; i < t.N; i++ {
|
||||
jb, err := fn(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var before typeForTest
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&before)
|
||||
jb, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal: %v", err)
|
||||
}
|
||||
|
||||
for i := 0; i < t.N; i++ {
|
||||
var after typeForTest
|
||||
err = fn(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStandardMarshal(t *testing.B) {
|
||||
benchmarkMarshal(t, "stdlib", json.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "stdlib", json.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalFastest(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalFastest(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalDefault(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalDefault(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalCompatible(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal)
|
||||
}
|
@ -1,3 +0,0 @@
|
||||
package test
|
||||
|
||||
type typeForTest []map[string]string
|
@ -1,152 +0,0 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
fuzz "github.com/google/gofuzz"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
func Test_Roundtrip(t *testing.T) {
|
||||
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
|
||||
for i := 0; i < 100; i++ {
|
||||
var before typeForTest
|
||||
fz.Fuzz(&before)
|
||||
|
||||
jbStd, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with stdlib: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbStd))) == 0 {
|
||||
t.Fatal("stdlib marshal produced empty result and no error")
|
||||
}
|
||||
jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with jsoniter: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbIter))) == 0 {
|
||||
t.Fatal("jsoniter marshal produced empty result and no error")
|
||||
}
|
||||
if string(jbStd) != string(jbIter) {
|
||||
t.Fatalf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
|
||||
indent(jbStd, " "), indent(jbIter, " "), dump(before))
|
||||
}
|
||||
|
||||
var afterStd typeForTest
|
||||
err = json.Unmarshal(jbIter, &afterStd)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
var afterIter typeForTest
|
||||
err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||
t.Fatalf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
|
||||
dump(afterStd), dump(afterIter), indent(jbIter, " "))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const indentStr = "> "
|
||||
|
||||
func fingerprint(obj interface{}) string {
|
||||
c := spew.ConfigState{
|
||||
SortKeys: true,
|
||||
SpewKeys: true,
|
||||
}
|
||||
return c.Sprintf("%v", obj)
|
||||
}
|
||||
|
||||
func dump(obj interface{}) string {
|
||||
cfg := spew.ConfigState{
|
||||
Indent: indentStr,
|
||||
}
|
||||
return cfg.Sdump(obj)
|
||||
}
|
||||
|
||||
func indent(src []byte, prefix string) string {
|
||||
var buf bytes.Buffer
|
||||
err := json.Indent(&buf, src, prefix, indentStr)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("!!! %v", err)
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var obj typeForTest
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&obj)
|
||||
for i := 0; i < t.N; i++ {
|
||||
jb, err := fn(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var before typeForTest
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&before)
|
||||
jb, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal: %v", err)
|
||||
}
|
||||
|
||||
for i := 0; i < t.N; i++ {
|
||||
var after typeForTest
|
||||
err = fn(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStandardMarshal(t *testing.B) {
|
||||
benchmarkMarshal(t, "stdlib", json.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "stdlib", json.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalFastest(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalFastest(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalDefault(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalDefault(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalCompatible(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal)
|
||||
}
|
@ -1,3 +0,0 @@
|
||||
package test
|
||||
|
||||
type typeForTest [4]*[4]bool
|
@ -1,152 +0,0 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
fuzz "github.com/google/gofuzz"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
func Test_Roundtrip(t *testing.T) {
|
||||
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
|
||||
for i := 0; i < 100; i++ {
|
||||
var before typeForTest
|
||||
fz.Fuzz(&before)
|
||||
|
||||
jbStd, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with stdlib: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbStd))) == 0 {
|
||||
t.Fatal("stdlib marshal produced empty result and no error")
|
||||
}
|
||||
jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with jsoniter: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbIter))) == 0 {
|
||||
t.Fatal("jsoniter marshal produced empty result and no error")
|
||||
}
|
||||
if string(jbStd) != string(jbIter) {
|
||||
t.Fatalf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
|
||||
indent(jbStd, " "), indent(jbIter, " "), dump(before))
|
||||
}
|
||||
|
||||
var afterStd typeForTest
|
||||
err = json.Unmarshal(jbIter, &afterStd)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
var afterIter typeForTest
|
||||
err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||
t.Fatalf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
|
||||
dump(afterStd), dump(afterIter), indent(jbIter, " "))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const indentStr = "> "
|
||||
|
||||
func fingerprint(obj interface{}) string {
|
||||
c := spew.ConfigState{
|
||||
SortKeys: true,
|
||||
SpewKeys: true,
|
||||
}
|
||||
return c.Sprintf("%v", obj)
|
||||
}
|
||||
|
||||
func dump(obj interface{}) string {
|
||||
cfg := spew.ConfigState{
|
||||
Indent: indentStr,
|
||||
}
|
||||
return cfg.Sdump(obj)
|
||||
}
|
||||
|
||||
func indent(src []byte, prefix string) string {
|
||||
var buf bytes.Buffer
|
||||
err := json.Indent(&buf, src, prefix, indentStr)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("!!! %v", err)
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var obj typeForTest
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&obj)
|
||||
for i := 0; i < t.N; i++ {
|
||||
jb, err := fn(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var before typeForTest
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&before)
|
||||
jb, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal: %v", err)
|
||||
}
|
||||
|
||||
for i := 0; i < t.N; i++ {
|
||||
var after typeForTest
|
||||
err = fn(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStandardMarshal(t *testing.B) {
|
||||
benchmarkMarshal(t, "stdlib", json.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "stdlib", json.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalFastest(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalFastest(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalDefault(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalDefault(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalCompatible(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal)
|
||||
}
|
@ -1,3 +0,0 @@
|
||||
package test
|
||||
|
||||
type typeForTest [4]*[4]byte
|
@ -1,152 +0,0 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
fuzz "github.com/google/gofuzz"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
func Test_Roundtrip(t *testing.T) {
|
||||
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
|
||||
for i := 0; i < 100; i++ {
|
||||
var before typeForTest
|
||||
fz.Fuzz(&before)
|
||||
|
||||
jbStd, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with stdlib: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbStd))) == 0 {
|
||||
t.Fatal("stdlib marshal produced empty result and no error")
|
||||
}
|
||||
jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with jsoniter: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbIter))) == 0 {
|
||||
t.Fatal("jsoniter marshal produced empty result and no error")
|
||||
}
|
||||
if string(jbStd) != string(jbIter) {
|
||||
t.Fatalf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
|
||||
indent(jbStd, " "), indent(jbIter, " "), dump(before))
|
||||
}
|
||||
|
||||
var afterStd typeForTest
|
||||
err = json.Unmarshal(jbIter, &afterStd)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
var afterIter typeForTest
|
||||
err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||
t.Fatalf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
|
||||
dump(afterStd), dump(afterIter), indent(jbIter, " "))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const indentStr = "> "
|
||||
|
||||
func fingerprint(obj interface{}) string {
|
||||
c := spew.ConfigState{
|
||||
SortKeys: true,
|
||||
SpewKeys: true,
|
||||
}
|
||||
return c.Sprintf("%v", obj)
|
||||
}
|
||||
|
||||
func dump(obj interface{}) string {
|
||||
cfg := spew.ConfigState{
|
||||
Indent: indentStr,
|
||||
}
|
||||
return cfg.Sdump(obj)
|
||||
}
|
||||
|
||||
func indent(src []byte, prefix string) string {
|
||||
var buf bytes.Buffer
|
||||
err := json.Indent(&buf, src, prefix, indentStr)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("!!! %v", err)
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var obj typeForTest
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&obj)
|
||||
for i := 0; i < t.N; i++ {
|
||||
jb, err := fn(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var before typeForTest
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&before)
|
||||
jb, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal: %v", err)
|
||||
}
|
||||
|
||||
for i := 0; i < t.N; i++ {
|
||||
var after typeForTest
|
||||
err = fn(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStandardMarshal(t *testing.B) {
|
||||
benchmarkMarshal(t, "stdlib", json.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "stdlib", json.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalFastest(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalFastest(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalDefault(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalDefault(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalCompatible(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal)
|
||||
}
|
@ -1,3 +0,0 @@
|
||||
package test
|
||||
|
||||
type typeForTest [4]*[4]float64
|
@ -1,152 +0,0 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
fuzz "github.com/google/gofuzz"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
func Test_Roundtrip(t *testing.T) {
|
||||
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
|
||||
for i := 0; i < 100; i++ {
|
||||
var before typeForTest
|
||||
fz.Fuzz(&before)
|
||||
|
||||
jbStd, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with stdlib: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbStd))) == 0 {
|
||||
t.Fatal("stdlib marshal produced empty result and no error")
|
||||
}
|
||||
jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with jsoniter: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbIter))) == 0 {
|
||||
t.Fatal("jsoniter marshal produced empty result and no error")
|
||||
}
|
||||
if string(jbStd) != string(jbIter) {
|
||||
t.Fatalf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
|
||||
indent(jbStd, " "), indent(jbIter, " "), dump(before))
|
||||
}
|
||||
|
||||
var afterStd typeForTest
|
||||
err = json.Unmarshal(jbIter, &afterStd)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
var afterIter typeForTest
|
||||
err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||
t.Fatalf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
|
||||
dump(afterStd), dump(afterIter), indent(jbIter, " "))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const indentStr = "> "
|
||||
|
||||
func fingerprint(obj interface{}) string {
|
||||
c := spew.ConfigState{
|
||||
SortKeys: true,
|
||||
SpewKeys: true,
|
||||
}
|
||||
return c.Sprintf("%v", obj)
|
||||
}
|
||||
|
||||
func dump(obj interface{}) string {
|
||||
cfg := spew.ConfigState{
|
||||
Indent: indentStr,
|
||||
}
|
||||
return cfg.Sdump(obj)
|
||||
}
|
||||
|
||||
func indent(src []byte, prefix string) string {
|
||||
var buf bytes.Buffer
|
||||
err := json.Indent(&buf, src, prefix, indentStr)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("!!! %v", err)
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var obj typeForTest
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&obj)
|
||||
for i := 0; i < t.N; i++ {
|
||||
jb, err := fn(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var before typeForTest
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&before)
|
||||
jb, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal: %v", err)
|
||||
}
|
||||
|
||||
for i := 0; i < t.N; i++ {
|
||||
var after typeForTest
|
||||
err = fn(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStandardMarshal(t *testing.B) {
|
||||
benchmarkMarshal(t, "stdlib", json.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "stdlib", json.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalFastest(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalFastest(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalDefault(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalDefault(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalCompatible(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal)
|
||||
}
|
@ -1,3 +0,0 @@
|
||||
package test
|
||||
|
||||
type typeForTest [4]*[4]int32
|
@ -1,152 +0,0 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
fuzz "github.com/google/gofuzz"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
func Test_Roundtrip(t *testing.T) {
|
||||
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
|
||||
for i := 0; i < 100; i++ {
|
||||
var before typeForTest
|
||||
fz.Fuzz(&before)
|
||||
|
||||
jbStd, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with stdlib: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbStd))) == 0 {
|
||||
t.Fatal("stdlib marshal produced empty result and no error")
|
||||
}
|
||||
jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with jsoniter: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbIter))) == 0 {
|
||||
t.Fatal("jsoniter marshal produced empty result and no error")
|
||||
}
|
||||
if string(jbStd) != string(jbIter) {
|
||||
t.Fatalf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
|
||||
indent(jbStd, " "), indent(jbIter, " "), dump(before))
|
||||
}
|
||||
|
||||
var afterStd typeForTest
|
||||
err = json.Unmarshal(jbIter, &afterStd)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
var afterIter typeForTest
|
||||
err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||
t.Fatalf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
|
||||
dump(afterStd), dump(afterIter), indent(jbIter, " "))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const indentStr = "> "
|
||||
|
||||
func fingerprint(obj interface{}) string {
|
||||
c := spew.ConfigState{
|
||||
SortKeys: true,
|
||||
SpewKeys: true,
|
||||
}
|
||||
return c.Sprintf("%v", obj)
|
||||
}
|
||||
|
||||
func dump(obj interface{}) string {
|
||||
cfg := spew.ConfigState{
|
||||
Indent: indentStr,
|
||||
}
|
||||
return cfg.Sdump(obj)
|
||||
}
|
||||
|
||||
func indent(src []byte, prefix string) string {
|
||||
var buf bytes.Buffer
|
||||
err := json.Indent(&buf, src, prefix, indentStr)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("!!! %v", err)
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var obj typeForTest
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&obj)
|
||||
for i := 0; i < t.N; i++ {
|
||||
jb, err := fn(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var before typeForTest
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&before)
|
||||
jb, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal: %v", err)
|
||||
}
|
||||
|
||||
for i := 0; i < t.N; i++ {
|
||||
var after typeForTest
|
||||
err = fn(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStandardMarshal(t *testing.B) {
|
||||
benchmarkMarshal(t, "stdlib", json.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "stdlib", json.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalFastest(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalFastest(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalDefault(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalDefault(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalCompatible(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal)
|
||||
}
|
@ -1,3 +0,0 @@
|
||||
package test
|
||||
|
||||
type typeForTest [4]*[4]*string
|
@ -1,152 +0,0 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
fuzz "github.com/google/gofuzz"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
func Test_Roundtrip(t *testing.T) {
|
||||
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
|
||||
for i := 0; i < 100; i++ {
|
||||
var before typeForTest
|
||||
fz.Fuzz(&before)
|
||||
|
||||
jbStd, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with stdlib: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbStd))) == 0 {
|
||||
t.Fatal("stdlib marshal produced empty result and no error")
|
||||
}
|
||||
jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with jsoniter: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbIter))) == 0 {
|
||||
t.Fatal("jsoniter marshal produced empty result and no error")
|
||||
}
|
||||
if string(jbStd) != string(jbIter) {
|
||||
t.Fatalf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
|
||||
indent(jbStd, " "), indent(jbIter, " "), dump(before))
|
||||
}
|
||||
|
||||
var afterStd typeForTest
|
||||
err = json.Unmarshal(jbIter, &afterStd)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
var afterIter typeForTest
|
||||
err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||
t.Fatalf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
|
||||
dump(afterStd), dump(afterIter), indent(jbIter, " "))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const indentStr = "> "
|
||||
|
||||
func fingerprint(obj interface{}) string {
|
||||
c := spew.ConfigState{
|
||||
SortKeys: true,
|
||||
SpewKeys: true,
|
||||
}
|
||||
return c.Sprintf("%v", obj)
|
||||
}
|
||||
|
||||
func dump(obj interface{}) string {
|
||||
cfg := spew.ConfigState{
|
||||
Indent: indentStr,
|
||||
}
|
||||
return cfg.Sdump(obj)
|
||||
}
|
||||
|
||||
func indent(src []byte, prefix string) string {
|
||||
var buf bytes.Buffer
|
||||
err := json.Indent(&buf, src, prefix, indentStr)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("!!! %v", err)
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var obj typeForTest
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&obj)
|
||||
for i := 0; i < t.N; i++ {
|
||||
jb, err := fn(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var before typeForTest
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&before)
|
||||
jb, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal: %v", err)
|
||||
}
|
||||
|
||||
for i := 0; i < t.N; i++ {
|
||||
var after typeForTest
|
||||
err = fn(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStandardMarshal(t *testing.B) {
|
||||
benchmarkMarshal(t, "stdlib", json.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "stdlib", json.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalFastest(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalFastest(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalDefault(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalDefault(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalCompatible(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal)
|
||||
}
|
@ -1,3 +0,0 @@
|
||||
package test
|
||||
|
||||
type typeForTest [4]*[4]string
|
@ -1,152 +0,0 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
fuzz "github.com/google/gofuzz"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
func Test_Roundtrip(t *testing.T) {
|
||||
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
|
||||
for i := 0; i < 100; i++ {
|
||||
var before typeForTest
|
||||
fz.Fuzz(&before)
|
||||
|
||||
jbStd, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with stdlib: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbStd))) == 0 {
|
||||
t.Fatal("stdlib marshal produced empty result and no error")
|
||||
}
|
||||
jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with jsoniter: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbIter))) == 0 {
|
||||
t.Fatal("jsoniter marshal produced empty result and no error")
|
||||
}
|
||||
if string(jbStd) != string(jbIter) {
|
||||
t.Fatalf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
|
||||
indent(jbStd, " "), indent(jbIter, " "), dump(before))
|
||||
}
|
||||
|
||||
var afterStd typeForTest
|
||||
err = json.Unmarshal(jbIter, &afterStd)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
var afterIter typeForTest
|
||||
err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||
t.Fatalf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
|
||||
dump(afterStd), dump(afterIter), indent(jbIter, " "))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const indentStr = "> "
|
||||
|
||||
func fingerprint(obj interface{}) string {
|
||||
c := spew.ConfigState{
|
||||
SortKeys: true,
|
||||
SpewKeys: true,
|
||||
}
|
||||
return c.Sprintf("%v", obj)
|
||||
}
|
||||
|
||||
func dump(obj interface{}) string {
|
||||
cfg := spew.ConfigState{
|
||||
Indent: indentStr,
|
||||
}
|
||||
return cfg.Sdump(obj)
|
||||
}
|
||||
|
||||
func indent(src []byte, prefix string) string {
|
||||
var buf bytes.Buffer
|
||||
err := json.Indent(&buf, src, prefix, indentStr)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("!!! %v", err)
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var obj typeForTest
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&obj)
|
||||
for i := 0; i < t.N; i++ {
|
||||
jb, err := fn(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var before typeForTest
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&before)
|
||||
jb, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal: %v", err)
|
||||
}
|
||||
|
||||
for i := 0; i < t.N; i++ {
|
||||
var after typeForTest
|
||||
err = fn(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStandardMarshal(t *testing.B) {
|
||||
benchmarkMarshal(t, "stdlib", json.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "stdlib", json.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalFastest(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalFastest(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalDefault(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalDefault(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalCompatible(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal)
|
||||
}
|
@ -1,3 +0,0 @@
|
||||
package test
|
||||
|
||||
type typeForTest [4]*[4]uint8
|
@ -1,152 +0,0 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
fuzz "github.com/google/gofuzz"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
func Test_Roundtrip(t *testing.T) {
|
||||
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
|
||||
for i := 0; i < 100; i++ {
|
||||
var before typeForTest
|
||||
fz.Fuzz(&before)
|
||||
|
||||
jbStd, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with stdlib: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbStd))) == 0 {
|
||||
t.Fatal("stdlib marshal produced empty result and no error")
|
||||
}
|
||||
jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with jsoniter: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbIter))) == 0 {
|
||||
t.Fatal("jsoniter marshal produced empty result and no error")
|
||||
}
|
||||
if string(jbStd) != string(jbIter) {
|
||||
t.Fatalf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
|
||||
indent(jbStd, " "), indent(jbIter, " "), dump(before))
|
||||
}
|
||||
|
||||
var afterStd typeForTest
|
||||
err = json.Unmarshal(jbIter, &afterStd)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
var afterIter typeForTest
|
||||
err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||
t.Fatalf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
|
||||
dump(afterStd), dump(afterIter), indent(jbIter, " "))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const indentStr = "> "
|
||||
|
||||
func fingerprint(obj interface{}) string {
|
||||
c := spew.ConfigState{
|
||||
SortKeys: true,
|
||||
SpewKeys: true,
|
||||
}
|
||||
return c.Sprintf("%v", obj)
|
||||
}
|
||||
|
||||
func dump(obj interface{}) string {
|
||||
cfg := spew.ConfigState{
|
||||
Indent: indentStr,
|
||||
}
|
||||
return cfg.Sdump(obj)
|
||||
}
|
||||
|
||||
func indent(src []byte, prefix string) string {
|
||||
var buf bytes.Buffer
|
||||
err := json.Indent(&buf, src, prefix, indentStr)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("!!! %v", err)
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var obj typeForTest
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&obj)
|
||||
for i := 0; i < t.N; i++ {
|
||||
jb, err := fn(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var before typeForTest
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&before)
|
||||
jb, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal: %v", err)
|
||||
}
|
||||
|
||||
for i := 0; i < t.N; i++ {
|
||||
var after typeForTest
|
||||
err = fn(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStandardMarshal(t *testing.B) {
|
||||
benchmarkMarshal(t, "stdlib", json.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "stdlib", json.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalFastest(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalFastest(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalDefault(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalDefault(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalCompatible(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal)
|
||||
}
|
@ -1,3 +0,0 @@
|
||||
package test
|
||||
|
||||
type typeForTest []*bool
|
@ -1,152 +0,0 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
fuzz "github.com/google/gofuzz"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
func Test_Roundtrip(t *testing.T) {
|
||||
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
|
||||
for i := 0; i < 100; i++ {
|
||||
var before typeForTest
|
||||
fz.Fuzz(&before)
|
||||
|
||||
jbStd, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with stdlib: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbStd))) == 0 {
|
||||
t.Fatal("stdlib marshal produced empty result and no error")
|
||||
}
|
||||
jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with jsoniter: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbIter))) == 0 {
|
||||
t.Fatal("jsoniter marshal produced empty result and no error")
|
||||
}
|
||||
if string(jbStd) != string(jbIter) {
|
||||
t.Fatalf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
|
||||
indent(jbStd, " "), indent(jbIter, " "), dump(before))
|
||||
}
|
||||
|
||||
var afterStd typeForTest
|
||||
err = json.Unmarshal(jbIter, &afterStd)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
var afterIter typeForTest
|
||||
err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||
t.Fatalf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
|
||||
dump(afterStd), dump(afterIter), indent(jbIter, " "))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const indentStr = "> "
|
||||
|
||||
func fingerprint(obj interface{}) string {
|
||||
c := spew.ConfigState{
|
||||
SortKeys: true,
|
||||
SpewKeys: true,
|
||||
}
|
||||
return c.Sprintf("%v", obj)
|
||||
}
|
||||
|
||||
func dump(obj interface{}) string {
|
||||
cfg := spew.ConfigState{
|
||||
Indent: indentStr,
|
||||
}
|
||||
return cfg.Sdump(obj)
|
||||
}
|
||||
|
||||
func indent(src []byte, prefix string) string {
|
||||
var buf bytes.Buffer
|
||||
err := json.Indent(&buf, src, prefix, indentStr)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("!!! %v", err)
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var obj typeForTest
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&obj)
|
||||
for i := 0; i < t.N; i++ {
|
||||
jb, err := fn(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var before typeForTest
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&before)
|
||||
jb, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal: %v", err)
|
||||
}
|
||||
|
||||
for i := 0; i < t.N; i++ {
|
||||
var after typeForTest
|
||||
err = fn(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStandardMarshal(t *testing.B) {
|
||||
benchmarkMarshal(t, "stdlib", json.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "stdlib", json.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalFastest(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalFastest(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalDefault(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalDefault(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalCompatible(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal)
|
||||
}
|
@ -1,3 +0,0 @@
|
||||
package test
|
||||
|
||||
type typeForTest []*float64
|
@ -1,152 +0,0 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
fuzz "github.com/google/gofuzz"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
func Test_Roundtrip(t *testing.T) {
|
||||
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
|
||||
for i := 0; i < 100; i++ {
|
||||
var before typeForTest
|
||||
fz.Fuzz(&before)
|
||||
|
||||
jbStd, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with stdlib: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbStd))) == 0 {
|
||||
t.Fatal("stdlib marshal produced empty result and no error")
|
||||
}
|
||||
jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with jsoniter: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbIter))) == 0 {
|
||||
t.Fatal("jsoniter marshal produced empty result and no error")
|
||||
}
|
||||
if string(jbStd) != string(jbIter) {
|
||||
t.Fatalf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
|
||||
indent(jbStd, " "), indent(jbIter, " "), dump(before))
|
||||
}
|
||||
|
||||
var afterStd typeForTest
|
||||
err = json.Unmarshal(jbIter, &afterStd)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
var afterIter typeForTest
|
||||
err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||
t.Fatalf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
|
||||
dump(afterStd), dump(afterIter), indent(jbIter, " "))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const indentStr = "> "
|
||||
|
||||
func fingerprint(obj interface{}) string {
|
||||
c := spew.ConfigState{
|
||||
SortKeys: true,
|
||||
SpewKeys: true,
|
||||
}
|
||||
return c.Sprintf("%v", obj)
|
||||
}
|
||||
|
||||
func dump(obj interface{}) string {
|
||||
cfg := spew.ConfigState{
|
||||
Indent: indentStr,
|
||||
}
|
||||
return cfg.Sdump(obj)
|
||||
}
|
||||
|
||||
func indent(src []byte, prefix string) string {
|
||||
var buf bytes.Buffer
|
||||
err := json.Indent(&buf, src, prefix, indentStr)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("!!! %v", err)
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var obj typeForTest
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&obj)
|
||||
for i := 0; i < t.N; i++ {
|
||||
jb, err := fn(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var before typeForTest
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&before)
|
||||
jb, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal: %v", err)
|
||||
}
|
||||
|
||||
for i := 0; i < t.N; i++ {
|
||||
var after typeForTest
|
||||
err = fn(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStandardMarshal(t *testing.B) {
|
||||
benchmarkMarshal(t, "stdlib", json.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "stdlib", json.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalFastest(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalFastest(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalDefault(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalDefault(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalCompatible(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal)
|
||||
}
|
@ -1,3 +0,0 @@
|
||||
package test
|
||||
|
||||
type typeForTest []*int32
|
@ -1,152 +0,0 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
fuzz "github.com/google/gofuzz"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
func Test_Roundtrip(t *testing.T) {
|
||||
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
|
||||
for i := 0; i < 100; i++ {
|
||||
var before typeForTest
|
||||
fz.Fuzz(&before)
|
||||
|
||||
jbStd, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with stdlib: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbStd))) == 0 {
|
||||
t.Fatal("stdlib marshal produced empty result and no error")
|
||||
}
|
||||
jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with jsoniter: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbIter))) == 0 {
|
||||
t.Fatal("jsoniter marshal produced empty result and no error")
|
||||
}
|
||||
if string(jbStd) != string(jbIter) {
|
||||
t.Fatalf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
|
||||
indent(jbStd, " "), indent(jbIter, " "), dump(before))
|
||||
}
|
||||
|
||||
var afterStd typeForTest
|
||||
err = json.Unmarshal(jbIter, &afterStd)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
var afterIter typeForTest
|
||||
err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||
t.Fatalf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
|
||||
dump(afterStd), dump(afterIter), indent(jbIter, " "))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const indentStr = "> "
|
||||
|
||||
func fingerprint(obj interface{}) string {
|
||||
c := spew.ConfigState{
|
||||
SortKeys: true,
|
||||
SpewKeys: true,
|
||||
}
|
||||
return c.Sprintf("%v", obj)
|
||||
}
|
||||
|
||||
func dump(obj interface{}) string {
|
||||
cfg := spew.ConfigState{
|
||||
Indent: indentStr,
|
||||
}
|
||||
return cfg.Sdump(obj)
|
||||
}
|
||||
|
||||
func indent(src []byte, prefix string) string {
|
||||
var buf bytes.Buffer
|
||||
err := json.Indent(&buf, src, prefix, indentStr)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("!!! %v", err)
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var obj typeForTest
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&obj)
|
||||
for i := 0; i < t.N; i++ {
|
||||
jb, err := fn(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var before typeForTest
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&before)
|
||||
jb, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal: %v", err)
|
||||
}
|
||||
|
||||
for i := 0; i < t.N; i++ {
|
||||
var after typeForTest
|
||||
err = fn(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStandardMarshal(t *testing.B) {
|
||||
benchmarkMarshal(t, "stdlib", json.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "stdlib", json.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalFastest(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalFastest(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalDefault(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalDefault(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalCompatible(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal)
|
||||
}
|
@ -1,3 +0,0 @@
|
||||
package test
|
||||
|
||||
type typeForTest []*map[int32]string
|
@ -1,152 +0,0 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
fuzz "github.com/google/gofuzz"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
func Test_Roundtrip(t *testing.T) {
|
||||
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
|
||||
for i := 0; i < 100; i++ {
|
||||
var before typeForTest
|
||||
fz.Fuzz(&before)
|
||||
|
||||
jbStd, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with stdlib: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbStd))) == 0 {
|
||||
t.Fatal("stdlib marshal produced empty result and no error")
|
||||
}
|
||||
jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with jsoniter: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbIter))) == 0 {
|
||||
t.Fatal("jsoniter marshal produced empty result and no error")
|
||||
}
|
||||
if string(jbStd) != string(jbIter) {
|
||||
t.Fatalf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
|
||||
indent(jbStd, " "), indent(jbIter, " "), dump(before))
|
||||
}
|
||||
|
||||
var afterStd typeForTest
|
||||
err = json.Unmarshal(jbIter, &afterStd)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
var afterIter typeForTest
|
||||
err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||
t.Fatalf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
|
||||
dump(afterStd), dump(afterIter), indent(jbIter, " "))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const indentStr = "> "
|
||||
|
||||
func fingerprint(obj interface{}) string {
|
||||
c := spew.ConfigState{
|
||||
SortKeys: true,
|
||||
SpewKeys: true,
|
||||
}
|
||||
return c.Sprintf("%v", obj)
|
||||
}
|
||||
|
||||
func dump(obj interface{}) string {
|
||||
cfg := spew.ConfigState{
|
||||
Indent: indentStr,
|
||||
}
|
||||
return cfg.Sdump(obj)
|
||||
}
|
||||
|
||||
func indent(src []byte, prefix string) string {
|
||||
var buf bytes.Buffer
|
||||
err := json.Indent(&buf, src, prefix, indentStr)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("!!! %v", err)
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var obj typeForTest
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&obj)
|
||||
for i := 0; i < t.N; i++ {
|
||||
jb, err := fn(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var before typeForTest
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&before)
|
||||
jb, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal: %v", err)
|
||||
}
|
||||
|
||||
for i := 0; i < t.N; i++ {
|
||||
var after typeForTest
|
||||
err = fn(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStandardMarshal(t *testing.B) {
|
||||
benchmarkMarshal(t, "stdlib", json.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "stdlib", json.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalFastest(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalFastest(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalDefault(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalDefault(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalCompatible(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal)
|
||||
}
|
@ -1,3 +0,0 @@
|
||||
package test
|
||||
|
||||
type typeForTest []*map[string]string
|
@ -1,152 +0,0 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
fuzz "github.com/google/gofuzz"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
func Test_Roundtrip(t *testing.T) {
|
||||
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
|
||||
for i := 0; i < 100; i++ {
|
||||
var before typeForTest
|
||||
fz.Fuzz(&before)
|
||||
|
||||
jbStd, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with stdlib: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbStd))) == 0 {
|
||||
t.Fatal("stdlib marshal produced empty result and no error")
|
||||
}
|
||||
jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with jsoniter: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbIter))) == 0 {
|
||||
t.Fatal("jsoniter marshal produced empty result and no error")
|
||||
}
|
||||
if string(jbStd) != string(jbIter) {
|
||||
t.Fatalf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
|
||||
indent(jbStd, " "), indent(jbIter, " "), dump(before))
|
||||
}
|
||||
|
||||
var afterStd typeForTest
|
||||
err = json.Unmarshal(jbIter, &afterStd)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
var afterIter typeForTest
|
||||
err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||
t.Fatalf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
|
||||
dump(afterStd), dump(afterIter), indent(jbIter, " "))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const indentStr = "> "
|
||||
|
||||
func fingerprint(obj interface{}) string {
|
||||
c := spew.ConfigState{
|
||||
SortKeys: true,
|
||||
SpewKeys: true,
|
||||
}
|
||||
return c.Sprintf("%v", obj)
|
||||
}
|
||||
|
||||
func dump(obj interface{}) string {
|
||||
cfg := spew.ConfigState{
|
||||
Indent: indentStr,
|
||||
}
|
||||
return cfg.Sdump(obj)
|
||||
}
|
||||
|
||||
func indent(src []byte, prefix string) string {
|
||||
var buf bytes.Buffer
|
||||
err := json.Indent(&buf, src, prefix, indentStr)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("!!! %v", err)
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var obj typeForTest
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&obj)
|
||||
for i := 0; i < t.N; i++ {
|
||||
jb, err := fn(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var before typeForTest
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&before)
|
||||
jb, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal: %v", err)
|
||||
}
|
||||
|
||||
for i := 0; i < t.N; i++ {
|
||||
var after typeForTest
|
||||
err = fn(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStandardMarshal(t *testing.B) {
|
||||
benchmarkMarshal(t, "stdlib", json.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "stdlib", json.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalFastest(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalFastest(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalDefault(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalDefault(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalCompatible(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal)
|
||||
}
|
@ -1,3 +0,0 @@
|
||||
package test
|
||||
|
||||
type typeForTest []*[]bool
|
@ -1,152 +0,0 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
fuzz "github.com/google/gofuzz"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
func Test_Roundtrip(t *testing.T) {
|
||||
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
|
||||
for i := 0; i < 100; i++ {
|
||||
var before typeForTest
|
||||
fz.Fuzz(&before)
|
||||
|
||||
jbStd, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with stdlib: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbStd))) == 0 {
|
||||
t.Fatal("stdlib marshal produced empty result and no error")
|
||||
}
|
||||
jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with jsoniter: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbIter))) == 0 {
|
||||
t.Fatal("jsoniter marshal produced empty result and no error")
|
||||
}
|
||||
if string(jbStd) != string(jbIter) {
|
||||
t.Fatalf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
|
||||
indent(jbStd, " "), indent(jbIter, " "), dump(before))
|
||||
}
|
||||
|
||||
var afterStd typeForTest
|
||||
err = json.Unmarshal(jbIter, &afterStd)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
var afterIter typeForTest
|
||||
err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||
t.Fatalf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
|
||||
dump(afterStd), dump(afterIter), indent(jbIter, " "))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const indentStr = "> "
|
||||
|
||||
func fingerprint(obj interface{}) string {
|
||||
c := spew.ConfigState{
|
||||
SortKeys: true,
|
||||
SpewKeys: true,
|
||||
}
|
||||
return c.Sprintf("%v", obj)
|
||||
}
|
||||
|
||||
func dump(obj interface{}) string {
|
||||
cfg := spew.ConfigState{
|
||||
Indent: indentStr,
|
||||
}
|
||||
return cfg.Sdump(obj)
|
||||
}
|
||||
|
||||
func indent(src []byte, prefix string) string {
|
||||
var buf bytes.Buffer
|
||||
err := json.Indent(&buf, src, prefix, indentStr)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("!!! %v", err)
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var obj typeForTest
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&obj)
|
||||
for i := 0; i < t.N; i++ {
|
||||
jb, err := fn(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var before typeForTest
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&before)
|
||||
jb, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal: %v", err)
|
||||
}
|
||||
|
||||
for i := 0; i < t.N; i++ {
|
||||
var after typeForTest
|
||||
err = fn(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStandardMarshal(t *testing.B) {
|
||||
benchmarkMarshal(t, "stdlib", json.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "stdlib", json.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalFastest(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalFastest(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalDefault(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalDefault(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalCompatible(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal)
|
||||
}
|
@ -1,3 +0,0 @@
|
||||
package test
|
||||
|
||||
type typeForTest []*[]byte
|
@ -1,152 +0,0 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
fuzz "github.com/google/gofuzz"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
func Test_Roundtrip(t *testing.T) {
|
||||
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
|
||||
for i := 0; i < 100; i++ {
|
||||
var before typeForTest
|
||||
fz.Fuzz(&before)
|
||||
|
||||
jbStd, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with stdlib: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbStd))) == 0 {
|
||||
t.Fatal("stdlib marshal produced empty result and no error")
|
||||
}
|
||||
jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with jsoniter: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbIter))) == 0 {
|
||||
t.Fatal("jsoniter marshal produced empty result and no error")
|
||||
}
|
||||
if string(jbStd) != string(jbIter) {
|
||||
t.Fatalf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
|
||||
indent(jbStd, " "), indent(jbIter, " "), dump(before))
|
||||
}
|
||||
|
||||
var afterStd typeForTest
|
||||
err = json.Unmarshal(jbIter, &afterStd)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
var afterIter typeForTest
|
||||
err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||
t.Fatalf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
|
||||
dump(afterStd), dump(afterIter), indent(jbIter, " "))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const indentStr = "> "
|
||||
|
||||
func fingerprint(obj interface{}) string {
|
||||
c := spew.ConfigState{
|
||||
SortKeys: true,
|
||||
SpewKeys: true,
|
||||
}
|
||||
return c.Sprintf("%v", obj)
|
||||
}
|
||||
|
||||
func dump(obj interface{}) string {
|
||||
cfg := spew.ConfigState{
|
||||
Indent: indentStr,
|
||||
}
|
||||
return cfg.Sdump(obj)
|
||||
}
|
||||
|
||||
func indent(src []byte, prefix string) string {
|
||||
var buf bytes.Buffer
|
||||
err := json.Indent(&buf, src, prefix, indentStr)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("!!! %v", err)
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var obj typeForTest
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&obj)
|
||||
for i := 0; i < t.N; i++ {
|
||||
jb, err := fn(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var before typeForTest
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&before)
|
||||
jb, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal: %v", err)
|
||||
}
|
||||
|
||||
for i := 0; i < t.N; i++ {
|
||||
var after typeForTest
|
||||
err = fn(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStandardMarshal(t *testing.B) {
|
||||
benchmarkMarshal(t, "stdlib", json.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "stdlib", json.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalFastest(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalFastest(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalDefault(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalDefault(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalCompatible(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal)
|
||||
}
|
@ -1,3 +0,0 @@
|
||||
package test
|
||||
|
||||
type typeForTest []*[]float64
|
@ -1,152 +0,0 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
fuzz "github.com/google/gofuzz"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
func Test_Roundtrip(t *testing.T) {
|
||||
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
|
||||
for i := 0; i < 100; i++ {
|
||||
var before typeForTest
|
||||
fz.Fuzz(&before)
|
||||
|
||||
jbStd, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with stdlib: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbStd))) == 0 {
|
||||
t.Fatal("stdlib marshal produced empty result and no error")
|
||||
}
|
||||
jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with jsoniter: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbIter))) == 0 {
|
||||
t.Fatal("jsoniter marshal produced empty result and no error")
|
||||
}
|
||||
if string(jbStd) != string(jbIter) {
|
||||
t.Fatalf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
|
||||
indent(jbStd, " "), indent(jbIter, " "), dump(before))
|
||||
}
|
||||
|
||||
var afterStd typeForTest
|
||||
err = json.Unmarshal(jbIter, &afterStd)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
var afterIter typeForTest
|
||||
err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||
t.Fatalf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
|
||||
dump(afterStd), dump(afterIter), indent(jbIter, " "))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const indentStr = "> "
|
||||
|
||||
func fingerprint(obj interface{}) string {
|
||||
c := spew.ConfigState{
|
||||
SortKeys: true,
|
||||
SpewKeys: true,
|
||||
}
|
||||
return c.Sprintf("%v", obj)
|
||||
}
|
||||
|
||||
func dump(obj interface{}) string {
|
||||
cfg := spew.ConfigState{
|
||||
Indent: indentStr,
|
||||
}
|
||||
return cfg.Sdump(obj)
|
||||
}
|
||||
|
||||
func indent(src []byte, prefix string) string {
|
||||
var buf bytes.Buffer
|
||||
err := json.Indent(&buf, src, prefix, indentStr)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("!!! %v", err)
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var obj typeForTest
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&obj)
|
||||
for i := 0; i < t.N; i++ {
|
||||
jb, err := fn(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var before typeForTest
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&before)
|
||||
jb, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal: %v", err)
|
||||
}
|
||||
|
||||
for i := 0; i < t.N; i++ {
|
||||
var after typeForTest
|
||||
err = fn(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStandardMarshal(t *testing.B) {
|
||||
benchmarkMarshal(t, "stdlib", json.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "stdlib", json.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalFastest(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalFastest(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalDefault(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalDefault(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalCompatible(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal)
|
||||
}
|
@ -1,3 +0,0 @@
|
||||
package test
|
||||
|
||||
type typeForTest []*[]int32
|
@ -1,152 +0,0 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
fuzz "github.com/google/gofuzz"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
func Test_Roundtrip(t *testing.T) {
|
||||
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
|
||||
for i := 0; i < 100; i++ {
|
||||
var before typeForTest
|
||||
fz.Fuzz(&before)
|
||||
|
||||
jbStd, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with stdlib: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbStd))) == 0 {
|
||||
t.Fatal("stdlib marshal produced empty result and no error")
|
||||
}
|
||||
jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with jsoniter: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbIter))) == 0 {
|
||||
t.Fatal("jsoniter marshal produced empty result and no error")
|
||||
}
|
||||
if string(jbStd) != string(jbIter) {
|
||||
t.Fatalf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
|
||||
indent(jbStd, " "), indent(jbIter, " "), dump(before))
|
||||
}
|
||||
|
||||
var afterStd typeForTest
|
||||
err = json.Unmarshal(jbIter, &afterStd)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
var afterIter typeForTest
|
||||
err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||
t.Fatalf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
|
||||
dump(afterStd), dump(afterIter), indent(jbIter, " "))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const indentStr = "> "
|
||||
|
||||
func fingerprint(obj interface{}) string {
|
||||
c := spew.ConfigState{
|
||||
SortKeys: true,
|
||||
SpewKeys: true,
|
||||
}
|
||||
return c.Sprintf("%v", obj)
|
||||
}
|
||||
|
||||
func dump(obj interface{}) string {
|
||||
cfg := spew.ConfigState{
|
||||
Indent: indentStr,
|
||||
}
|
||||
return cfg.Sdump(obj)
|
||||
}
|
||||
|
||||
func indent(src []byte, prefix string) string {
|
||||
var buf bytes.Buffer
|
||||
err := json.Indent(&buf, src, prefix, indentStr)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("!!! %v", err)
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var obj typeForTest
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&obj)
|
||||
for i := 0; i < t.N; i++ {
|
||||
jb, err := fn(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var before typeForTest
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&before)
|
||||
jb, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal: %v", err)
|
||||
}
|
||||
|
||||
for i := 0; i < t.N; i++ {
|
||||
var after typeForTest
|
||||
err = fn(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStandardMarshal(t *testing.B) {
|
||||
benchmarkMarshal(t, "stdlib", json.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "stdlib", json.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalFastest(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalFastest(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalDefault(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalDefault(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalCompatible(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal)
|
||||
}
|
@ -1,3 +0,0 @@
|
||||
package test
|
||||
|
||||
type typeForTest []*[]*string
|
@ -1,152 +0,0 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
fuzz "github.com/google/gofuzz"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
func Test_Roundtrip(t *testing.T) {
|
||||
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
|
||||
for i := 0; i < 100; i++ {
|
||||
var before typeForTest
|
||||
fz.Fuzz(&before)
|
||||
|
||||
jbStd, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with stdlib: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbStd))) == 0 {
|
||||
t.Fatal("stdlib marshal produced empty result and no error")
|
||||
}
|
||||
jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with jsoniter: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbIter))) == 0 {
|
||||
t.Fatal("jsoniter marshal produced empty result and no error")
|
||||
}
|
||||
if string(jbStd) != string(jbIter) {
|
||||
t.Fatalf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
|
||||
indent(jbStd, " "), indent(jbIter, " "), dump(before))
|
||||
}
|
||||
|
||||
var afterStd typeForTest
|
||||
err = json.Unmarshal(jbIter, &afterStd)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
var afterIter typeForTest
|
||||
err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||
t.Fatalf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
|
||||
dump(afterStd), dump(afterIter), indent(jbIter, " "))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const indentStr = "> "
|
||||
|
||||
func fingerprint(obj interface{}) string {
|
||||
c := spew.ConfigState{
|
||||
SortKeys: true,
|
||||
SpewKeys: true,
|
||||
}
|
||||
return c.Sprintf("%v", obj)
|
||||
}
|
||||
|
||||
func dump(obj interface{}) string {
|
||||
cfg := spew.ConfigState{
|
||||
Indent: indentStr,
|
||||
}
|
||||
return cfg.Sdump(obj)
|
||||
}
|
||||
|
||||
func indent(src []byte, prefix string) string {
|
||||
var buf bytes.Buffer
|
||||
err := json.Indent(&buf, src, prefix, indentStr)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("!!! %v", err)
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var obj typeForTest
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&obj)
|
||||
for i := 0; i < t.N; i++ {
|
||||
jb, err := fn(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var before typeForTest
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&before)
|
||||
jb, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal: %v", err)
|
||||
}
|
||||
|
||||
for i := 0; i < t.N; i++ {
|
||||
var after typeForTest
|
||||
err = fn(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStandardMarshal(t *testing.B) {
|
||||
benchmarkMarshal(t, "stdlib", json.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "stdlib", json.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalFastest(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalFastest(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalDefault(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalDefault(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalCompatible(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal)
|
||||
}
|
@ -1,3 +0,0 @@
|
||||
package test
|
||||
|
||||
type typeForTest []*[]string
|
@ -1,152 +0,0 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
fuzz "github.com/google/gofuzz"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
func Test_Roundtrip(t *testing.T) {
|
||||
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
|
||||
for i := 0; i < 100; i++ {
|
||||
var before typeForTest
|
||||
fz.Fuzz(&before)
|
||||
|
||||
jbStd, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with stdlib: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbStd))) == 0 {
|
||||
t.Fatal("stdlib marshal produced empty result and no error")
|
||||
}
|
||||
jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with jsoniter: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbIter))) == 0 {
|
||||
t.Fatal("jsoniter marshal produced empty result and no error")
|
||||
}
|
||||
if string(jbStd) != string(jbIter) {
|
||||
t.Fatalf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
|
||||
indent(jbStd, " "), indent(jbIter, " "), dump(before))
|
||||
}
|
||||
|
||||
var afterStd typeForTest
|
||||
err = json.Unmarshal(jbIter, &afterStd)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
var afterIter typeForTest
|
||||
err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||
t.Fatalf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
|
||||
dump(afterStd), dump(afterIter), indent(jbIter, " "))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const indentStr = "> "
|
||||
|
||||
func fingerprint(obj interface{}) string {
|
||||
c := spew.ConfigState{
|
||||
SortKeys: true,
|
||||
SpewKeys: true,
|
||||
}
|
||||
return c.Sprintf("%v", obj)
|
||||
}
|
||||
|
||||
func dump(obj interface{}) string {
|
||||
cfg := spew.ConfigState{
|
||||
Indent: indentStr,
|
||||
}
|
||||
return cfg.Sdump(obj)
|
||||
}
|
||||
|
||||
func indent(src []byte, prefix string) string {
|
||||
var buf bytes.Buffer
|
||||
err := json.Indent(&buf, src, prefix, indentStr)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("!!! %v", err)
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var obj typeForTest
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&obj)
|
||||
for i := 0; i < t.N; i++ {
|
||||
jb, err := fn(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var before typeForTest
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&before)
|
||||
jb, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal: %v", err)
|
||||
}
|
||||
|
||||
for i := 0; i < t.N; i++ {
|
||||
var after typeForTest
|
||||
err = fn(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStandardMarshal(t *testing.B) {
|
||||
benchmarkMarshal(t, "stdlib", json.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "stdlib", json.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalFastest(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalFastest(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalDefault(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalDefault(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalCompatible(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal)
|
||||
}
|
@ -1,3 +0,0 @@
|
||||
package test
|
||||
|
||||
type typeForTest []*[]uint8
|
@ -1,152 +0,0 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
fuzz "github.com/google/gofuzz"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
func Test_Roundtrip(t *testing.T) {
|
||||
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
|
||||
for i := 0; i < 100; i++ {
|
||||
var before typeForTest
|
||||
fz.Fuzz(&before)
|
||||
|
||||
jbStd, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with stdlib: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbStd))) == 0 {
|
||||
t.Fatal("stdlib marshal produced empty result and no error")
|
||||
}
|
||||
jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with jsoniter: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbIter))) == 0 {
|
||||
t.Fatal("jsoniter marshal produced empty result and no error")
|
||||
}
|
||||
if string(jbStd) != string(jbIter) {
|
||||
t.Fatalf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
|
||||
indent(jbStd, " "), indent(jbIter, " "), dump(before))
|
||||
}
|
||||
|
||||
var afterStd typeForTest
|
||||
err = json.Unmarshal(jbIter, &afterStd)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
var afterIter typeForTest
|
||||
err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||
t.Fatalf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
|
||||
dump(afterStd), dump(afterIter), indent(jbIter, " "))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const indentStr = "> "
|
||||
|
||||
func fingerprint(obj interface{}) string {
|
||||
c := spew.ConfigState{
|
||||
SortKeys: true,
|
||||
SpewKeys: true,
|
||||
}
|
||||
return c.Sprintf("%v", obj)
|
||||
}
|
||||
|
||||
func dump(obj interface{}) string {
|
||||
cfg := spew.ConfigState{
|
||||
Indent: indentStr,
|
||||
}
|
||||
return cfg.Sdump(obj)
|
||||
}
|
||||
|
||||
func indent(src []byte, prefix string) string {
|
||||
var buf bytes.Buffer
|
||||
err := json.Indent(&buf, src, prefix, indentStr)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("!!! %v", err)
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var obj typeForTest
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&obj)
|
||||
for i := 0; i < t.N; i++ {
|
||||
jb, err := fn(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var before typeForTest
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&before)
|
||||
jb, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal: %v", err)
|
||||
}
|
||||
|
||||
for i := 0; i < t.N; i++ {
|
||||
var after typeForTest
|
||||
err = fn(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStandardMarshal(t *testing.B) {
|
||||
benchmarkMarshal(t, "stdlib", json.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "stdlib", json.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalFastest(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalFastest(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalDefault(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalDefault(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalCompatible(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal)
|
||||
}
|
@ -1,3 +0,0 @@
|
||||
package test
|
||||
|
||||
type typeForTest []*string
|
@ -1,152 +0,0 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
fuzz "github.com/google/gofuzz"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
func Test_Roundtrip(t *testing.T) {
|
||||
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
|
||||
for i := 0; i < 100; i++ {
|
||||
var before typeForTest
|
||||
fz.Fuzz(&before)
|
||||
|
||||
jbStd, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with stdlib: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbStd))) == 0 {
|
||||
t.Fatal("stdlib marshal produced empty result and no error")
|
||||
}
|
||||
jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with jsoniter: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbIter))) == 0 {
|
||||
t.Fatal("jsoniter marshal produced empty result and no error")
|
||||
}
|
||||
if string(jbStd) != string(jbIter) {
|
||||
t.Fatalf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
|
||||
indent(jbStd, " "), indent(jbIter, " "), dump(before))
|
||||
}
|
||||
|
||||
var afterStd typeForTest
|
||||
err = json.Unmarshal(jbIter, &afterStd)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
var afterIter typeForTest
|
||||
err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||
t.Fatalf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
|
||||
dump(afterStd), dump(afterIter), indent(jbIter, " "))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const indentStr = "> "
|
||||
|
||||
func fingerprint(obj interface{}) string {
|
||||
c := spew.ConfigState{
|
||||
SortKeys: true,
|
||||
SpewKeys: true,
|
||||
}
|
||||
return c.Sprintf("%v", obj)
|
||||
}
|
||||
|
||||
func dump(obj interface{}) string {
|
||||
cfg := spew.ConfigState{
|
||||
Indent: indentStr,
|
||||
}
|
||||
return cfg.Sdump(obj)
|
||||
}
|
||||
|
||||
func indent(src []byte, prefix string) string {
|
||||
var buf bytes.Buffer
|
||||
err := json.Indent(&buf, src, prefix, indentStr)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("!!! %v", err)
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var obj typeForTest
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&obj)
|
||||
for i := 0; i < t.N; i++ {
|
||||
jb, err := fn(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var before typeForTest
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&before)
|
||||
jb, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal: %v", err)
|
||||
}
|
||||
|
||||
for i := 0; i < t.N; i++ {
|
||||
var after typeForTest
|
||||
err = fn(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStandardMarshal(t *testing.B) {
|
||||
benchmarkMarshal(t, "stdlib", json.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "stdlib", json.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalFastest(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalFastest(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalDefault(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalDefault(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalCompatible(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal)
|
||||
}
|
@ -1,12 +0,0 @@
|
||||
package test
|
||||
|
||||
type typeForTest []*struct {
|
||||
String string
|
||||
Int int32
|
||||
Float float64
|
||||
Struct struct {
|
||||
X string
|
||||
}
|
||||
Slice []string
|
||||
Map map[string]string
|
||||
}
|
@ -1,152 +0,0 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
fuzz "github.com/google/gofuzz"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
func Test_Roundtrip(t *testing.T) {
|
||||
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
|
||||
for i := 0; i < 100; i++ {
|
||||
var before typeForTest
|
||||
fz.Fuzz(&before)
|
||||
|
||||
jbStd, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with stdlib: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbStd))) == 0 {
|
||||
t.Fatal("stdlib marshal produced empty result and no error")
|
||||
}
|
||||
jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with jsoniter: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbIter))) == 0 {
|
||||
t.Fatal("jsoniter marshal produced empty result and no error")
|
||||
}
|
||||
if string(jbStd) != string(jbIter) {
|
||||
t.Fatalf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
|
||||
indent(jbStd, " "), indent(jbIter, " "), dump(before))
|
||||
}
|
||||
|
||||
var afterStd typeForTest
|
||||
err = json.Unmarshal(jbIter, &afterStd)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
var afterIter typeForTest
|
||||
err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||
t.Fatalf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
|
||||
dump(afterStd), dump(afterIter), indent(jbIter, " "))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const indentStr = "> "
|
||||
|
||||
func fingerprint(obj interface{}) string {
|
||||
c := spew.ConfigState{
|
||||
SortKeys: true,
|
||||
SpewKeys: true,
|
||||
}
|
||||
return c.Sprintf("%v", obj)
|
||||
}
|
||||
|
||||
func dump(obj interface{}) string {
|
||||
cfg := spew.ConfigState{
|
||||
Indent: indentStr,
|
||||
}
|
||||
return cfg.Sdump(obj)
|
||||
}
|
||||
|
||||
func indent(src []byte, prefix string) string {
|
||||
var buf bytes.Buffer
|
||||
err := json.Indent(&buf, src, prefix, indentStr)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("!!! %v", err)
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var obj typeForTest
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&obj)
|
||||
for i := 0; i < t.N; i++ {
|
||||
jb, err := fn(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var before typeForTest
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&before)
|
||||
jb, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal: %v", err)
|
||||
}
|
||||
|
||||
for i := 0; i < t.N; i++ {
|
||||
var after typeForTest
|
||||
err = fn(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStandardMarshal(t *testing.B) {
|
||||
benchmarkMarshal(t, "stdlib", json.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "stdlib", json.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalFastest(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalFastest(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalDefault(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalDefault(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalCompatible(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal)
|
||||
}
|
@ -1,3 +0,0 @@
|
||||
package test
|
||||
|
||||
type typeForTest []*uint8
|
@ -1,152 +0,0 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
fuzz "github.com/google/gofuzz"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
func Test_Roundtrip(t *testing.T) {
|
||||
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
|
||||
for i := 0; i < 100; i++ {
|
||||
var before typeForTest
|
||||
fz.Fuzz(&before)
|
||||
|
||||
jbStd, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with stdlib: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbStd))) == 0 {
|
||||
t.Fatal("stdlib marshal produced empty result and no error")
|
||||
}
|
||||
jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with jsoniter: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbIter))) == 0 {
|
||||
t.Fatal("jsoniter marshal produced empty result and no error")
|
||||
}
|
||||
if string(jbStd) != string(jbIter) {
|
||||
t.Fatalf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
|
||||
indent(jbStd, " "), indent(jbIter, " "), dump(before))
|
||||
}
|
||||
|
||||
var afterStd typeForTest
|
||||
err = json.Unmarshal(jbIter, &afterStd)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
var afterIter typeForTest
|
||||
err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||
t.Fatalf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
|
||||
dump(afterStd), dump(afterIter), indent(jbIter, " "))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const indentStr = "> "
|
||||
|
||||
func fingerprint(obj interface{}) string {
|
||||
c := spew.ConfigState{
|
||||
SortKeys: true,
|
||||
SpewKeys: true,
|
||||
}
|
||||
return c.Sprintf("%v", obj)
|
||||
}
|
||||
|
||||
func dump(obj interface{}) string {
|
||||
cfg := spew.ConfigState{
|
||||
Indent: indentStr,
|
||||
}
|
||||
return cfg.Sdump(obj)
|
||||
}
|
||||
|
||||
func indent(src []byte, prefix string) string {
|
||||
var buf bytes.Buffer
|
||||
err := json.Indent(&buf, src, prefix, indentStr)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("!!! %v", err)
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var obj typeForTest
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&obj)
|
||||
for i := 0; i < t.N; i++ {
|
||||
jb, err := fn(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var before typeForTest
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&before)
|
||||
jb, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal: %v", err)
|
||||
}
|
||||
|
||||
for i := 0; i < t.N; i++ {
|
||||
var after typeForTest
|
||||
err = fn(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStandardMarshal(t *testing.B) {
|
||||
benchmarkMarshal(t, "stdlib", json.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "stdlib", json.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalFastest(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalFastest(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalDefault(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalDefault(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalCompatible(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal)
|
||||
}
|
@ -1,3 +0,0 @@
|
||||
package test
|
||||
|
||||
type typeForTest [][]bool
|
@ -1,152 +0,0 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
fuzz "github.com/google/gofuzz"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
func Test_Roundtrip(t *testing.T) {
|
||||
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
|
||||
for i := 0; i < 100; i++ {
|
||||
var before typeForTest
|
||||
fz.Fuzz(&before)
|
||||
|
||||
jbStd, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with stdlib: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbStd))) == 0 {
|
||||
t.Fatal("stdlib marshal produced empty result and no error")
|
||||
}
|
||||
jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with jsoniter: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbIter))) == 0 {
|
||||
t.Fatal("jsoniter marshal produced empty result and no error")
|
||||
}
|
||||
if string(jbStd) != string(jbIter) {
|
||||
t.Fatalf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
|
||||
indent(jbStd, " "), indent(jbIter, " "), dump(before))
|
||||
}
|
||||
|
||||
var afterStd typeForTest
|
||||
err = json.Unmarshal(jbIter, &afterStd)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
var afterIter typeForTest
|
||||
err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||
t.Fatalf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
|
||||
dump(afterStd), dump(afterIter), indent(jbIter, " "))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const indentStr = "> "
|
||||
|
||||
func fingerprint(obj interface{}) string {
|
||||
c := spew.ConfigState{
|
||||
SortKeys: true,
|
||||
SpewKeys: true,
|
||||
}
|
||||
return c.Sprintf("%v", obj)
|
||||
}
|
||||
|
||||
func dump(obj interface{}) string {
|
||||
cfg := spew.ConfigState{
|
||||
Indent: indentStr,
|
||||
}
|
||||
return cfg.Sdump(obj)
|
||||
}
|
||||
|
||||
func indent(src []byte, prefix string) string {
|
||||
var buf bytes.Buffer
|
||||
err := json.Indent(&buf, src, prefix, indentStr)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("!!! %v", err)
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var obj typeForTest
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&obj)
|
||||
for i := 0; i < t.N; i++ {
|
||||
jb, err := fn(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var before typeForTest
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&before)
|
||||
jb, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal: %v", err)
|
||||
}
|
||||
|
||||
for i := 0; i < t.N; i++ {
|
||||
var after typeForTest
|
||||
err = fn(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStandardMarshal(t *testing.B) {
|
||||
benchmarkMarshal(t, "stdlib", json.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "stdlib", json.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalFastest(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalFastest(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalDefault(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalDefault(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalCompatible(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal)
|
||||
}
|
@ -1,3 +0,0 @@
|
||||
package test
|
||||
|
||||
type typeForTest [][]byte
|
@ -1,152 +0,0 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
fuzz "github.com/google/gofuzz"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
func Test_Roundtrip(t *testing.T) {
|
||||
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
|
||||
for i := 0; i < 100; i++ {
|
||||
var before typeForTest
|
||||
fz.Fuzz(&before)
|
||||
|
||||
jbStd, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with stdlib: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbStd))) == 0 {
|
||||
t.Fatal("stdlib marshal produced empty result and no error")
|
||||
}
|
||||
jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with jsoniter: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbIter))) == 0 {
|
||||
t.Fatal("jsoniter marshal produced empty result and no error")
|
||||
}
|
||||
if string(jbStd) != string(jbIter) {
|
||||
t.Fatalf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
|
||||
indent(jbStd, " "), indent(jbIter, " "), dump(before))
|
||||
}
|
||||
|
||||
var afterStd typeForTest
|
||||
err = json.Unmarshal(jbIter, &afterStd)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
var afterIter typeForTest
|
||||
err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||
t.Fatalf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
|
||||
dump(afterStd), dump(afterIter), indent(jbIter, " "))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const indentStr = "> "
|
||||
|
||||
func fingerprint(obj interface{}) string {
|
||||
c := spew.ConfigState{
|
||||
SortKeys: true,
|
||||
SpewKeys: true,
|
||||
}
|
||||
return c.Sprintf("%v", obj)
|
||||
}
|
||||
|
||||
func dump(obj interface{}) string {
|
||||
cfg := spew.ConfigState{
|
||||
Indent: indentStr,
|
||||
}
|
||||
return cfg.Sdump(obj)
|
||||
}
|
||||
|
||||
func indent(src []byte, prefix string) string {
|
||||
var buf bytes.Buffer
|
||||
err := json.Indent(&buf, src, prefix, indentStr)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("!!! %v", err)
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var obj typeForTest
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&obj)
|
||||
for i := 0; i < t.N; i++ {
|
||||
jb, err := fn(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var before typeForTest
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&before)
|
||||
jb, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal: %v", err)
|
||||
}
|
||||
|
||||
for i := 0; i < t.N; i++ {
|
||||
var after typeForTest
|
||||
err = fn(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStandardMarshal(t *testing.B) {
|
||||
benchmarkMarshal(t, "stdlib", json.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "stdlib", json.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalFastest(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalFastest(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalDefault(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalDefault(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalCompatible(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal)
|
||||
}
|
@ -1,3 +0,0 @@
|
||||
package test
|
||||
|
||||
type typeForTest [][]float64
|
@ -1,152 +0,0 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
fuzz "github.com/google/gofuzz"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
func Test_Roundtrip(t *testing.T) {
|
||||
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
|
||||
for i := 0; i < 100; i++ {
|
||||
var before typeForTest
|
||||
fz.Fuzz(&before)
|
||||
|
||||
jbStd, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with stdlib: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbStd))) == 0 {
|
||||
t.Fatal("stdlib marshal produced empty result and no error")
|
||||
}
|
||||
jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with jsoniter: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbIter))) == 0 {
|
||||
t.Fatal("jsoniter marshal produced empty result and no error")
|
||||
}
|
||||
if string(jbStd) != string(jbIter) {
|
||||
t.Fatalf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
|
||||
indent(jbStd, " "), indent(jbIter, " "), dump(before))
|
||||
}
|
||||
|
||||
var afterStd typeForTest
|
||||
err = json.Unmarshal(jbIter, &afterStd)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
var afterIter typeForTest
|
||||
err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||
t.Fatalf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
|
||||
dump(afterStd), dump(afterIter), indent(jbIter, " "))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const indentStr = "> "
|
||||
|
||||
func fingerprint(obj interface{}) string {
|
||||
c := spew.ConfigState{
|
||||
SortKeys: true,
|
||||
SpewKeys: true,
|
||||
}
|
||||
return c.Sprintf("%v", obj)
|
||||
}
|
||||
|
||||
func dump(obj interface{}) string {
|
||||
cfg := spew.ConfigState{
|
||||
Indent: indentStr,
|
||||
}
|
||||
return cfg.Sdump(obj)
|
||||
}
|
||||
|
||||
func indent(src []byte, prefix string) string {
|
||||
var buf bytes.Buffer
|
||||
err := json.Indent(&buf, src, prefix, indentStr)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("!!! %v", err)
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var obj typeForTest
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&obj)
|
||||
for i := 0; i < t.N; i++ {
|
||||
jb, err := fn(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var before typeForTest
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&before)
|
||||
jb, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal: %v", err)
|
||||
}
|
||||
|
||||
for i := 0; i < t.N; i++ {
|
||||
var after typeForTest
|
||||
err = fn(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStandardMarshal(t *testing.B) {
|
||||
benchmarkMarshal(t, "stdlib", json.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "stdlib", json.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalFastest(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalFastest(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalDefault(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalDefault(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalCompatible(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal)
|
||||
}
|
@ -1,3 +0,0 @@
|
||||
package test
|
||||
|
||||
type typeForTest [][]int32
|
@ -1,152 +0,0 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
fuzz "github.com/google/gofuzz"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
func Test_Roundtrip(t *testing.T) {
|
||||
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
|
||||
for i := 0; i < 100; i++ {
|
||||
var before typeForTest
|
||||
fz.Fuzz(&before)
|
||||
|
||||
jbStd, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with stdlib: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbStd))) == 0 {
|
||||
t.Fatal("stdlib marshal produced empty result and no error")
|
||||
}
|
||||
jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with jsoniter: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbIter))) == 0 {
|
||||
t.Fatal("jsoniter marshal produced empty result and no error")
|
||||
}
|
||||
if string(jbStd) != string(jbIter) {
|
||||
t.Fatalf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
|
||||
indent(jbStd, " "), indent(jbIter, " "), dump(before))
|
||||
}
|
||||
|
||||
var afterStd typeForTest
|
||||
err = json.Unmarshal(jbIter, &afterStd)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
var afterIter typeForTest
|
||||
err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||
t.Fatalf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
|
||||
dump(afterStd), dump(afterIter), indent(jbIter, " "))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const indentStr = "> "
|
||||
|
||||
func fingerprint(obj interface{}) string {
|
||||
c := spew.ConfigState{
|
||||
SortKeys: true,
|
||||
SpewKeys: true,
|
||||
}
|
||||
return c.Sprintf("%v", obj)
|
||||
}
|
||||
|
||||
func dump(obj interface{}) string {
|
||||
cfg := spew.ConfigState{
|
||||
Indent: indentStr,
|
||||
}
|
||||
return cfg.Sdump(obj)
|
||||
}
|
||||
|
||||
func indent(src []byte, prefix string) string {
|
||||
var buf bytes.Buffer
|
||||
err := json.Indent(&buf, src, prefix, indentStr)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("!!! %v", err)
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var obj typeForTest
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&obj)
|
||||
for i := 0; i < t.N; i++ {
|
||||
jb, err := fn(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var before typeForTest
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&before)
|
||||
jb, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal: %v", err)
|
||||
}
|
||||
|
||||
for i := 0; i < t.N; i++ {
|
||||
var after typeForTest
|
||||
err = fn(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStandardMarshal(t *testing.B) {
|
||||
benchmarkMarshal(t, "stdlib", json.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "stdlib", json.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalFastest(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalFastest(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalDefault(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalDefault(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalCompatible(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal)
|
||||
}
|
@ -1,3 +0,0 @@
|
||||
package test
|
||||
|
||||
type typeForTest [][]*string
|
@ -1,152 +0,0 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
fuzz "github.com/google/gofuzz"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
func Test_Roundtrip(t *testing.T) {
|
||||
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
|
||||
for i := 0; i < 100; i++ {
|
||||
var before typeForTest
|
||||
fz.Fuzz(&before)
|
||||
|
||||
jbStd, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with stdlib: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbStd))) == 0 {
|
||||
t.Fatal("stdlib marshal produced empty result and no error")
|
||||
}
|
||||
jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with jsoniter: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbIter))) == 0 {
|
||||
t.Fatal("jsoniter marshal produced empty result and no error")
|
||||
}
|
||||
if string(jbStd) != string(jbIter) {
|
||||
t.Fatalf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
|
||||
indent(jbStd, " "), indent(jbIter, " "), dump(before))
|
||||
}
|
||||
|
||||
var afterStd typeForTest
|
||||
err = json.Unmarshal(jbIter, &afterStd)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
var afterIter typeForTest
|
||||
err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||
t.Fatalf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
|
||||
dump(afterStd), dump(afterIter), indent(jbIter, " "))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const indentStr = "> "
|
||||
|
||||
func fingerprint(obj interface{}) string {
|
||||
c := spew.ConfigState{
|
||||
SortKeys: true,
|
||||
SpewKeys: true,
|
||||
}
|
||||
return c.Sprintf("%v", obj)
|
||||
}
|
||||
|
||||
func dump(obj interface{}) string {
|
||||
cfg := spew.ConfigState{
|
||||
Indent: indentStr,
|
||||
}
|
||||
return cfg.Sdump(obj)
|
||||
}
|
||||
|
||||
func indent(src []byte, prefix string) string {
|
||||
var buf bytes.Buffer
|
||||
err := json.Indent(&buf, src, prefix, indentStr)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("!!! %v", err)
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var obj typeForTest
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&obj)
|
||||
for i := 0; i < t.N; i++ {
|
||||
jb, err := fn(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var before typeForTest
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&before)
|
||||
jb, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal: %v", err)
|
||||
}
|
||||
|
||||
for i := 0; i < t.N; i++ {
|
||||
var after typeForTest
|
||||
err = fn(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStandardMarshal(t *testing.B) {
|
||||
benchmarkMarshal(t, "stdlib", json.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "stdlib", json.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalFastest(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalFastest(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalDefault(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalDefault(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalCompatible(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal)
|
||||
}
|
@ -1,3 +0,0 @@
|
||||
package test
|
||||
|
||||
type typeForTest [][]string
|
@ -1,152 +0,0 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
fuzz "github.com/google/gofuzz"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
func Test_Roundtrip(t *testing.T) {
|
||||
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
|
||||
for i := 0; i < 100; i++ {
|
||||
var before typeForTest
|
||||
fz.Fuzz(&before)
|
||||
|
||||
jbStd, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with stdlib: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbStd))) == 0 {
|
||||
t.Fatal("stdlib marshal produced empty result and no error")
|
||||
}
|
||||
jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with jsoniter: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbIter))) == 0 {
|
||||
t.Fatal("jsoniter marshal produced empty result and no error")
|
||||
}
|
||||
if string(jbStd) != string(jbIter) {
|
||||
t.Fatalf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
|
||||
indent(jbStd, " "), indent(jbIter, " "), dump(before))
|
||||
}
|
||||
|
||||
var afterStd typeForTest
|
||||
err = json.Unmarshal(jbIter, &afterStd)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
var afterIter typeForTest
|
||||
err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||
t.Fatalf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
|
||||
dump(afterStd), dump(afterIter), indent(jbIter, " "))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const indentStr = "> "
|
||||
|
||||
func fingerprint(obj interface{}) string {
|
||||
c := spew.ConfigState{
|
||||
SortKeys: true,
|
||||
SpewKeys: true,
|
||||
}
|
||||
return c.Sprintf("%v", obj)
|
||||
}
|
||||
|
||||
func dump(obj interface{}) string {
|
||||
cfg := spew.ConfigState{
|
||||
Indent: indentStr,
|
||||
}
|
||||
return cfg.Sdump(obj)
|
||||
}
|
||||
|
||||
func indent(src []byte, prefix string) string {
|
||||
var buf bytes.Buffer
|
||||
err := json.Indent(&buf, src, prefix, indentStr)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("!!! %v", err)
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var obj typeForTest
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&obj)
|
||||
for i := 0; i < t.N; i++ {
|
||||
jb, err := fn(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var before typeForTest
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&before)
|
||||
jb, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal: %v", err)
|
||||
}
|
||||
|
||||
for i := 0; i < t.N; i++ {
|
||||
var after typeForTest
|
||||
err = fn(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStandardMarshal(t *testing.B) {
|
||||
benchmarkMarshal(t, "stdlib", json.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "stdlib", json.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalFastest(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalFastest(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalDefault(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalDefault(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalCompatible(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal)
|
||||
}
|
@ -1,3 +0,0 @@
|
||||
package test
|
||||
|
||||
type typeForTest [][]uint8
|
@ -1,152 +0,0 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
fuzz "github.com/google/gofuzz"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
func Test_Roundtrip(t *testing.T) {
|
||||
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
|
||||
for i := 0; i < 100; i++ {
|
||||
var before typeForTest
|
||||
fz.Fuzz(&before)
|
||||
|
||||
jbStd, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with stdlib: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbStd))) == 0 {
|
||||
t.Fatal("stdlib marshal produced empty result and no error")
|
||||
}
|
||||
jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with jsoniter: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbIter))) == 0 {
|
||||
t.Fatal("jsoniter marshal produced empty result and no error")
|
||||
}
|
||||
if string(jbStd) != string(jbIter) {
|
||||
t.Fatalf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
|
||||
indent(jbStd, " "), indent(jbIter, " "), dump(before))
|
||||
}
|
||||
|
||||
var afterStd typeForTest
|
||||
err = json.Unmarshal(jbIter, &afterStd)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
var afterIter typeForTest
|
||||
err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||
t.Fatalf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
|
||||
dump(afterStd), dump(afterIter), indent(jbIter, " "))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const indentStr = "> "
|
||||
|
||||
func fingerprint(obj interface{}) string {
|
||||
c := spew.ConfigState{
|
||||
SortKeys: true,
|
||||
SpewKeys: true,
|
||||
}
|
||||
return c.Sprintf("%v", obj)
|
||||
}
|
||||
|
||||
func dump(obj interface{}) string {
|
||||
cfg := spew.ConfigState{
|
||||
Indent: indentStr,
|
||||
}
|
||||
return cfg.Sdump(obj)
|
||||
}
|
||||
|
||||
func indent(src []byte, prefix string) string {
|
||||
var buf bytes.Buffer
|
||||
err := json.Indent(&buf, src, prefix, indentStr)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("!!! %v", err)
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var obj typeForTest
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&obj)
|
||||
for i := 0; i < t.N; i++ {
|
||||
jb, err := fn(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var before typeForTest
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&before)
|
||||
jb, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal: %v", err)
|
||||
}
|
||||
|
||||
for i := 0; i < t.N; i++ {
|
||||
var after typeForTest
|
||||
err = fn(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStandardMarshal(t *testing.B) {
|
||||
benchmarkMarshal(t, "stdlib", json.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "stdlib", json.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalFastest(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalFastest(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalDefault(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalDefault(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalCompatible(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal)
|
||||
}
|
@ -1,3 +0,0 @@
|
||||
package test
|
||||
|
||||
type typeForTest []string
|
@ -1,152 +0,0 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
fuzz "github.com/google/gofuzz"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
func Test_Roundtrip(t *testing.T) {
|
||||
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
|
||||
for i := 0; i < 100; i++ {
|
||||
var before typeForTest
|
||||
fz.Fuzz(&before)
|
||||
|
||||
jbStd, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with stdlib: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbStd))) == 0 {
|
||||
t.Fatal("stdlib marshal produced empty result and no error")
|
||||
}
|
||||
jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with jsoniter: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbIter))) == 0 {
|
||||
t.Fatal("jsoniter marshal produced empty result and no error")
|
||||
}
|
||||
if string(jbStd) != string(jbIter) {
|
||||
t.Fatalf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
|
||||
indent(jbStd, " "), indent(jbIter, " "), dump(before))
|
||||
}
|
||||
|
||||
var afterStd typeForTest
|
||||
err = json.Unmarshal(jbIter, &afterStd)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
var afterIter typeForTest
|
||||
err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||
t.Fatalf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
|
||||
dump(afterStd), dump(afterIter), indent(jbIter, " "))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const indentStr = "> "
|
||||
|
||||
func fingerprint(obj interface{}) string {
|
||||
c := spew.ConfigState{
|
||||
SortKeys: true,
|
||||
SpewKeys: true,
|
||||
}
|
||||
return c.Sprintf("%v", obj)
|
||||
}
|
||||
|
||||
func dump(obj interface{}) string {
|
||||
cfg := spew.ConfigState{
|
||||
Indent: indentStr,
|
||||
}
|
||||
return cfg.Sdump(obj)
|
||||
}
|
||||
|
||||
func indent(src []byte, prefix string) string {
|
||||
var buf bytes.Buffer
|
||||
err := json.Indent(&buf, src, prefix, indentStr)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("!!! %v", err)
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var obj typeForTest
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&obj)
|
||||
for i := 0; i < t.N; i++ {
|
||||
jb, err := fn(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var before typeForTest
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&before)
|
||||
jb, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal: %v", err)
|
||||
}
|
||||
|
||||
for i := 0; i < t.N; i++ {
|
||||
var after typeForTest
|
||||
err = fn(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStandardMarshal(t *testing.B) {
|
||||
benchmarkMarshal(t, "stdlib", json.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "stdlib", json.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalFastest(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalFastest(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalDefault(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalDefault(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalCompatible(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal)
|
||||
}
|
@ -1,3 +0,0 @@
|
||||
package test
|
||||
|
||||
type typeForTest []struct{}
|
@ -1,152 +0,0 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
fuzz "github.com/google/gofuzz"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
func Test_Roundtrip(t *testing.T) {
|
||||
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
|
||||
for i := 0; i < 100; i++ {
|
||||
var before typeForTest
|
||||
fz.Fuzz(&before)
|
||||
|
||||
jbStd, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with stdlib: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbStd))) == 0 {
|
||||
t.Fatal("stdlib marshal produced empty result and no error")
|
||||
}
|
||||
jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with jsoniter: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbIter))) == 0 {
|
||||
t.Fatal("jsoniter marshal produced empty result and no error")
|
||||
}
|
||||
if string(jbStd) != string(jbIter) {
|
||||
t.Fatalf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
|
||||
indent(jbStd, " "), indent(jbIter, " "), dump(before))
|
||||
}
|
||||
|
||||
var afterStd typeForTest
|
||||
err = json.Unmarshal(jbIter, &afterStd)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
var afterIter typeForTest
|
||||
err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||
t.Fatalf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
|
||||
dump(afterStd), dump(afterIter), indent(jbIter, " "))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const indentStr = "> "
|
||||
|
||||
func fingerprint(obj interface{}) string {
|
||||
c := spew.ConfigState{
|
||||
SortKeys: true,
|
||||
SpewKeys: true,
|
||||
}
|
||||
return c.Sprintf("%v", obj)
|
||||
}
|
||||
|
||||
func dump(obj interface{}) string {
|
||||
cfg := spew.ConfigState{
|
||||
Indent: indentStr,
|
||||
}
|
||||
return cfg.Sdump(obj)
|
||||
}
|
||||
|
||||
func indent(src []byte, prefix string) string {
|
||||
var buf bytes.Buffer
|
||||
err := json.Indent(&buf, src, prefix, indentStr)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("!!! %v", err)
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var obj typeForTest
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&obj)
|
||||
for i := 0; i < t.N; i++ {
|
||||
jb, err := fn(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var before typeForTest
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&before)
|
||||
jb, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal: %v", err)
|
||||
}
|
||||
|
||||
for i := 0; i < t.N; i++ {
|
||||
var after typeForTest
|
||||
err = fn(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStandardMarshal(t *testing.B) {
|
||||
benchmarkMarshal(t, "stdlib", json.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "stdlib", json.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalFastest(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalFastest(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalDefault(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalDefault(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalCompatible(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal)
|
||||
}
|
@ -1,5 +0,0 @@
|
||||
package test
|
||||
|
||||
type typeA struct{}
|
||||
|
||||
type typeForTest []typeA
|
@ -1,152 +0,0 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
fuzz "github.com/google/gofuzz"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
func Test_Roundtrip(t *testing.T) {
|
||||
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
|
||||
for i := 0; i < 100; i++ {
|
||||
var before typeForTest
|
||||
fz.Fuzz(&before)
|
||||
|
||||
jbStd, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with stdlib: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbStd))) == 0 {
|
||||
t.Fatal("stdlib marshal produced empty result and no error")
|
||||
}
|
||||
jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with jsoniter: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbIter))) == 0 {
|
||||
t.Fatal("jsoniter marshal produced empty result and no error")
|
||||
}
|
||||
if string(jbStd) != string(jbIter) {
|
||||
t.Fatalf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
|
||||
indent(jbStd, " "), indent(jbIter, " "), dump(before))
|
||||
}
|
||||
|
||||
var afterStd typeForTest
|
||||
err = json.Unmarshal(jbIter, &afterStd)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
var afterIter typeForTest
|
||||
err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||
t.Fatalf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
|
||||
dump(afterStd), dump(afterIter), indent(jbIter, " "))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const indentStr = "> "
|
||||
|
||||
func fingerprint(obj interface{}) string {
|
||||
c := spew.ConfigState{
|
||||
SortKeys: true,
|
||||
SpewKeys: true,
|
||||
}
|
||||
return c.Sprintf("%v", obj)
|
||||
}
|
||||
|
||||
func dump(obj interface{}) string {
|
||||
cfg := spew.ConfigState{
|
||||
Indent: indentStr,
|
||||
}
|
||||
return cfg.Sdump(obj)
|
||||
}
|
||||
|
||||
func indent(src []byte, prefix string) string {
|
||||
var buf bytes.Buffer
|
||||
err := json.Indent(&buf, src, prefix, indentStr)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("!!! %v", err)
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var obj typeForTest
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&obj)
|
||||
for i := 0; i < t.N; i++ {
|
||||
jb, err := fn(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var before typeForTest
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&before)
|
||||
jb, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal: %v", err)
|
||||
}
|
||||
|
||||
for i := 0; i < t.N; i++ {
|
||||
var after typeForTest
|
||||
err = fn(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStandardMarshal(t *testing.B) {
|
||||
benchmarkMarshal(t, "stdlib", json.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "stdlib", json.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalFastest(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalFastest(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalDefault(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalDefault(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalCompatible(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal)
|
||||
}
|
@ -1,5 +0,0 @@
|
||||
package test
|
||||
|
||||
type typeForTest []struct {
|
||||
F *string
|
||||
}
|
@ -1,152 +0,0 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
fuzz "github.com/google/gofuzz"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
func Test_Roundtrip(t *testing.T) {
|
||||
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
|
||||
for i := 0; i < 100; i++ {
|
||||
var before typeForTest
|
||||
fz.Fuzz(&before)
|
||||
|
||||
jbStd, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with stdlib: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbStd))) == 0 {
|
||||
t.Fatal("stdlib marshal produced empty result and no error")
|
||||
}
|
||||
jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with jsoniter: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbIter))) == 0 {
|
||||
t.Fatal("jsoniter marshal produced empty result and no error")
|
||||
}
|
||||
if string(jbStd) != string(jbIter) {
|
||||
t.Fatalf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
|
||||
indent(jbStd, " "), indent(jbIter, " "), dump(before))
|
||||
}
|
||||
|
||||
var afterStd typeForTest
|
||||
err = json.Unmarshal(jbIter, &afterStd)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
var afterIter typeForTest
|
||||
err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||
t.Fatalf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
|
||||
dump(afterStd), dump(afterIter), indent(jbIter, " "))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const indentStr = "> "
|
||||
|
||||
func fingerprint(obj interface{}) string {
|
||||
c := spew.ConfigState{
|
||||
SortKeys: true,
|
||||
SpewKeys: true,
|
||||
}
|
||||
return c.Sprintf("%v", obj)
|
||||
}
|
||||
|
||||
func dump(obj interface{}) string {
|
||||
cfg := spew.ConfigState{
|
||||
Indent: indentStr,
|
||||
}
|
||||
return cfg.Sdump(obj)
|
||||
}
|
||||
|
||||
func indent(src []byte, prefix string) string {
|
||||
var buf bytes.Buffer
|
||||
err := json.Indent(&buf, src, prefix, indentStr)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("!!! %v", err)
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var obj typeForTest
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&obj)
|
||||
for i := 0; i < t.N; i++ {
|
||||
jb, err := fn(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var before typeForTest
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&before)
|
||||
jb, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal: %v", err)
|
||||
}
|
||||
|
||||
for i := 0; i < t.N; i++ {
|
||||
var after typeForTest
|
||||
err = fn(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStandardMarshal(t *testing.B) {
|
||||
benchmarkMarshal(t, "stdlib", json.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "stdlib", json.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalFastest(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalFastest(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalDefault(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalDefault(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalCompatible(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal)
|
||||
}
|
@ -1,12 +0,0 @@
|
||||
package test
|
||||
|
||||
type typeForTest []struct {
|
||||
String string
|
||||
Int int32
|
||||
Float float64
|
||||
Struct struct {
|
||||
X string
|
||||
}
|
||||
Slice []string
|
||||
Map map[string]string
|
||||
}
|
@ -1,152 +0,0 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
fuzz "github.com/google/gofuzz"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
func Test_Roundtrip(t *testing.T) {
|
||||
fz := fuzz.New().MaxDepth(10).NilChance(0.3)
|
||||
for i := 0; i < 100; i++ {
|
||||
var before typeForTest
|
||||
fz.Fuzz(&before)
|
||||
|
||||
jbStd, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with stdlib: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbStd))) == 0 {
|
||||
t.Fatal("stdlib marshal produced empty result and no error")
|
||||
}
|
||||
jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal with jsoniter: %v", err)
|
||||
}
|
||||
if len(strings.TrimSpace(string(jbIter))) == 0 {
|
||||
t.Fatal("jsoniter marshal produced empty result and no error")
|
||||
}
|
||||
if string(jbStd) != string(jbIter) {
|
||||
t.Fatalf("marshal expected:\n %s\ngot:\n %s\nobj:\n %s",
|
||||
indent(jbStd, " "), indent(jbIter, " "), dump(before))
|
||||
}
|
||||
|
||||
var afterStd typeForTest
|
||||
err = json.Unmarshal(jbIter, &afterStd)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
var afterIter typeForTest
|
||||
err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to unmarshal with jsoniter: %v\nvia:\n %s",
|
||||
err, indent(jbIter, " "))
|
||||
}
|
||||
if fingerprint(afterStd) != fingerprint(afterIter) {
|
||||
t.Fatalf("unmarshal expected:\n %s\ngot:\n %s\nvia:\n %s",
|
||||
dump(afterStd), dump(afterIter), indent(jbIter, " "))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const indentStr = "> "
|
||||
|
||||
func fingerprint(obj interface{}) string {
|
||||
c := spew.ConfigState{
|
||||
SortKeys: true,
|
||||
SpewKeys: true,
|
||||
}
|
||||
return c.Sprintf("%v", obj)
|
||||
}
|
||||
|
||||
func dump(obj interface{}) string {
|
||||
cfg := spew.ConfigState{
|
||||
Indent: indentStr,
|
||||
}
|
||||
return cfg.Sdump(obj)
|
||||
}
|
||||
|
||||
func indent(src []byte, prefix string) string {
|
||||
var buf bytes.Buffer
|
||||
err := json.Indent(&buf, src, prefix, indentStr)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("!!! %v", err)
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var obj typeForTest
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&obj)
|
||||
for i := 0; i < t.N; i++ {
|
||||
jb, err := fn(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to marshal:\n input: %s\n error: %v", name, dump(obj), err)
|
||||
}
|
||||
_ = jb
|
||||
}
|
||||
}
|
||||
|
||||
func benchmarkUnmarshal(t *testing.B, name string, fn func(data []byte, v interface{}) error) {
|
||||
t.ReportAllocs()
|
||||
t.ResetTimer()
|
||||
|
||||
var before typeForTest
|
||||
fz := fuzz.NewWithSeed(0).MaxDepth(10).NilChance(0.3)
|
||||
fz.Fuzz(&before)
|
||||
jb, err := json.Marshal(before)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal: %v", err)
|
||||
}
|
||||
|
||||
for i := 0; i < t.N; i++ {
|
||||
var after typeForTest
|
||||
err = fn(jb, &after)
|
||||
if err != nil {
|
||||
t.Fatalf("%s failed to unmarshal:\n input: %q\n error: %v", name, string(jb), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkStandardMarshal(t *testing.B) {
|
||||
benchmarkMarshal(t, "stdlib", json.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkStandardUnmarshal(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "stdlib", json.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalFastest(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalFastest(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-fastest", jsoniter.ConfigFastest.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalDefault(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-default", jsoniter.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalDefault(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-default", jsoniter.Unmarshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterMarshalCompatible(t *testing.B) {
|
||||
benchmarkMarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Marshal)
|
||||
}
|
||||
|
||||
func BenchmarkJSONIterUnmarshalCompatible(t *testing.B) {
|
||||
benchmarkUnmarshal(t, "jsoniter-compat", jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal)
|
||||
}
|
@ -1,3 +0,0 @@
|
||||
package test
|
||||
|
||||
type typeForTest []uint8
|
75
type_tests/slice_test.go
Normal file
75
type_tests/slice_test.go
Normal file
@ -0,0 +1,75 @@
|
||||
package test
|
||||
|
||||
func init() {
|
||||
testCases = append(testCases,
|
||||
(*[][4]bool)(nil),
|
||||
(*[][4]byte)(nil),
|
||||
(*[][4]float64)(nil),
|
||||
(*[][4]int32)(nil),
|
||||
(*[][4]*string)(nil),
|
||||
(*[][4]string)(nil),
|
||||
(*[][4]uint8)(nil),
|
||||
(*[]bool)(nil),
|
||||
(*[]byte)(nil),
|
||||
(*[]float64)(nil),
|
||||
(*[]int32)(nil),
|
||||
(*[]int64)(nil),
|
||||
(*[]map[int32]string)(nil),
|
||||
(*[]map[string]string)(nil),
|
||||
(*[4]*[4]bool)(nil),
|
||||
(*[4]*[4]byte)(nil),
|
||||
(*[4]*[4]float64)(nil),
|
||||
(*[4]*[4]int32)(nil),
|
||||
(*[4]*[4]*string)(nil),
|
||||
(*[4]*[4]string)(nil),
|
||||
(*[4]*[4]uint8)(nil),
|
||||
(*[]*bool)(nil),
|
||||
(*[]*float64)(nil),
|
||||
(*[]*int32)(nil),
|
||||
(*[]*map[int32]string)(nil),
|
||||
(*[]*map[string]string)(nil),
|
||||
(*[]*[]bool)(nil),
|
||||
(*[]*[]byte)(nil),
|
||||
(*[]*[]float64)(nil),
|
||||
(*[]*[]int32)(nil),
|
||||
(*[]*[]*string)(nil),
|
||||
(*[]*[]string)(nil),
|
||||
(*[]*[]uint8)(nil),
|
||||
(*[]*string)(nil),
|
||||
(*[]*struct {
|
||||
String string
|
||||
Int int32
|
||||
Float float64
|
||||
Struct struct {
|
||||
X string
|
||||
}
|
||||
Slice []string
|
||||
Map map[string]string
|
||||
})(nil),
|
||||
(*[]*uint8)(nil),
|
||||
(*[][]bool)(nil),
|
||||
(*[][]byte)(nil),
|
||||
(*[][]float64)(nil),
|
||||
(*[][]int32)(nil),
|
||||
(*[][]*string)(nil),
|
||||
(*[][]string)(nil),
|
||||
(*[][]uint8)(nil),
|
||||
(*[]string)(nil),
|
||||
(*[]struct{})(nil),
|
||||
(*[]structEmpty)(nil),
|
||||
(*[]struct {
|
||||
F *string
|
||||
})(nil),
|
||||
(*[]struct {
|
||||
String string
|
||||
Int int32
|
||||
Float float64
|
||||
Struct struct {
|
||||
X string
|
||||
}
|
||||
Slice []string
|
||||
Map map[string]string
|
||||
})(nil),
|
||||
(*[]uint8)(nil),
|
||||
)
|
||||
}
|
Loading…
Reference in New Issue
Block a user