Browse Source

Add output tests for omitempty

Tim Hockin 8 years ago
parent
commit
1589ab2fd7
30 changed files with 2175 additions and 0 deletions
  1. 139 0
      output_tests/struct_tags/omitempty/bool/json_test.go
  2. 6 0
      output_tests/struct_tags/omitempty/bool/types.go
  3. 139 0
      output_tests/struct_tags/omitempty/float32/json_test.go
  4. 6 0
      output_tests/struct_tags/omitempty/float32/types.go
  5. 139 0
      output_tests/struct_tags/omitempty/int32/json_test.go
  6. 6 0
      output_tests/struct_tags/omitempty/int32/types.go
  7. 139 0
      output_tests/struct_tags/omitempty/map_string_string/json_test.go
  8. 6 0
      output_tests/struct_tags/omitempty/map_string_string/types.go
  9. 139 0
      output_tests/struct_tags/omitempty/ptr_bool/json_test.go
  10. 6 0
      output_tests/struct_tags/omitempty/ptr_bool/types.go
  11. 139 0
      output_tests/struct_tags/omitempty/ptr_float32/json_test.go
  12. 6 0
      output_tests/struct_tags/omitempty/ptr_float32/types.go
  13. 139 0
      output_tests/struct_tags/omitempty/ptr_int32/json_test.go
  14. 6 0
      output_tests/struct_tags/omitempty/ptr_int32/types.go
  15. 139 0
      output_tests/struct_tags/omitempty/ptr_map_string_string/json_test.go
  16. 6 0
      output_tests/struct_tags/omitempty/ptr_map_string_string/types.go
  17. 139 0
      output_tests/struct_tags/omitempty/ptr_slice_string/json_test.go
  18. 6 0
      output_tests/struct_tags/omitempty/ptr_slice_string/types.go
  19. 139 0
      output_tests/struct_tags/omitempty/ptr_string/json_test.go
  20. 139 0
      output_tests/struct_tags/omitempty/ptr_string/string/json_test.go
  21. 6 0
      output_tests/struct_tags/omitempty/ptr_string/string/types.go
  22. 6 0
      output_tests/struct_tags/omitempty/ptr_string/types.go
  23. 139 0
      output_tests/struct_tags/omitempty/ptr_uint32/json_test.go
  24. 6 0
      output_tests/struct_tags/omitempty/ptr_uint32/types.go
  25. 139 0
      output_tests/struct_tags/omitempty/slice_string/json_test.go
  26. 6 0
      output_tests/struct_tags/omitempty/slice_string/types.go
  27. 139 0
      output_tests/struct_tags/omitempty/string/json_test.go
  28. 6 0
      output_tests/struct_tags/omitempty/string/types.go
  29. 139 0
      output_tests/struct_tags/omitempty/uint32/json_test.go
  30. 6 0
      output_tests/struct_tags/omitempty/uint32/types.go

+ 139 - 0
output_tests/struct_tags/omitempty/bool/json_test.go

@@ -0,0 +1,139 @@
+package test
+
+import (
+	"bytes"
+	"encoding/json"
+	"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 < 1000; i++ {
+		var before T
+		fz.Fuzz(&before)
+
+		jbStd, err := json.Marshal(before)
+		if err != nil {
+			t.Errorf("failed to marshal with stdlib: %v", err)
+		}
+		jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
+		if err != nil {
+			t.Errorf("failed to marshal with jsoniter: %v", err)
+		}
+		if string(jbStd) != string(jbIter) {
+			t.Errorf("marshal expected:\n    %s\ngot:\n    %s\nobj:\n    %s",
+				indent(jbStd, "    "), indent(jbIter, "    "), dump(before))
+		}
+
+		var afterStd T
+		err = json.Unmarshal(jbIter, &afterStd)
+		if err != nil {
+			t.Errorf("failed to unmarshal with stdlib: %v", err)
+		}
+		var afterIter T
+		err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
+		if err != nil {
+			t.Errorf("failed to unmarshal with jsoniter: %v", err)
+		}
+		if fingerprint(afterStd) != fingerprint(afterIter) {
+			t.Errorf("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
+	json.Indent(&buf, src, prefix, indentStr)
+	return buf.String()
+}
+
+func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
+	t.ReportAllocs()
+	t.ResetTimer()
+
+	var obj T
+	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 T
+	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 T
+		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)
+}

