Skip to content
This repository was archived by the owner on Dec 15, 2025. It is now read-only.

Commit 8fa357a

Browse files
committed
consolidate mor tests
1 parent 761ce8c commit 8fa357a

33 files changed

+1132
-1663
lines changed

api_tests/config_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,25 @@ func Test_use_number_for_unmarshal(t *testing.T) {
1414
should.Nil(api.UnmarshalFromString("123", &obj))
1515
should.Equal(json.Number("123"), obj)
1616
}
17+
18+
func Test_customize_float_marshal(t *testing.T) {
19+
should := require.New(t)
20+
json := jsoniter.Config{MarshalFloatWith6Digits: true}.Froze()
21+
str, err := json.MarshalToString(float32(1.23456789))
22+
should.Nil(err)
23+
should.Equal("1.234568", str)
24+
}
25+
26+
27+
func Test_customize_tag_key(t *testing.T) {
28+
29+
type TestObject struct {
30+
Field string `orm:"field"`
31+
}
32+
33+
should := require.New(t)
34+
json := jsoniter.Config{TagKey: "orm"}.Froze()
35+
str, err := json.MarshalToString(TestObject{"hello"})
36+
should.Nil(err)
37+
should.Equal(`{"field":"hello"}`, str)
38+
}

extension_tests/decoder_test.go

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package test
2+
3+
import (
4+
"testing"
5+
"unsafe"
6+
"time"
7+
"github.com/json-iterator/go"
8+
"github.com/stretchr/testify/require"
9+
"strconv"
10+
)
11+
12+
func Test_customize_type_decoder(t *testing.T) {
13+
t.Skip()
14+
jsoniter.RegisterTypeDecoderFunc("time.Time", func(ptr unsafe.Pointer, iter *jsoniter.Iterator) {
15+
t, err := time.ParseInLocation("2006-01-02 15:04:05", iter.ReadString(), time.UTC)
16+
if err != nil {
17+
iter.Error = err
18+
return
19+
}
20+
*((*time.Time)(ptr)) = t
21+
})
22+
//defer jsoniter.ConfigDefault.(*frozenConfig).cleanDecoders()
23+
val := time.Time{}
24+
err := jsoniter.Unmarshal([]byte(`"2016-12-05 08:43:28"`), &val)
25+
if err != nil {
26+
t.Fatal(err)
27+
}
28+
year, month, day := val.Date()
29+
if year != 2016 || month != 12 || day != 5 {
30+
t.Fatal(val)
31+
}
32+
}
33+
34+
func Test_customize_byte_array_encoder(t *testing.T) {
35+
t.Skip()
36+
//jsoniter.ConfigDefault.(*frozenConfig).cleanEncoders()
37+
should := require.New(t)
38+
jsoniter.RegisterTypeEncoderFunc("[]uint8", func(ptr unsafe.Pointer, stream *jsoniter.Stream) {
39+
t := *((*[]byte)(ptr))
40+
stream.WriteString(string(t))
41+
}, nil)
42+
//defer jsoniter.ConfigDefault.(*frozenConfig).cleanEncoders()
43+
val := []byte("abc")
44+
str, err := jsoniter.MarshalToString(val)
45+
should.Nil(err)
46+
should.Equal(`"abc"`, str)
47+
}
48+
49+
func Test_customize_field_decoder(t *testing.T) {
50+
type Tom struct {
51+
field1 string
52+
}
53+
jsoniter.RegisterFieldDecoderFunc("jsoniter.Tom", "field1", func(ptr unsafe.Pointer, iter *jsoniter.Iterator) {
54+
*((*string)(ptr)) = strconv.Itoa(iter.ReadInt())
55+
})
56+
//defer jsoniter.ConfigDefault.(*frozenConfig).cleanDecoders()
57+
tom := Tom{}
58+
err := jsoniter.Unmarshal([]byte(`{"field1": 100}`), &tom)
59+
if err != nil {
60+
t.Fatal(err)
61+
}
62+
}
63+
64+
65+
func Test_recursive_empty_interface_customization(t *testing.T) {
66+
t.Skip()
67+
var obj interface{}
68+
jsoniter.RegisterTypeDecoderFunc("interface {}", func(ptr unsafe.Pointer, iter *jsoniter.Iterator) {
69+
switch iter.WhatIsNext() {
70+
case jsoniter.NumberValue:
71+
*(*interface{})(ptr) = iter.ReadInt64()
72+
default:
73+
*(*interface{})(ptr) = iter.Read()
74+
}
75+
})
76+
should := require.New(t)
77+
jsoniter.Unmarshal([]byte("[100]"), &obj)
78+
should.Equal([]interface{}{int64(100)}, obj)
79+
}
80+
81+
type MyInterface interface {
82+
Hello() string
83+
}
84+
85+
type MyString string
86+
87+
func (ms MyString) Hello() string {
88+
return string(ms)
89+
}
90+
91+
func Test_read_custom_interface(t *testing.T) {
92+
t.Skip()
93+
should := require.New(t)
94+
var val MyInterface
95+
jsoniter.RegisterTypeDecoderFunc("jsoniter.MyInterface", func(ptr unsafe.Pointer, iter *jsoniter.Iterator) {
96+
*((*MyInterface)(ptr)) = MyString(iter.ReadString())
97+
})
98+
err := jsoniter.UnmarshalFromString(`"hello"`, &val)
99+
should.Nil(err)
100+
should.Equal("hello", val.Hello())
101+
}

