1
0
mirror of https://github.com/json-iterator/go.git synced 2025-06-18 22:57:33 +02:00
Files
extra
output_tests
array
builtins
caseless_unmarshal
json_marshal
map
map_key_text_marshal
partial_unmarshal
slice
array
bool
byte
json_test.go
types.go
float64
int32
ptr_string
string
uint8
bool
byte
float64
int32
map
ptr_array
ptr_bool
ptr_float64
ptr_int32
ptr_map
ptr_slice
ptr_string
ptr_struct_various
ptr_uint8
slice
string
struct_empty
struct_empty_alias
struct_ptr_string
struct_various
uint8
struct
struct_tags
text_marshal
marshal_fail_case.go
skip_tests
.codecov.yml
.gitignore
.travis.yml
LICENSE
README.md
example_test.go
feature_adapter.go
feature_any.go
feature_any_array.go
feature_any_bool.go
feature_any_float.go
feature_any_int32.go
feature_any_int64.go
feature_any_invalid.go
feature_any_nil.go
feature_any_number.go
feature_any_object.go
feature_any_string.go
feature_any_uint32.go
feature_any_uint64.go
feature_config.go
feature_iter.go
feature_iter_array.go
feature_iter_float.go
feature_iter_int.go
feature_iter_object.go
feature_iter_skip.go
feature_iter_skip_sloppy.go
feature_iter_skip_strict.go
feature_iter_string.go
feature_pool.go
feature_reflect.go
feature_reflect_array.go
feature_reflect_extension.go
feature_reflect_map.go
feature_reflect_native.go
feature_reflect_object.go
feature_reflect_slice.go
feature_reflect_struct_decoder.go
feature_stream.go
feature_stream_float.go
feature_stream_int.go
feature_stream_string.go
fuzzy_mode_convert_table.md
jsoniter.go
jsoniter_1dot8_only_test.go
jsoniter_adapter_test.go
jsoniter_alias_test.go
jsoniter_any_array_test.go
jsoniter_any_bool_test.go
jsoniter_any_float_test.go
jsoniter_any_int_test.go
jsoniter_any_map_test.go
jsoniter_any_null_test.go
jsoniter_any_object_test.go
jsoniter_any_string_test.go
jsoniter_array_test.go
jsoniter_bool_test.go
jsoniter_customize_test.go
jsoniter_demo_test.go
jsoniter_encode_interface_test.go
jsoniter_fixed_array_test.go
jsoniter_float_test.go
jsoniter_int_test.go
jsoniter_interface_test.go
jsoniter_invalid_test.go
jsoniter_io_test.go
jsoniter_iterator_test.go
jsoniter_large_file_test.go
jsoniter_map_test.go
jsoniter_must_be_valid_test.go
jsoniter_nested_test.go
jsoniter_null_test.go
jsoniter_object_test.go
jsoniter_optional_test.go
jsoniter_raw_message_test.go
jsoniter_reader_test.go
jsoniter_reflect_native_test.go
jsoniter_skip_test.go
jsoniter_sloppy_test.go
jsoniter_stream_test.go
jsoniter_string_test.go
jsoniter_struct_decoder_test.go
jsoniter_wrap_test.go
test.sh
unmarshal_input_test.go
json-iterator/output_tests/slice/array/byte/json_test.go

153 lines
3.9 KiB
Go
Raw Normal View History

2017-06-29 07:25:19 -07:00
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)
2017-07-02 18:18:12 +08:00
for i := 0; i < 100; i++ {
2017-07-09 11:40:45 +08:00
var before typeForTest
2017-06-29 07:25:19 -07:00
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))
}
2017-07-09 11:40:45 +08:00
var afterStd typeForTest
2017-06-29 07:25:19 -07:00
err = json.Unmarshal(jbIter, &afterStd)
if err != nil {
t.Fatalf("failed to unmarshal with stdlib: %v\nvia:\n %s",
err, indent(jbIter, " "))
}
2017-07-09 11:40:45 +08:00
var afterIter typeForTest
2017-06-29 07:25:19 -07:00
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()
2017-07-09 11:40:45 +08:00
var obj typeForTest
2017-06-29 07:25:19 -07:00
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()
2017-07-09 11:40:45 +08:00
var before typeForTest
2017-06-29 07:25:19 -07:00
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++ {
2017-07-09 11:40:45 +08:00
var after typeForTest
2017-06-29 07:25:19 -07:00
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)
}