+ 6 - 0
output_tests/struct_tags/omitempty/bool/types.go

@@ -0,0 +1,6 @@
+package test
+
+type T struct {
+	F1 bool `json:"F1"`
+	F2 bool `json:"F2,omitempty"`
+}

+ 139 - 0
output_tests/struct_tags/omitempty/float32/json_test.go

@@ -0,0 +1,139 @@
+package test
+
+import (
+	"bytes"
+	"encoding/json"
+	"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 < 1000; i++ {
+		var before T
+		fz.Fuzz(&before)
+
+		jbStd, err := json.Marshal(before)
+		if err != nil {
+			t.Errorf("failed to marshal with stdlib: %v", err)
+		}
+		jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
+		if err != nil {
+			t.Errorf("failed to marshal with jsoniter: %v", err)
+		}
+		if string(jbStd) != string(jbIter) {
+			t.Errorf("marshal expected:\n    %s\ngot:\n    %s\nobj:\n    %s",
+				indent(jbStd, "    "), indent(jbIter, "    "), dump(before))
+		}
+
+		var afterStd T
+		err = json.Unmarshal(jbIter, &afterStd)
+		if err != nil {
+			t.Errorf("failed to unmarshal with stdlib: %v", err)
+		}
+		var afterIter T
+		err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
+		if err != nil {
+			t.Errorf("failed to unmarshal with jsoniter: %v", err)
+		}
+		if fingerprint(afterStd) != fingerprint(afterIter) {
+			t.Errorf("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
+	json.Indent(&buf, src, prefix, indentStr)
+	return buf.String()
+}
+
+func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
+	t.ReportAllocs()
+	t.ResetTimer()
+
+	var obj T
+	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 T
+	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 T
+		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)
+}

+ 6 - 0
output_tests/struct_tags/omitempty/float32/types.go

@@ -0,0 +1,6 @@
+package test
+
+type T struct {
+	F1 float32 `json:"F1"`
+	F2 float32 `json:"F2,omitempty"`
+}

+ 139 - 0
output_tests/struct_tags/omitempty/int32/json_test.go

@@ -0,0 +1,139 @@
+package test
+
+import (
+	"bytes"
+	"encoding/json"
+	"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 < 1000; i++ {
+		var before T
+		fz.Fuzz(&before)
+
+		jbStd, err := json.Marshal(before)
+		if err != nil {
+			t.Errorf("failed to marshal with stdlib: %v", err)
+		}
+		jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
+		if err != nil {
+			t.Errorf("failed to marshal with jsoniter: %v", err)
+		}
+		if string(jbStd) != string(jbIter) {
+			t.Errorf("marshal expected:\n    %s\ngot:\n    %s\nobj:\n    %s",
+				indent(jbStd, "    "), indent(jbIter, "    "), dump(before))
+		}
+
+		var afterStd T
+		err = json.Unmarshal(jbIter, &afterStd)
+		if err != nil {
+			t.Errorf("failed to unmarshal with stdlib: %v", err)
+		}
+		var afterIter T
+		err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
+		if err != nil {
+			t.Errorf("failed to unmarshal with jsoniter: %v", err)
+		}
+		if fingerprint(afterStd) != fingerprint(afterIter) {
+			t.Errorf("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
+	json.Indent(&buf, src, prefix, indentStr)
+	return buf.String()
+}
+
+func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
+	t.ReportAllocs()
+	t.ResetTimer()
+
+	var obj T
+	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 T
+	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 T
+		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)
+}

+ 6 - 0
output_tests/struct_tags/omitempty/int32/types.go

@@ -0,0 +1,6 @@
+package test
+
+type T struct {
+	F1 int32 `json:"F1"`
+	F2 int32 `json:"F2,omitempty"`
+}

+ 139 - 0
output_tests/struct_tags/omitempty/map_string_string/json_test.go