extension_tests/extension_test.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package test
2+
3+
import (
4+
"unsafe"
5+
"strconv"
6+
"testing"
7+
"github.com/stretchr/testify/require"
8+
"github.com/json-iterator/go"
9+
)
10+
11+
type TestObject1 struct {
12+
Field1 string
13+
}
14+
15+
type testExtension struct {
16+
jsoniter.DummyExtension
17+
}
18+
19+
func (extension *testExtension) UpdateStructDescriptor(structDescriptor *jsoniter.StructDescriptor) {
20+
if structDescriptor.Type.String() != "test.TestObject1" {
21+
return
22+
}
23+
binding := structDescriptor.GetField("Field1")
24+
binding.Encoder = &funcEncoder{fun: func(ptr unsafe.Pointer, stream *jsoniter.Stream) {
25+
str := *((*string)(ptr))
26+
val, _ := strconv.Atoi(str)
27+
stream.WriteInt(val)
28+
}}
29+
binding.Decoder = &funcDecoder{func(ptr unsafe.Pointer, iter *jsoniter.Iterator) {
30+
*((*string)(ptr)) = strconv.Itoa(iter.ReadInt())
31+
}}
32+
binding.ToNames = []string{"field-1"}
33+
binding.FromNames = []string{"field-1"}
34+
}
35+
36+
func Test_customize_field_by_extension(t *testing.T) {
37+
should := require.New(t)
38+
cfg := jsoniter.Config{}.Froze()
39+
cfg.RegisterExtension(&testExtension{})
40+
obj := TestObject1{}
41+
err := cfg.UnmarshalFromString(`{"field-1": 100}`, &obj)
42+
should.Nil(err)
43+
should.Equal("100", obj.Field1)
44+
str, err := cfg.MarshalToString(obj)
45+
should.Nil(err)
46+
should.Equal(`{"field-1":100}`, str)
47+
}
48+
49+
type funcDecoder struct {
50+
fun jsoniter.DecoderFunc
51+
}
52+
53+
func (decoder *funcDecoder) Decode(ptr unsafe.Pointer, iter *jsoniter.Iterator) {
54+
decoder.fun(ptr, iter)
55+
}
56+
57+
type funcEncoder struct {
58+
fun jsoniter.EncoderFunc
59+
isEmptyFunc func(ptr unsafe.Pointer) bool
60+
}
61+
62+
func (encoder *funcEncoder) Encode(ptr unsafe.Pointer, stream *jsoniter.Stream) {
63+
encoder.fun(ptr, stream)
64+
}
65+
66+
func (encoder *funcEncoder) EncodeInterface(val interface{}, stream *jsoniter.Stream) {
67+
jsoniter.WriteToStream(val, stream, encoder)
68+
}
69+
70+
func (encoder *funcEncoder) IsEmpty(ptr unsafe.Pointer) bool {
71+
if encoder.isEmptyFunc == nil {
72+
return false
73+
}
74+
return encoder.isEmptyFunc(ptr)
75+
}

jsoniter_bool_test.go

Lines changed: 0 additions & 113 deletions
This file was deleted.

0 commit comments

Comments
 (0)