...
1 package test
2
3 import (
4 "bytes"
5 "fmt"
6 "github.com/json-iterator/go"
7 "github.com/stretchr/testify/require"
8 "strconv"
9 "testing"
10 "time"
11 "unsafe"
12 )
13
14 func Test_customize_type_decoder(t *testing.T) {
15 t.Skip()
16 jsoniter.RegisterTypeDecoderFunc("time.Time", func(ptr unsafe.Pointer, iter *jsoniter.Iterator) {
17 t, err := time.ParseInLocation("2006-01-02 15:04:05", iter.ReadString(), time.UTC)
18 if err != nil {
19 iter.Error = err
20 return
21 }
22 *((*time.Time)(ptr)) = t
23 })
24
25 val := time.Time{}
26 err := jsoniter.Unmarshal([]byte(`"2016-12-05 08:43:28"`), &val)
27 if err != nil {
28 t.Fatal(err)
29 }
30 year, month, day := val.Date()
31 if year != 2016 || month != 12 || day != 5 {
32 t.Fatal(val)
33 }
34 }
35
36 func Test_customize_byte_array_encoder(t *testing.T) {
37 t.Skip()
38
39 should := require.New(t)
40 jsoniter.RegisterTypeEncoderFunc("[]uint8", func(ptr unsafe.Pointer, stream *jsoniter.Stream) {
41 t := *((*[]byte)(ptr))
42 stream.WriteString(string(t))
43 }, nil)
44
45 val := []byte("abc")
46 str, err := jsoniter.MarshalToString(val)
47 should.Nil(err)
48 should.Equal(`"abc"`, str)
49 }
50
51 type CustomEncoderAttachmentTestStruct struct {
52 Value int32 `json:"value"`
53 }
54
55 type CustomEncoderAttachmentTestStructEncoder struct {}
56
57 func (c *CustomEncoderAttachmentTestStructEncoder) Encode(ptr unsafe.Pointer, stream *jsoniter.Stream) {
58 attachVal, ok := stream.Attachment.(int)
59 stream.WriteRaw(`"`)
60 stream.WriteRaw(fmt.Sprintf("%t %d", ok, attachVal))
61 stream.WriteRaw(`"`)
62 }
63
64 func (c *CustomEncoderAttachmentTestStructEncoder) IsEmpty(ptr unsafe.Pointer) bool {
65 return false
66 }
67
68 func Test_custom_encoder_attachment(t *testing.T) {
69
70 jsoniter.RegisterTypeEncoder("test.CustomEncoderAttachmentTestStruct", &CustomEncoderAttachmentTestStructEncoder{})
71 expectedValue := 17
72 should := require.New(t)
73 buf := &bytes.Buffer{}
74 stream := jsoniter.NewStream(jsoniter.Config{SortMapKeys: true}.Froze(), buf, 4096)
75 stream.Attachment = expectedValue
76 val := map[string]CustomEncoderAttachmentTestStruct{"a": {}}
77 stream.WriteVal(val)
78 stream.Flush()
79 should.Nil(stream.Error)
80 should.Equal("{\"a\":\"true 17\"}", buf.String())
81 }
82
83 func Test_customize_field_decoder(t *testing.T) {
84 type Tom struct {
85 field1 string
86 }
87 jsoniter.RegisterFieldDecoderFunc("jsoniter.Tom", "field1", func(ptr unsafe.Pointer, iter *jsoniter.Iterator) {
88 *((*string)(ptr)) = strconv.Itoa(iter.ReadInt())
89 })
90
91 tom := Tom{}
92 err := jsoniter.Unmarshal([]byte(`{"field1": 100}`), &tom)
93 if err != nil {
94 t.Fatal(err)
95 }
96 }
97
98 func Test_recursive_empty_interface_customization(t *testing.T) {
99 t.Skip()
100 var obj interface{}
101 jsoniter.RegisterTypeDecoderFunc("interface {}", func(ptr unsafe.Pointer, iter *jsoniter.Iterator) {
102 switch iter.WhatIsNext() {
103 case jsoniter.NumberValue:
104 *(*interface{})(ptr) = iter.ReadInt64()
105 default:
106 *(*interface{})(ptr) = iter.Read()
107 }
108 })
109 should := require.New(t)
110 jsoniter.Unmarshal([]byte("[100]"), &obj)
111 should.Equal([]interface{}{int64(100)}, obj)
112 }
113
114 type MyInterface interface {
115 Hello() string
116 }
117
118 type MyString string
119
120 func (ms MyString) Hello() string {
121 return string(ms)
122 }
123
124 func Test_read_custom_interface(t *testing.T) {
125 t.Skip()
126 should := require.New(t)
127 var val MyInterface
128 jsoniter.RegisterTypeDecoderFunc("jsoniter.MyInterface", func(ptr unsafe.Pointer, iter *jsoniter.Iterator) {
129 *((*MyInterface)(ptr)) = MyString(iter.ReadString())
130 })
131 err := jsoniter.UnmarshalFromString(`"hello"`, &val)
132 should.Nil(err)
133 should.Equal("hello", val.Hello())
134 }
135
136 const flow1 = `
137 {"A":"hello"}
138 {"A":"hello"}
139 {"A":"hello"}
140 {"A":"hello"}
141 {"A":"hello"}`
142
143 const flow2 = `
144 {"A":"hello"}
145 {"A":"hello"}
146 {"A":"hello"}
147 {"A":"hello"}
148 {"A":"hello"}
149 `
150
151 type (
152 Type1 struct {
153 A string
154 }
155
156 Type2 struct {
157 A string
158 }
159 )
160
161 func (t *Type2) UnmarshalJSON(data []byte) error {
162 return nil
163 }
164
165 func (t *Type2) MarshalJSON() ([]byte, error) {
166 return nil, nil
167 }
168
169 func TestType1NoFinalLF(t *testing.T) {
170 reader := bytes.NewReader([]byte(flow1))
171 dec := jsoniter.NewDecoder(reader)
172
173 i := 0
174 for dec.More() {
175 data := &Type1{}
176 if err := dec.Decode(data); err != nil {
177 t.Errorf("at %v got %v", i, err)
178 }
179 i++
180 }
181 }
182
183 func TestType1FinalLF(t *testing.T) {
184 reader := bytes.NewReader([]byte(flow2))
185 dec := jsoniter.NewDecoder(reader)
186
187 i := 0
188 for dec.More() {
189 data := &Type1{}
190 if err := dec.Decode(data); err != nil {
191 t.Errorf("at %v got %v", i, err)
192 }
193 i++
194 }
195 }
196
197 func TestType2NoFinalLF(t *testing.T) {
198 reader := bytes.NewReader([]byte(flow1))
199 dec := jsoniter.NewDecoder(reader)
200
201 i := 0
202 for dec.More() {
203 data := &Type2{}
204 if err := dec.Decode(data); err != nil {
205 t.Errorf("at %v got %v", i, err)
206 }
207 i++
208 }
209 }
210
211 func TestType2FinalLF(t *testing.T) {
212 reader := bytes.NewReader([]byte(flow2))
213 dec := jsoniter.NewDecoder(reader)
214
215 i := 0
216 for dec.More() {
217 data := &Type2{}
218 if err := dec.Decode(data); err != nil {
219 t.Errorf("at %v got %v", i, err)
220 }
221 i++
222 }
223 }
224
View as plain text