@@ -0,0 +1,139 @@
+package test
+
+import (
+	"bytes"
+	"encoding/json"
+	"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 < 1000; i++ {
+		var before T
+		fz.Fuzz(&before)
+
+		jbStd, err := json.Marshal(before)
+		if err != nil {
+			t.Errorf("failed to marshal with stdlib: %v", err)
+		}
+		jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
+		if err != nil {
+			t.Errorf("failed to marshal with jsoniter: %v", err)
+		}
+		if string(jbStd) != string(jbIter) {
+			t.Errorf("marshal expected:\n    %s\ngot:\n    %s\nobj:\n    %s",
+				indent(jbStd, "    "), indent(jbIter, "    "), dump(before))
+		}
+
+		var afterStd T
+		err = json.Unmarshal(jbIter, &afterStd)
+		if err != nil {
+			t.Errorf("failed to unmarshal with stdlib: %v", err)
+		}
+		var afterIter T
+		err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
+		if err != nil {
+			t.Errorf("failed to unmarshal with jsoniter: %v", err)
+		}
+		if fingerprint(afterStd) != fingerprint(afterIter) {
+			t.Errorf("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
+	json.Indent(&buf, src, prefix, indentStr)
+	return buf.String()
+}
+
+func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
+	t.ReportAllocs()
+	t.ResetTimer()
+
+	var obj T
+	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 T
+	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 T
+		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)
+}

+ 6 - 0
output_tests/struct_tags/omitempty/map_string_string/types.go

@@ -0,0 +1,6 @@
+package test
+
+type T struct {
+	F1 map[string]string `json:"F1"`
+	F2 map[string]string `json:"F2,omitempty"`
+}

+ 139 - 0
output_tests/struct_tags/omitempty/ptr_bool/json_test.go

@@ -0,0 +1,139 @@
+package test
+
+import (
+	"bytes"
+	"encoding/json"
+	"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 < 1000; i++ {
+		var before T
+		fz.Fuzz(&before)
+
+		jbStd, err := json.Marshal(before)
+		if err != nil {
+			t.Errorf("failed to marshal with stdlib: %v", err)
+		}
+		jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
+		if err != nil {
+			t.Errorf("failed to marshal with jsoniter: %v", err)
+		}
+		if string(jbStd) != string(jbIter) {
+			t.Errorf("marshal expected:\n    %s\ngot:\n    %s\nobj:\n    %s",
+				indent(jbStd, "    "), indent(jbIter, "    "), dump(before))
+		}
+
+		var afterStd T
+		err = json.Unmarshal(jbIter, &afterStd)
+		if err != nil {
+			t.Errorf("failed to unmarshal with stdlib: %v", err)
+		}
+		var afterIter T
+		err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
+		if err != nil {
+			t.Errorf("failed to unmarshal with jsoniter: %v", err)
+		}
+		if fingerprint(afterStd) != fingerprint(afterIter) {
+			t.Errorf("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
+	json.Indent(&buf, src, prefix, indentStr)
+	return buf.String()
+}
+
+func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
+	t.ReportAllocs()
+	t.ResetTimer()
+
+	var obj T
+	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 T
+	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 T
+		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)
+}

+ 6 - 0
output_tests/struct_tags/omitempty/ptr_bool/types.go

@@ -0,0 +1,6 @@
+package test
+
+type T struct {
+	F1 *bool `json:"F1"`
+	F2 *bool `json:"F2,omitempty"`
+}

+ 139 - 0
output_tests/struct_tags/omitempty/ptr_float32/json_test.go

@@ -0,0 +1,139 @@
+package test
+
+import (
+	"bytes"
+	"encoding/json"
+	"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 < 1000; i++ {
+		var before T
+		fz.Fuzz(&before)
+
+		jbStd, err := json.Marshal(before)
+		if err != nil {
+			t.Errorf("failed to marshal with stdlib: %v", err)
+		}
+		jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
+		if err != nil {
+			t.Errorf("failed to marshal with jsoniter: %v", err)
+		}
+		if string(jbStd) != string(jbIter) {
+			t.Errorf("marshal expected:\n    %s\ngot:\n    %s\nobj:\n    %s",
+				indent(jbStd, "    "), indent(jbIter, "    "), dump(before))
+		}
+
+		var afterStd T
+		err = json.Unmarshal(jbIter, &afterStd)
+		if err != nil {
+			t.Errorf("failed to unmarshal with stdlib: %v", err)
+		}
+		var afterIter T
+		err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
+		if err != nil {
+			t.Errorf("failed to unmarshal with jsoniter: %v", err)
+		}
+		if fingerprint(afterStd) != fingerprint(afterIter) {
+			t.Errorf("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
+	json.Indent(&buf, src, prefix, indentStr)
+	return buf.String()
+}
+
+func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
+	t.ReportAllocs()
+	t.ResetTimer()
+
+	var obj T
+	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 T
+	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 T
+		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)
+}

