|
7 | 7 | "testing"
|
8 | 8 | "time"
|
9 | 9 | "unsafe"
|
| 10 | + "bytes" |
10 | 11 | )
|
11 | 12 |
|
12 | 13 | func Test_customize_type_decoder(t *testing.T) {
|
@@ -98,3 +99,92 @@ func Test_read_custom_interface(t *testing.T) {
|
98 | 99 | should.Nil(err)
|
99 | 100 | should.Equal("hello", val.Hello())
|
100 | 101 | }
|
| 102 | + |
| 103 | +const flow1 = ` |
| 104 | +{"A":"hello"} |
| 105 | +{"A":"hello"} |
| 106 | +{"A":"hello"} |
| 107 | +{"A":"hello"} |
| 108 | +{"A":"hello"}` |
| 109 | + |
| 110 | +const flow2 = ` |
| 111 | +{"A":"hello"} |
| 112 | +{"A":"hello"} |
| 113 | +{"A":"hello"} |
| 114 | +{"A":"hello"} |
| 115 | +{"A":"hello"} |
| 116 | +` |
| 117 | + |
| 118 | +type ( |
| 119 | + Type1 struct { |
| 120 | + A string |
| 121 | + } |
| 122 | + |
| 123 | + Type2 struct { |
| 124 | + A string |
| 125 | + } |
| 126 | +) |
| 127 | + |
| 128 | +func (t *Type2) UnmarshalJSON(data []byte) error { |
| 129 | + return nil |
| 130 | +} |
| 131 | + |
| 132 | +func (t *Type2) MarshalJSON() ([]byte, error) { |
| 133 | + return nil, nil |
| 134 | +} |
| 135 | + |
| 136 | +func TestType1NoFinalLF(t *testing.T) { |
| 137 | + reader := bytes.NewReader([]byte(flow1)) |
| 138 | + dec := jsoniter.NewDecoder(reader) |
| 139 | + |
| 140 | + i := 0 |
| 141 | + for dec.More() { |
| 142 | + data := &Type1{} |
| 143 | + if err := dec.Decode(data); err != nil { |
| 144 | + t.Errorf("at %v got %v", i, err) |
| 145 | + } |
| 146 | + i++ |
| 147 | + } |
| 148 | +} |
| 149 | + |
| 150 | +func TestType1FinalLF(t *testing.T) { |
| 151 | + reader := bytes.NewReader([]byte(flow2)) |
| 152 | + dec := jsoniter.NewDecoder(reader) |
| 153 | + |
| 154 | + i := 0 |
| 155 | + for dec.More() { |
| 156 | + data := &Type1{} |
| 157 | + if err := dec.Decode(data); err != nil { |
| 158 | + t.Errorf("at %v got %v", i, err) |
| 159 | + } |
| 160 | + i++ |
| 161 | + } |
| 162 | +} |
| 163 | + |
| 164 | +func TestType2NoFinalLF(t *testing.T) { |
| 165 | + reader := bytes.NewReader([]byte(flow1)) |
| 166 | + dec := jsoniter.NewDecoder(reader) |
| 167 | + |
| 168 | + i := 0 |
| 169 | + for dec.More() { |
| 170 | + data := &Type2{} |
| 171 | + if err := dec.Decode(data); err != nil { |
| 172 | + t.Errorf("at %v got %v", i, err) |
| 173 | + } |
| 174 | + i++ |
| 175 | + } |
| 176 | +} |
| 177 | + |
| 178 | +func TestType2FinalLF(t *testing.T) { |
| 179 | + reader := bytes.NewReader([]byte(flow2)) |
| 180 | + dec := jsoniter.NewDecoder(reader) |
| 181 | + |
| 182 | + i := 0 |
| 183 | + for dec.More() { |
| 184 | + data := &Type2{} |
| 185 | + if err := dec.Decode(data); err != nil { |
| 186 | + t.Errorf("at %v got %v", i, err) |
| 187 | + } |
| 188 | + i++ |
| 189 | + } |
| 190 | +} |
0 commit comments