+ 6 - 0
output_tests/struct_tags/omitempty/ptr_float32/types.go

@@ -0,0 +1,6 @@
+package test
+
+type T struct {
+	F1 *float32 `json:"F1"`
+	F2 *float32 `json:"F2,omitempty"`
+}

+ 139 - 0
output_tests/struct_tags/omitempty/ptr_int32/json_test.go

@@ -0,0 +1,139 @@
+package test
+
+import (
+	"bytes"
+	"encoding/json"
+	"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 < 1000; i++ {
+		var before T
+		fz.Fuzz(&before)
+
+		jbStd, err := json.Marshal(before)
+		if err != nil {
+			t.Errorf("failed to marshal with stdlib: %v", err)
+		}
+		jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
+		if err != nil {
+			t.Errorf("failed to marshal with jsoniter: %v", err)
+		}
+		if string(jbStd) != string(jbIter) {
+			t.Errorf("marshal expected:\n    %s\ngot:\n    %s\nobj:\n    %s",
+				indent(jbStd, "    "), indent(jbIter, "    "), dump(before))
+		}
+
+		var afterStd T
+		err = json.Unmarshal(jbIter, &afterStd)
+		if err != nil {
+			t.Errorf("failed to unmarshal with stdlib: %v", err)
+		}
+		var afterIter T
+		err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
+		if err != nil {
+			t.Errorf("failed to unmarshal with jsoniter: %v", err)
+		}
+		if fingerprint(afterStd) != fingerprint(afterIter) {
+			t.Errorf("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
+	json.Indent(&buf, src, prefix, indentStr)
+	return buf.String()
+}
+
+func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
+	t.ReportAllocs()
+	t.ResetTimer()
+
+	var obj T
+	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 T
+	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 T
+		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)
+}

+ 6 - 0
output_tests/struct_tags/omitempty/ptr_int32/types.go

@@ -0,0 +1,6 @@
+package test
+
+type T struct {
+	F1 *int32 `json:"F1"`
+	F2 *int32 `json:"F2,omitempty"`
+}

+ 139 - 0
output_tests/struct_tags/omitempty/ptr_map_string_string/json_test.go

@@ -0,0 +1,139 @@
+package test
+
+import (
+	"bytes"
+	"encoding/json"
+	"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 < 1000; i++ {
+		var before T
+		fz.Fuzz(&before)
+
+		jbStd, err := json.Marshal(before)
+		if err != nil {
+			t.Errorf("failed to marshal with stdlib: %v", err)
+		}
+		jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
+		if err != nil {
+			t.Errorf("failed to marshal with jsoniter: %v", err)
+		}
+		if string(jbStd) != string(jbIter) {
+			t.Errorf("marshal expected:\n    %s\ngot:\n    %s\nobj:\n    %s",
+				indent(jbStd, "    "), indent(jbIter, "    "), dump(before))
+		}
+
+		var afterStd T
+		err = json.Unmarshal(jbIter, &afterStd)
+		if err != nil {
+			t.Errorf("failed to unmarshal with stdlib: %v", err)
+		}
+		var afterIter T
+		err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
+		if err != nil {
+			t.Errorf("failed to unmarshal with jsoniter: %v", err)
+		}
+		if fingerprint(afterStd) != fingerprint(afterIter) {
+			t.Errorf("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
+	json.Indent(&buf, src, prefix, indentStr)
+	return buf.String()
+}
+
+func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
+	t.ReportAllocs()
+	t.ResetTimer()
+
+	var obj T
+	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 T
+	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 T
+		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)
+}

+ 6 - 0
output_tests/struct_tags/omitempty/ptr_map_string_string/types.go

@@ -0,0 +1,6 @@
+package test
+
+type T struct {
+	F1 *map[string]string `json:"F1"`
+	F2 *map[string]string `json:"F2,omitempty"`
+}

+ 139 - 0
output_tests/struct_tags/omitempty/ptr_slice_string/json_test.go

@@ -0,0 +1,139 @@
+package test
+
+import (
+	"bytes"
+	"encoding/json"
+	"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 < 1000; i++ {
+		var before T
+		fz.Fuzz(&before)
+
+		jbStd, err := json.Marshal(before)
+		if err != nil {
+			t.Errorf("failed to marshal with stdlib: %v", err)
+		}
+		jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
+		if err != nil {
+			t.Errorf("failed to marshal with jsoniter: %v", err)
+		}
+		if string(jbStd) != string(jbIter) {
+			t.Errorf("marshal expected:\n    %s\ngot:\n    %s\nobj:\n    %s",
+				indent(jbStd, "    "), indent(jbIter, "    "), dump(before))
+		}
+
+		var afterStd T
+		err = json.Unmarshal(jbIter, &afterStd)
+		if err != nil {
+			t.Errorf("failed to unmarshal with stdlib: %v", err)
+		}
+		var afterIter T
+		err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
+		if err != nil {
+			t.Errorf("failed to unmarshal with jsoniter: %v", err)
+		}
+		if fingerprint(afterStd) != fingerprint(afterIter) {
+			t.Errorf("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
+	json.Indent(&buf, src, prefix, indentStr)
+	return buf.String()
+}
+
+func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
+	t.ReportAllocs()
+	t.ResetTimer()
+
+	var obj T
+	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 T
+	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 T
+		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)
+}

+ 6 - 0
output_tests/struct_tags/omitempty/ptr_slice_string/types.go

@@ -0,0 +1,6 @@
+package test
+
+type T struct {
+	F1 *[]string `json:"F1"`
+	F2 *[]string `json:"F2,omitempty"`
+}

+ 139 - 0
output_tests/struct_tags/omitempty/ptr_string/json_test.go

@@ -0,0 +1,139 @@
+package test
+
+import (
+	"bytes"
+	"encoding/json"
+	"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 < 1000; i++ {
+		var before T
+		fz.Fuzz(&before)
+
+		jbStd, err := json.Marshal(before)
+		if err != nil {
+			t.Errorf("failed to marshal with stdlib: %v", err)
+		}
+		jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
+		if err != nil {
+			t.Errorf("failed to marshal with jsoniter: %v", err)
+		}
+		if string(jbStd) != string(jbIter) {
+			t.Errorf("marshal expected:\n    %s\ngot:\n    %s\nobj:\n    %s",
+				indent(jbStd, "    "), indent(jbIter, "    "), dump(before))
+		}
+
+		var afterStd T
+		err = json.Unmarshal(jbIter, &afterStd)
+		if err != nil {
+			t.Errorf("failed to unmarshal with stdlib: %v", err)
+		}
+		var afterIter T
+		err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
+		if err != nil {
+			t.Errorf("failed to unmarshal with jsoniter: %v", err)
+		}
+		if fingerprint(afterStd) != fingerprint(afterIter) {
+			t.Errorf("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
+	json.Indent(&buf, src, prefix, indentStr)
+	return buf.String()
+}
+
+func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
+	t.ReportAllocs()
+	t.ResetTimer()
+
+	var obj T
+	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 T
+	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 T
+		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)
+}

+ 139 - 0
output_tests/struct_tags/omitempty/ptr_string/string/json_test.go

@@ -0,0 +1,139 @@
+package test
+
+import (
+	"bytes"
+	"encoding/json"
+	"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 < 1000; i++ {
+		var before T
+		fz.Fuzz(&before)
+
+		jbStd, err := json.Marshal(before)
+		if err != nil {
+			t.Errorf("failed to marshal with stdlib: %v", err)
+		}
+		jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
+		if err != nil {
+			t.Errorf("failed to marshal with jsoniter: %v", err)
+		}
+		if string(jbStd) != string(jbIter) {
+			t.Errorf("marshal expected:\n    %s\ngot:\n    %s\nobj:\n    %s",
+				indent(jbStd, "    "), indent(jbIter, "    "), dump(before))
+		}
+
+		var afterStd T
+		err = json.Unmarshal(jbIter, &afterStd)
+		if err != nil {
+			t.Errorf("failed to unmarshal with stdlib: %v", err)
+		}
+		var afterIter T
+		err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
+		if err != nil {
+			t.Errorf("failed to unmarshal with jsoniter: %v", err)
+		}
+		if fingerprint(afterStd) != fingerprint(afterIter) {
+			t.Errorf("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
+	json.Indent(&buf, src, prefix, indentStr)
+	return buf.String()
+}
+
+func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
+	t.ReportAllocs()
+	t.ResetTimer()
+
+	var obj T
+	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 T
+	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 T
+		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)
+}

+ 6 - 0
output_tests/struct_tags/omitempty/ptr_string/string/types.go

@@ -0,0 +1,6 @@
+package test
+
+type T struct {
+	F1 string `json:"F1"`
+	F2 string `json:"F2,omitempty"`
+}

+ 6 - 0
output_tests/struct_tags/omitempty/ptr_string/types.go

@@ -0,0 +1,6 @@
+package test
+
+type T struct {
+	F1 *string `json:"F1"`
+	F2 *string `json:"F2,omitempty"`
+}

+ 139 - 0
output_tests/struct_tags/omitempty/ptr_uint32/json_test.go

@@ -0,0 +1,139 @@
+package test
+
+import (
+	"bytes"
+	"encoding/json"
+	"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 < 1000; i++ {
+		var before T
+		fz.Fuzz(&before)
+
+		jbStd, err := json.Marshal(before)
+		if err != nil {
+			t.Errorf("failed to marshal with stdlib: %v", err)
+		}
+		jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
+		if err != nil {
+			t.Errorf("failed to marshal with jsoniter: %v", err)
+		}
+		if string(jbStd) != string(jbIter) {
+			t.Errorf("marshal expected:\n    %s\ngot:\n    %s\nobj:\n    %s",
+				indent(jbStd, "    "), indent(jbIter, "    "), dump(before))
+		}
+
+		var afterStd T
+		err = json.Unmarshal(jbIter, &afterStd)
+		if err != nil {
+			t.Errorf("failed to unmarshal with stdlib: %v", err)
+		}
+		var afterIter T
+		err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
+		if err != nil {
+			t.Errorf("failed to unmarshal with jsoniter: %v", err)
+		}
+		if fingerprint(afterStd) != fingerprint(afterIter) {
+			t.Errorf("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
+	json.Indent(&buf, src, prefix, indentStr)
+	return buf.String()
+}
+
+func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
+	t.ReportAllocs()
+	t.ResetTimer()
+
+	var obj T
+	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 T
+	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 T
+		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)
+}

+ 6 - 0
output_tests/struct_tags/omitempty/ptr_uint32/types.go

@@ -0,0 +1,6 @@
+package test
+
+type T struct {
+	F1 *uint32 `json:"F1"`
+	F2 *uint32 `json:"F2,omitempty"`
+}

+ 139 - 0
output_tests/struct_tags/omitempty/slice_string/json_test.go

@@ -0,0 +1,139 @@
+package test
+
+import (
+	"bytes"
+	"encoding/json"
+	"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 < 1000; i++ {
+		var before T
+		fz.Fuzz(&before)
+
+		jbStd, err := json.Marshal(before)
+		if err != nil {
+			t.Errorf("failed to marshal with stdlib: %v", err)
+		}
+		jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
+		if err != nil {
+			t.Errorf("failed to marshal with jsoniter: %v", err)
+		}
+		if string(jbStd) != string(jbIter) {
+			t.Errorf("marshal expected:\n    %s\ngot:\n    %s\nobj:\n    %s",
+				indent(jbStd, "    "), indent(jbIter, "    "), dump(before))
+		}
+
+		var afterStd T
+		err = json.Unmarshal(jbIter, &afterStd)
+		if err != nil {
+			t.Errorf("failed to unmarshal with stdlib: %v", err)
+		}
+		var afterIter T
+		err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
+		if err != nil {
+			t.Errorf("failed to unmarshal with jsoniter: %v", err)
+		}
+		if fingerprint(afterStd) != fingerprint(afterIter) {
+			t.Errorf("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
+	json.Indent(&buf, src, prefix, indentStr)
+	return buf.String()
+}
+
+func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
+	t.ReportAllocs()
+	t.ResetTimer()
+
+	var obj T
+	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 T
+	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 T
+		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)
+}

+ 6 - 0
output_tests/struct_tags/omitempty/slice_string/types.go

@@ -0,0 +1,6 @@
+package test
+
+type T struct {
+	F1 []string `json:"F1"`
+	F2 []string `json:"F2,omitempty"`
+}

+ 139 - 0
output_tests/struct_tags/omitempty/string/json_test.go

@@ -0,0 +1,139 @@
+package test
+
+import (
+	"bytes"
+	"encoding/json"
+	"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 < 1000; i++ {
+		var before T
+		fz.Fuzz(&before)
+
+		jbStd, err := json.Marshal(before)
+		if err != nil {
+			t.Errorf("failed to marshal with stdlib: %v", err)
+		}
+		jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
+		if err != nil {
+			t.Errorf("failed to marshal with jsoniter: %v", err)
+		}
+		if string(jbStd) != string(jbIter) {
+			t.Errorf("marshal expected:\n    %s\ngot:\n    %s\nobj:\n    %s",
+				indent(jbStd, "    "), indent(jbIter, "    "), dump(before))
+		}
+
+		var afterStd T
+		err = json.Unmarshal(jbIter, &afterStd)
+		if err != nil {
+			t.Errorf("failed to unmarshal with stdlib: %v", err)
+		}
+		var afterIter T
+		err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
+		if err != nil {
+			t.Errorf("failed to unmarshal with jsoniter: %v", err)
+		}
+		if fingerprint(afterStd) != fingerprint(afterIter) {
+			t.Errorf("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
+	json.Indent(&buf, src, prefix, indentStr)
+	return buf.String()
+}
+
+func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
+	t.ReportAllocs()
+	t.ResetTimer()
+
+	var obj T
+	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 T
+	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 T
+		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)
+}

+ 6 - 0
output_tests/struct_tags/omitempty/string/types.go

@@ -0,0 +1,6 @@
+package test
+
+type T struct {
+	F1 string `json:"F1"`
+	F2 string `json:"F2,omitempty"`
+}

+ 139 - 0
output_tests/struct_tags/omitempty/uint32/json_test.go

@@ -0,0 +1,139 @@
+package test
+
+import (
+	"bytes"
+	"encoding/json"
+	"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 < 1000; i++ {
+		var before T
+		fz.Fuzz(&before)
+
+		jbStd, err := json.Marshal(before)
+		if err != nil {
+			t.Errorf("failed to marshal with stdlib: %v", err)
+		}
+		jbIter, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(before)
+		if err != nil {
+			t.Errorf("failed to marshal with jsoniter: %v", err)
+		}
+		if string(jbStd) != string(jbIter) {
+			t.Errorf("marshal expected:\n    %s\ngot:\n    %s\nobj:\n    %s",
+				indent(jbStd, "    "), indent(jbIter, "    "), dump(before))
+		}
+
+		var afterStd T
+		err = json.Unmarshal(jbIter, &afterStd)
+		if err != nil {
+			t.Errorf("failed to unmarshal with stdlib: %v", err)
+		}
+		var afterIter T
+		err = jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(jbIter, &afterIter)
+		if err != nil {
+			t.Errorf("failed to unmarshal with jsoniter: %v", err)
+		}
+		if fingerprint(afterStd) != fingerprint(afterIter) {
+			t.Errorf("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
+	json.Indent(&buf, src, prefix, indentStr)
+	return buf.String()
+}
+
+func benchmarkMarshal(t *testing.B, name string, fn func(interface{}) ([]byte, error)) {
+	t.ReportAllocs()
+	t.ResetTimer()
+
+	var obj T
+	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 T
+	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 T
+		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)
+}

+ 6 - 0
output_tests/struct_tags/omitempty/uint32/types.go

@@ -0,0 +1,6 @@
+package test
+
+type T struct {
+	F1 uint32 `json:"F1"`
+	F2 uint32 `json:"F2,omitempty"`
+}